From 36425a408d841093a12e2b74328bcb7aed6acafc Mon Sep 17 00:00:00 2001 From: Felix Perktold Date: Sun, 1 Feb 2026 13:37:50 +0100 Subject: improved lambda printing --- lisp.c | 29 ++++++++++++++++++----------- lisp.y | 3 +-- lisp_api.h | 3 ++- 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(""); 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(""); return; } - print_value(force_thunk(val), depth-1); + print_value(val->as.thunk.env, force_thunk(val), depth-1); break; default: printf("\n[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 { -- cgit v1.2.3