tisp

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

commit 74059fd448ccc311417ceddf591a1825cffe3ecf
parent b6f853c5d4282f67a106d0e5ba90963f5525cad0
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Sat, 14 Apr 2018 16:24:22 -0700

Add - primitive to subtract and negate integers

Diffstat:
tib/math.c | 19++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/tib/math.c b/tib/math.c @@ -23,8 +23,25 @@ prim_add(Hash env, Val args) return mk_int(i); } +static Val +prim_sub(Hash env, Val args) +{ + Val v; + int i = 0; + if (!(v = eval_list(env, args))) + return NULL; + INC(+, "-"); + v = cdr(v); + if (nilp(v)) + return mk_int(-i); + for (; !nilp(v); v = cdr(v)) + INC(-, "-"); + return mk_int(i); +} + void tib_math_env(Hash ht) { - hash_add(ht, "+", mk_prim(prim_add)); + hash_add(ht, "+", mk_prim(prim_add)); + hash_add(ht, "-", mk_prim(prim_sub)); }