tisp

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

commit d124a98e22b9b10db23892ec77fa3caab34f4982
parent 4ac6c0651baaf295c6fd24cfc880ed92594ffa90
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Sat, 24 Oct 2020 23:06:38 -0700

Have mk_list take multiple values instead of array

Diffstat:
tib/math.c | 20++++++++++----------
tisp.c | 18++++++++++--------
tisp.h | 2+-
3 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/tib/math.c b/tib/math.c @@ -193,7 +193,7 @@ prim_pow(Tsp st, Hash vars, Val args) if ((bnum == (int)bnum && bden == (int)bden) || b->t & TSP_DEC || p->t & TSP_DEC) return mk_num(b->t, p->t, 0)(bnum, bden); - return mk_pair(mk_sym(st, "^"), mk_pair(b, mk_pair(p, st->nil))); + return mk_list(st, 3, mk_sym(st, "^"), b, p); } #define PRIM_COMPARE(NAME, OP) \ @@ -214,15 +214,15 @@ PRIM_COMPARE(gt, >) PRIM_COMPARE(lte, <=) PRIM_COMPARE(gte, >=) -#define PRIM_TRIG(NAME) \ -static Val \ -prim_##NAME(Tsp st, Hash vars, Val args) \ -{ \ - tsp_arg_num(args, #NAME, 1); \ - tsp_arg_type(car(args), #NAME, TSP_EXPR); \ - if (car(args)->t & TSP_DEC) \ - return mk_dec(NAME(num(car(args)))); \ - return mk_pair(mk_sym(st, #NAME), mk_pair(car(args), st->nil)); \ +#define PRIM_TRIG(NAME) \ +static Val \ +prim_##NAME(Tsp st, Hash vars, Val args) \ +{ \ + tsp_arg_num(args, #NAME, 1); \ + tsp_arg_type(car(args), #NAME, TSP_EXPR); \ + if (car(args)->t & TSP_DEC) \ + return mk_dec(NAME(num(car(args)))); \ + return mk_list(st, 2, mk_sym(st, #NAME), car(args)); \ } PRIM_TRIG(sin) diff --git a/tisp.c b/tisp.c @@ -380,13 +380,16 @@ mk_pair(Val a, Val b) } Val -mk_list(Tsp st, int n, Val *a) +mk_list(Tsp st, int n, ...) { - int i; - Val b = st->nil; - for (i = n-1; i >= 0; i--) - b = mk_pair(a[i], b); - return b; + Val lst, cur; + va_list argp; + va_start(argp, n); + lst = mk_pair(va_arg(argp, Val), st->nil); + for (cur = lst; n > 1; n--, cur = cdr(cur)) + cdr(cur) = mk_pair(va_arg(argp, Val), st->nil); + va_end(argp); + return lst; } /* read */ @@ -574,8 +577,7 @@ tisp_read(Tsp st) tsp_finc(st); if (!(v = tisp_read(st))) return NULL; - return mk_pair(mk_sym(st, shorthands[i+1]), - mk_pair(v, st->nil)); + return mk_list(st, 2, mk_sym(st, shorthands[i+1]), v); } } if (issym(tsp_fget(st))) diff --git a/tisp.h b/tisp.h @@ -141,7 +141,7 @@ Val mk_sym(Tsp st, char *s); Val mk_prim(TspType t, Prim prim, char *name); Val mk_func(TspType t, char *name, Val args, Val body, Hash env); Val mk_pair(Val a, Val b); -Val mk_list(Tsp st, int n, Val *a); +Val mk_list(Tsp st, int n, ...); Val tisp_read(Tsp st); Val tisp_read_line(Tsp st);