From 1dfba9f4a1f35fbe37a03fd4d221b6903aaa6175 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 7 May 2023 12:49:09 -0400 Subject: [PATCH] add parent environments --- src/mumble.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/mumble.c b/src/mumble.c index 7f997f7..548d72e 100644 --- a/src/mumble.c +++ b/src/mumble.c @@ -286,15 +286,17 @@ struct lispenv { int count; char** syms; // list of strings lispval** vals; // list of pointers to vals + lispenv* parent; }; lispenv* new_lispenv() { - lispenv* n = malloc(sizeof(lispenv)); - n->count = 0; - n->syms = NULL; - n->vals = NULL; - return n; + lispenv* e = malloc(sizeof(lispenv)); + e->count = 0; + e->syms = NULL; + e->vals = NULL; + e->parent = NULL; + return e; } void destroy_lispenv(lispenv* env) @@ -311,8 +313,12 @@ void destroy_lispenv(lispenv* env) env->vals = NULL; free(env); env = NULL; + // parent is it's own environment + // so it isn't destroyed } + + lispval* clone_lispval(lispval* old); lispval* get_from_lispenv(char* sym, lispenv* env) { @@ -322,7 +328,13 @@ lispval* get_from_lispenv(char* sym, lispenv* env) // return env->vals[i]; } } - return lispval_err("Error: unbound symbol"); + + if(env->parent != NULL){ + return get_from_lispenv(sym, env->parent); + } else { + return lispval_err("Error: unbound symbol"); + } + // and this explains shadowing! } void insert_in_lispenv(char* sym, lispval* v, lispenv* env) @@ -739,6 +751,9 @@ lispval* builtin_def(lispval* v, lispenv* env) // A builtin for defining a function lispval* builtin_define_lambda(lispval* v, lispenv* env){ // @ { {x y} { + x y } } + // def { {plus} {{@ {x y} {+ x y}} }} + // (eval plus) 1 2 + // (@ { {x y} { + x y } }) 1 2 LISPVAL_ASSERT( v->count == 2, "Lambda definition requires two arguments; try @ { {x y} { + x y } }"); LISPVAL_ASSERT(v->cell[0]->type == LISPVAL_QEXPR, "Lambda definition (@) requires that the first sub-arg be a q-expression; try @ { {x y} { + x y } }"); LISPVAL_ASSERT(v->cell[1]->type == LISPVAL_QEXPR, "Lambda definition (@) requires that the second sub-arg be a q-expression; try @ { {x y} { + x y } }");