tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

commit 9c817c76b1a5965203db65704be6770102d6cb0f
parent 5ec23fca3e25c5a0dee3baaa82802831dc502fec
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Sat, 26 Dec 2020 21:53:06 -0800

Add defined? function

Returns bool on if symbol given is assigned a value

Diffstat:
test.c | 6++++++
tisp.c | 15+++++++++++++++
2 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/test.c b/test.c @@ -205,6 +205,12 @@ char *tests[][2] = { { "add2", "2" }, { "(set! add2 \"2\")", "2" }, { "add2", "2" }, + { "defined?", NULL }, + { "(defined? invalid-var)", "Nil" }, + { "(defined? defined?)", "True" }, + { "(defined? car)", "True" }, + { "(defined? when)", "True" }, + { "(defined? apply)", "True" }, { "lambda", NULL }, { "((lambda (x) x) 3)", "3" }, diff --git a/tisp.c b/tisp.c @@ -1000,6 +1000,20 @@ form_set(Tsp st, Hash env, Val args) return val; } +static Val +form_definedp(Tsp st, Hash env, Val args) +{ + Entry e = NULL; + tsp_arg_min(args, "defined?", 1); + tsp_arg_type(car(args), "defined?", TSP_SYM); + for (Hash h = env; h; h = h->next) { + e = entry_get(h, car(args)->v.s); + if (e->key) + break; + } + return (!e || !e->key) ? st->nil : st->t; +} + /* loads tisp file or C dynamic library */ /* TODO lua like error listing places load looked */ /* TODO only use dlopen if -ldl is given with TIB_DYNAMIC */ @@ -1117,6 +1131,7 @@ tisp_env_init(size_t cap) tsp_env_form(def); tsp_env_name_form(set!, set); tsp_env_prim(load); + tsp_env_name_form(defined?, definedp); tsp_env_prim(error); tsp_env_form(version);