From ac18f54aca98531e8200d8bcc012555c1f195ec1 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 28 Jun 2021 06:22:24 -0700 Subject: constantp: fully expand; recognize functions. This patch improves the constantp function dramatically. It now performs a full expansion of its argument, and recognizes all of the constant foldable functions that the compiler recognizes. * eval.c (const_foldable_s): New symbol variable. (const_foldable_hash): New static variable. (constantp_noex): Look up function in the hash table of const foldable functions, including in the case when it appears in a dwim form as in [+ 2 2] which is (dwim + 2 2). In this case, recursively check the arguments for constantp_noex. We get the hash table of foldable functions from the sys:%const-foldable% variable, which comes from an autoloaded module. (constantp): Fully expand the input form, not just m macroexpand. (eval_init): Register the const_foldable_s variable. * lisplib.c (constfun_instantiate, constfun_set_entries): New static functions. (lisplib_init): Register auto-loading of constfun module via new static functions. * stdlib/compiler.tl; Load the constfun module if %const-foldable% is not defined. (%const-foldable-funs%, %const-foldable%): Removed from here. * stdlib/constfun.tl: New file. (%const-foldable-funs%, %const-foldable%): Moved here. * txr.1: Documented changes to constantp. --- lisplib.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lisplib.c') diff --git a/lisplib.c b/lisplib.c index 37b21ba0..a97f741f 100644 --- a/lisplib.c +++ b/lisplib.c @@ -936,6 +936,23 @@ static val pic_set_entries(val dlt, val fun) return nil; } +static val constfun_instantiate(val set_fun) +{ + funcall1(set_fun, nil); + load(scat2(stdlib_path, lit("constfun"))); + return nil; +} + +static val constfun_set_entries(val dlt, val fun) +{ + val sys_name[] = { + lit("%const-foldable%"), + nil + }; + set_dlt_entries_sys(dlt, sys_name, fun); + return nil; +} + val dlt_register(val dlt, val (*instantiate)(val), val (*set_entries)(val, val)) @@ -993,6 +1010,7 @@ void lisplib_init(void) dlt_register(dl_table, match_instantiate, match_set_entries); dlt_register(dl_table, doc_instantiate, doc_set_entries); dlt_register(dl_table, pic_instantiate, pic_set_entries); + dlt_register(dl_table, constfun_instantiate, constfun_set_entries); reg_fun(intern(lit("try-load"), system_package), func_n1(lisplib_try_load)); } -- cgit v1.2.3