summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Perktold <Felix.Perktold@student.uibk.ac.at>2025-12-23 13:12:30 +0100
committerFelix Perktold <Felix.Perktold@student.uibk.ac.at>2025-12-23 13:12:30 +0100
commitfddfed044719c2e8d9664a007b214dacc8a0aa36 (patch)
tree3a5d6966fb0dacae3dc8b19022ef4527b77ff439
parent3a9a1921fe7a56cefe6650529a2572e9a8debc98 (diff)
fix redefinition of symbols in env, cleanup
-rw-r--r--Makefile2
-rw-r--r--README.ORG1
-rw-r--r--lisp.c17
-rw-r--r--lisp.h4
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("<unknown>\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 {