commit 4dac1c2d761c9cfc5b801a0502d0a2e9b650ee72
parent bc31c9ccfb4840e72b849453f656c3abba24ba5d
Author: Ed van Bruggen <edvb@uw.edu>
Date: Mon, 1 Jun 2020 01:26:15 -0700
Make symbol self evaluating if only arg to define
Diffstat:
3 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/doc/tisp.1.md b/doc/tisp.1.md
@@ -221,7 +221,8 @@ argument list and rest as macro's body.
Create variable with the name of the first argument, with the value of the
second. If name given is a list use the first element of this list as a new
-functions name and rest of list as its arguments.
+functions name and rest of list as its arguments. If only variable name is
+given make it self evaluating.
#### set!
diff --git a/tibs/doc.tsp b/tibs/doc.tsp
@@ -36,9 +36,10 @@
"(macro args . body)"
"create anonymous macro")
(define
- "(define var val)"
+ "(define var . val)"
"(define (func . args) . body)"
- "creates new variable with value, or create new function if argument list given")
+ "creates new variable with value, or create new function if argument list given"
+ "if value for variable is not given, make it a self-evaluating symbol")
(set!
"(set! var val)"
"change the variable var to val")
diff --git a/tisp.c b/tisp.c
@@ -990,7 +990,7 @@ static Val
prim_define(Tsp st, Hash env, Val args)
{
Val sym, val;
- tsp_arg_min(args, "define", 2);
+ tsp_arg_min(args, "define", 1);
if (car(args)->t == PAIR) { /* create function if given argument list */
sym = caar(args); /* first element of argument list is function name */
if (sym->t != SYMBOL)
@@ -999,12 +999,12 @@ prim_define(Tsp st, Hash env, Val args)
type_str(sym->t));
val = mk_func(FUNCTION, sym->v.s, cdar(args), cdr(args), env);
} else if (car(args)->t == SYMBOL) { /* create variable */
- sym = car(args);
- val = tisp_eval(st, env, cadr(args));
+ sym = car(args); /* if only symbol given, make it self evaluating */
+ val = nilp(cdr(args)) ? sym : tisp_eval(st, env, cadr(args));
} else tsp_warn("define: incorrect format, no variable name found");
if (!val)
return NULL;
- /* set procedure name if it was previously anoymous */
+ /* set procedure name if it was previously anonymous */
if (val->t & (FUNCTION|MACRO) && !val->v.f.name)
val->v.f.name = sym->v.s;
hash_add(env, sym->v.s, val);