tisp

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

commit 70ae628f495a1b219a3444d3cc1e60373bbe3cc1
parent b7bb03cb98be281977d54c41514026657414a183
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Wed, 22 Aug 2018 15:56:39 -0700

Remove macro to create mk val functions

Dynamically allocate string argument in mk_str itself

Diffstat:
tisp.c | 47+++++++++++++++++++++++++++++++++++------------
1 file changed, 35 insertions(+), 12 deletions(-)

diff --git a/tisp.c b/tisp.c @@ -224,19 +224,15 @@ hash_merge(Hash ht, Hash ht2) hash_add(ht, ht2->items[i].key, ht2->items[i].val); } -#define MK_TYPE(TYPE, TYPE_NAME, TYPE_FULL, FN_NAME) \ -Val FN_NAME(TYPE TYPE_NAME) { \ - Val ret = emalloc(sizeof(struct Val)); \ - ret->t = TYPE_FULL; \ - ret->v.TYPE_NAME = TYPE_NAME; \ - return ret; \ +Val +mk_int(int i) +{ + Val ret = emalloc(sizeof(struct Val)); + ret->t = INTEGER; + ret->v.i = i; + return ret; } -MK_TYPE(int, i, INTEGER, mk_int) -MK_TYPE(char *, s, STRING, mk_str) -MK_TYPE(char *, s, SYMBOL, mk_sym) -MK_TYPE(Prim, pr, PRIMITIVE, mk_prim) - Val mk_rat(int num, int den) { @@ -255,6 +251,33 @@ mk_rat(int num, int den) return ret; } +Val mk_str(char *s) { + Val ret = emalloc(sizeof(struct Val)); + ret->t = STRING; + ret->v.s = emalloc((strlen(s)+1) * sizeof(char)); + strcpy(ret->v.s, s); + return ret; +} + +/* TODO return existing symbol */ +Val +mk_sym(char *s) +{ + Val ret = emalloc(sizeof(struct Val)); + ret->t = SYMBOL; + ret->v.s = s; + return ret; +} + +Val +mk_prim(Prim pr) +{ + Val ret = emalloc(sizeof(struct Val)); + ret->t = PRIMITIVE; + ret->v.pr = pr; + return ret; +} + Val mk_func(Val args, Val body, Env env) { @@ -318,7 +341,7 @@ read_str(Str str) char *s = ++str->d; for (; *str->d && *str->d++ != '"'; len++); s[len] = '\0'; - return mk_str(estrdup(s)); + return mk_str(s); } Val