From 6a5ea25622c3dbab61b35bab87506c83206ec5fd Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sat, 15 Oct 2016 19:56:39 -0700 Subject: Support nil env in env-fbind and env-vbind. * eval.c (env_fbind, env_vbind): Allow env to be nil, indicating that the binding is to take place in the global environment. * txr.1: Documented. --- eval.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'eval.c') diff --git a/eval.c b/eval.c index ed87549b..82afee1c 100644 --- a/eval.c +++ b/eval.c @@ -149,20 +149,34 @@ static val make_env_intrinsic(val vbindings, val fbindings, val up_env) val env_fbind(val env, val sym, val fun) { - val cell; - type_check(env, ENV); - cell = acons_new_c(sym, nulloc, mkloc(env->e.fbindings, env)); - rplacd(cell, fun); - return cell; + if (env) { + val cell; + type_check(env, ENV); + cell = acons_new_c(sym, nulloc, mkloc(env->e.fbindings, env)); + return rplacd(cell, fun); + } else { + val hcell = gethash_c(top_fb, sym, nulloc); + val cell = cdr(hcell); + if (cell) + return rplacd(cell, fun); + return sys_rplacd(hcell, cons(sym, fun)); + } } val env_vbind(val env, val sym, val obj) { - val cell; - type_check(env, ENV); - cell = acons_new_c(sym, nulloc, mkloc(env->e.vbindings, env)); - rplacd(cell, obj); - return cell; + if (env) { + val cell; + type_check(env, ENV); + cell = acons_new_c(sym, nulloc, mkloc(env->e.vbindings, env)); + return rplacd(cell, obj); + } else { + val hcell = gethash_c(top_vb, sym, nulloc); + val cell = cdr(hcell); + if (cell) + return rplacd(cell, obj); + return sys_rplacd(hcell, cons(sym, obj)); + } } static void env_vb_to_fb(val env) -- cgit v1.2.3