summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Perktold <Felix.Perktold@student.uibk.ac.at>2026-02-01 13:37:50 +0100
committerFelix Perktold <Felix.Perktold@student.uibk.ac.at>2026-02-01 13:37:50 +0100
commit36425a408d841093a12e2b74328bcb7aed6acafc (patch)
tree7661fbc51886e3dd030d3e71be21057be5ab9d09
parentf8fe85b3203ea5e675af6fa4a66c3c8b7c7575e3 (diff)
improved lambda printingmaster
-rw-r--r--lisp.c29
-rw-r--r--lisp.y3
-rw-r--r--lisp_api.h3
3 files changed, 21 insertions, 14 deletions
diff --git a/lisp.c b/lisp.c
index c95bdf7..73d7fa0 100644
--- a/lisp.c
+++ b/lisp.c
@@ -154,17 +154,17 @@ value *reverse(value *list) {
}
void println_value(value *val) {
- print_value(val, -1);
- //print_value(val, 50);
+ print_value(global_env, val, -1);
+ //print_value(global_env, val, 50);
printf("\n");
}
void println_value_shallow(value *val) {
- print_value(val, 0);
+ print_value(global_env, val, 0);
printf("\n");
}
-void print_value(value *val, int depth) {
+void print_value(env *e, value *val, int depth) {
if(!val) { return; }
switch (val->type) {
case VT_INT:
@@ -176,7 +176,14 @@ void print_value(value *val, int depth) {
break;
case VT_SYMBOL:
- printf("%s", val->as.sym);
+ value *symbol_found = env_lookup(e, val->as.sym);
+ if (!depth || !symbol_found ||
+ symbol_found->type == VT_PROCEDURE ||
+ symbol_found->type == VT_LAMBDA) {
+ printf("%s", val->as.sym);
+ return;
+ }
+ print_value(e, symbol_found, depth-1);
break;
case VT_STRING:
@@ -193,19 +200,19 @@ void print_value(value *val, int depth) {
return;
}
printf("(");
- print_value(force_thunk(car(val)), depth-1);
+ print_value(e, force_thunk(car(val)), depth-1);
value *p_cdr = cdr(val);
p_cdr = force_thunk(p_cdr);
while (p_cdr->type == VT_PAIR || p_cdr->type == VT_THUNK) {
printf(" ");
- print_value(car(p_cdr), depth-1);
+ print_value(e, car(p_cdr), depth-1);
p_cdr = cdr(p_cdr);
p_cdr = force_thunk(p_cdr);
}
if(p_cdr->type != VT_NIL) {
printf(" . ");
- print_value(p_cdr, depth-1);
+ print_value(e, p_cdr, depth-1);
}
printf(")");
break;
@@ -215,7 +222,7 @@ void print_value(value *val, int depth) {
printf("<lambda>");
return;
}
- print_value(
+ print_value(val->as.lambda.env,
cons(make_symbol("λ"),
cons(val->as.lambda.params, val->as.lambda.body)
),
@@ -241,7 +248,7 @@ void print_value(value *val, int depth) {
printf("<thunk>");
return;
}
- print_value(force_thunk(val), depth-1);
+ print_value(val->as.thunk.env, force_thunk(val), depth-1);
break;
default:
printf("\n<unknown>[as str:%s][as f:%f][as int:%d]\n", val->as.str, val->as.d, val->as.i);
@@ -433,7 +440,7 @@ value *procedure_load_module(env *e, value *args) {
if (fst_arg->type != VT_STRING) {
printf("error: not a string:");
- print_value(fst_arg, 3);
+ print_value(global_env, fst_arg, 3);
return make_error("not a string: ");
}
diff --git a/lisp.y b/lisp.y
index 2ed97bf..f566990 100644
--- a/lisp.y
+++ b/lisp.y
@@ -12,8 +12,7 @@ void eval_and_print_REPL(value *v) {
if(!v) { return; }
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)));
+ printf(";> defined %s\n", car(cdr(v))->as.sym);
return; // Skip printing for define so infinite lists do not eval
}
value *evaled = eval(global_env, v);
diff --git a/lisp_api.h b/lisp_api.h
index 91de402..6b1c3e0 100644
--- a/lisp_api.h
+++ b/lisp_api.h
@@ -6,7 +6,8 @@ typedef struct env env;
value *eval(env *e, value *val);
-void print_value(value *val, int depth);
+void print_value(env *e, value *val, int depth);
+void println_value_shallow(value *val);
void println_value(value *val);
typedef enum {