From fddfed044719c2e8d9664a007b214dacc8a0aa36 Mon Sep 17 00:00:00 2001 From: Felix Perktold Date: Tue, 23 Dec 2025 13:12:30 +0100 Subject: fix redefinition of symbols in env, cleanup --- Makefile | 2 +- README.ORG | 1 + lisp.c | 17 ++++++++++------- lisp.h | 4 ++-- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 095a7ae..d6d3fae 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ LEX = flex YACC = bison -CC = gcc -g +CC = gcc -g -Wall -Werror CFLAGS = -DYYDEBUG=1 LIBS = -lreadline -lfl -lm OBJS = lisp.o lisp.tab.o lex.yy.o diff --git a/README.ORG b/README.ORG index 8969629..1ea05cf 100644 --- a/README.ORG +++ b/README.ORG @@ -6,3 +6,4 @@ using lex and yacc + [[https://en.wikipedia.org/wiki/S-expression][s-expressions]] + [[https://www.bowaggoner.com/writeups/jumpstart/flexbison/jumpstart_flexbison.pdf?utm_source][flex / bison jumpstart]] + [[https://github.com/Robert-van-Engelen/tinylisp/blob/main/tinylisp.pdf][tinylisp.pdf]] ++ [[https://w3.pppl.gov/info/readline/A_Short_Completion_Example.html][pppl.gov readline]] diff --git a/lisp.c b/lisp.c index df84b6f..1695ff7 100644 --- a/lisp.c +++ b/lisp.c @@ -35,7 +35,7 @@ value *make_string(const char *s) { return val; } -value *NIL = NULL; +value *NIL; value *make_nil() { if (!NIL) { NIL = malloc(sizeof(value)); @@ -94,12 +94,11 @@ value *reverse(value *list) { return reversed; } -void *println_value(value *val){ +void println_value(value *val){ print_value(val); printf("\n"); } -void *print_value(value *val){ - char *str; +void print_value(value *val){ switch (val->type) { case VT_INT: printf("%d", val->as.i); @@ -159,8 +158,6 @@ void *print_value(value *val){ default: printf("\n"); } - - return val; } env *env_create(env *parent) { @@ -173,6 +170,12 @@ env *env_create(env *parent) { } env *env_define(env *e, const char *sym, value *val) { + //if symbol has been found, redefine + if (e->symbol && !strcmp(e->symbol, sym)) { + e->value = val; + return e; + } + //if not, find end of list and add new definition if (e->next) { return env_define(e->next, sym, val); } @@ -460,5 +463,5 @@ value *builtin_lt(env *e, value *args) { } int is_integer(double x) { - return floor(x) == x && isfinite(x); + return floor(x) == x && isfinite(x); } diff --git a/lisp.h b/lisp.h index bb4d3ae..45986a6 100644 --- a/lisp.h +++ b/lisp.h @@ -49,8 +49,8 @@ value *car(value *cons); value *cdr(value *cons); value *reverse(value *list); -void *print_value(value *val); -void *println_value(value *val); +void print_value(value *val); +void println_value(value *val); // environments, TODO: make this use hashtable // symbol->val typedef struct env { -- cgit v1.2.3