From 77e596fe12ffd0b74159e7b9427108d0fc21300c Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 26 Sep 2019 06:54:56 -0700 Subject: lookup_fun: eliminate recursion. * eval.c (lookup_fun); Use iteration to search the nested environments. Put this code ahead of the global search so we fall back on it. Also, let's check for a compound function name first, so we don't search the environments for this. --- eval.c | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/eval.c b/eval.c index c2e85f98..807abdce 100644 --- a/eval.c +++ b/eval.c @@ -533,40 +533,40 @@ val lookup_fun(val env, val sym) { uses_or2; - if (nilp(env)) { - if (consp(sym)) { - if (car(sym) == meth_s) { - val strct = cadr(sym); - val slot = caddr(sym); - val type = or2(find_struct_type(strct), - if2(lisplib_try_load(strct), - find_struct_type(strct))); - if (slot == init_k) { - return cons(sym, struct_get_initfun(type)); - } else if (slot == postinit_k) { - return cons(sym, struct_get_postinitfun(type)); - } else { - return if2(and2(type, static_slot_p(type, slot)), - cons(sym, static_slot(type, slot))); - } - } else if (car(sym) == macro_s) { - return lookup_mac(nil, cadr(sym)); + if (consp(sym)) { + if (car(sym) == meth_s) { + val strct = cadr(sym); + val slot = caddr(sym); + val type = or2(find_struct_type(strct), + if2(lisplib_try_load(strct), + find_struct_type(strct))); + if (slot == init_k) { + return cons(sym, struct_get_initfun(type)); + } else if (slot == postinit_k) { + return cons(sym, struct_get_postinitfun(type)); } else { - return nil; + return if2(and2(type, static_slot_p(type, slot)), + cons(sym, static_slot(type, slot))); } + } else if (car(sym) == macro_s) { + return lookup_mac(nil, cadr(sym)); + } else { + return nil; } - return or2(gethash(top_fb, sym), - if2(lisplib_try_load(sym), gethash(top_fb, sym))); - } else { + } + + if (env) { type_check(lit("function lookup"), env, ENV); - { + for (; env; env = env->e.up_env) { val binding = assoc(sym, env->e.fbindings); if (binding) return binding; - return lookup_fun(env->e.up_env, sym); } } + + return or2(gethash(top_fb, sym), + if2(lisplib_try_load(sym), gethash(top_fb, sym))); } val func_get_name(val fun, val env) -- cgit v1.2.3