summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Perktold <Felix.Perktold@student.uibk.ac.at>2026-01-22 00:23:18 +0100
committerFelix Perktold <Felix.Perktold@student.uibk.ac.at>2026-01-22 00:23:18 +0100
commiteaa0ad227969429b333d6ef26e4f4c9f266fbee5 (patch)
treec5f64b2e7927783ce57fab5f1420124a4a13e3d9
parente3b8eded6cf924e2f1e532f0db1aef899b451ccf (diff)
changed the repl so defined values don't immediately get evaluated from REPL (important for endless lists etc)
-rw-r--r--lisp.c23
-rw-r--r--lisp.y10
2 files changed, 16 insertions, 17 deletions
diff --git a/lisp.c b/lisp.c
index 324eeb6..139f76f 100644
--- a/lisp.c
+++ b/lisp.c
@@ -94,8 +94,10 @@ value *force_thunk(value *v) {
return v->as.thunk.cached;
}
- v->as.thunk.cached = eval(v->as.thunk.env, v->as.thunk.expr);
- return v;
+ value *evaled = eval(v->as.thunk.env, v->as.thunk.expr);
+ force_thunk(evaled);
+ v->as.thunk.cached = evaled;
+ return evaled;
}
value *cons(value *car, value *cdr) {
@@ -164,16 +166,12 @@ void print_value(value *val) {
printf("(");
print_value(force_thunk(car(val)));
value *p_cdr = cdr(val);
+ p_cdr = force_thunk(p_cdr);
while (p_cdr->type == VT_PAIR || p_cdr->type == VT_THUNK) {
- while (p_cdr->type == VT_THUNK) {
- p_cdr = force_thunk(p_cdr);
- }
- if (p_cdr->type != VT_PAIR) {
- break;
- }
printf(" ");
print_value(car(p_cdr));
p_cdr = cdr(p_cdr);
+ p_cdr = force_thunk(p_cdr);
}
if(p_cdr->type != VT_NIL) {
@@ -251,10 +249,6 @@ value *env_lookup(env *e, const char *sym) {
}
value *eval(env *e, value *v) {
- //printf("evaluating: ");
- //print_value(v);
- //printf("\n");
-
//nil evaluates to itself
if (!v || v->type == VT_NIL) {
return make_nil();
@@ -273,12 +267,11 @@ value *eval(env *e, value *v) {
//pairs get special treatment
if (v->type == VT_PAIR) {
- //printf("pair found: ");
- //println_value(v);
return eval_pair(e, v);
}
- // strings and errors etc do not get evaluated further
+ // strings and errors etc do not get evaluated further, thunks get forced
+ v = force_thunk(v);
return v;
}
diff --git a/lisp.y b/lisp.y
index ec246f3..504e50f 100644
--- a/lisp.y
+++ b/lisp.y
@@ -1,6 +1,7 @@
%{
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "lisp_api.h"
#include "lisp.h"
@@ -9,9 +10,14 @@ int yyerror(const char *s);
void eval_and_print_REPL(value *v) {
if(!v) { return; }
- value *ve = eval(global_env, v);
+ if (v->type == VT_PAIR && car(v)->type == VT_SYMBOL && !strcmp(car(v)->as.sym, "define")) {
+ eval(global_env, v);
+ printf(";> defined ");
+ println_value(car(cdr(v)));
+ return; // Skip printing for define so infinite lists do not eval
+ }
printf(";> ");
- println_value(ve);
+ println_value(eval(global_env, v));
}
%}