tisp

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

commit 16ebac4e16de5a393f4d35061f24a7e248801dc9
parent a3d16089c88f4788b659b894f9a751b89318ad95
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Tue, 24 Dec 2019 20:24:11 -0800

String and symbol to concatenate into single value

Convert all arguments supplied to each primitive into a single string or
symbol.

Diffstat:
Makefile | 5+++--
test.c | 2++
tibs/string.c | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
3 files changed, 61 insertions(+), 29 deletions(-)

diff --git a/Makefile b/Makefile @@ -50,6 +50,7 @@ clean: @echo cleaning @rm -f $(OBJ) $(LIB) $(EXE) test test.o libs.tsp.h +# TODO don't cp some if not in dynamic mode install: all @echo installing $(EXE) to $(DESTDIR)$(PREFIX)/bin @mkdir -p $(DESTDIR)$(PREFIX)/bin @@ -78,10 +79,10 @@ uninstall: @echo removing tisp libraries from $(DESTDIR)$(PREFIX)/share/tisp @rm -rf $(DESTDIR)$(PREFIX)/share/tisp/ -test: $(OBJ) $(LIB) test.o +test: $(OBJ) $(LIB) libs.tsp.h test.o @echo running tests @echo $(CC) -o test - @$(CC) -o test tisp.o tibs/math.o test.o $(LDFLAGS) + @$(CC) -o test tisp.o $(TIB:.c=.o) test.o $(LDFLAGS) @./test man: diff --git a/test.c b/test.c @@ -9,6 +9,7 @@ #include "libs.tsp.h" tsp_include_tib(math); +tsp_include_tib(string); #define LEN(X) (sizeof(X) / sizeof((X)[0])) @@ -521,6 +522,7 @@ main(void) int errors[LEN(tests)] = {0}; Tsp st = tisp_env_init(1024); tib_env_math(st); + tib_env_string(st); tisp_env_lib(st, libs_tsp); for (int i = 0; ; i++) { diff --git a/tibs/string.c b/tibs/string.c @@ -28,32 +28,61 @@ typedef Val (*MkFn)(Tsp, char*); /* TODO string tib: strlen lower upper strpos strsub */ -/* TODO return string val in val_string to be concated */ +/* TODO simplify by using fmemopen and tisp_print */ static Val -val_string(Tsp st, Val v, MkFn mk_fn) +val_string(Tsp st, Val args, MkFn mk_fn) { - char s[43]; - switch (v->t) { - case NONE: - return mk_fn(st, "void"); - case NIL: - return mk_fn(st, "nil"); - case INTEGER: - snprintf(s, 21, "%d", (int)v->v.n.num); - return mk_fn(st, s); - case DECIMAL: - snprintf(s, 17, "%.15g", v->v.n.num); - return mk_fn(st, s); - case RATIO: - snprintf(s, 43, "%d/%d", (int)v->v.n.num, (int)v->v.n.den); - return mk_fn(st, s); - case STRING: - case SYMBOL: - return mk_fn(st, v->v.s); - case PAIR: - default: - tsp_warnf("could not convert type %s into string", type_str(v->t)); + Val v; + char s[43], *ret = calloc(1, sizeof(char)); + int len = 1; + for (; !nilp(args); args = cdr(args)) { + v = car(args); + switch (v->t) { + case NONE: + len += 5; + ret = realloc(ret, len*sizeof(char)); + strcat(ret, "void"); + break; + case NIL: + len += 4; + ret = realloc(ret, len*sizeof(char)); + strcat(ret, "nil"); + break; + case INTEGER: + snprintf(s, 21, "%d", (int)v->v.n.num); + len += strlen(s); + s[len] = '\0'; + ret = realloc(ret, len*sizeof(char)); + strcat(ret, s); + break; + case DECIMAL: + snprintf(s, 17, "%.15g", v->v.n.num); + len += strlen(s); + s[len] = '\0'; + ret = realloc(ret, len*sizeof(char)); + strcat(ret, s); + break; + case RATIO: + snprintf(s, 43, "%d/%d", (int)v->v.n.num, (int)v->v.n.den); + len += strlen(s); + s[len] = '\0'; + ret = realloc(ret, len*sizeof(char)); + strcat(ret, s); + break; + case STRING: + case SYMBOL: + len += strlen(v->v.s); + ret = realloc(ret, len*sizeof(char)); + strcat(ret, v->v.s); + break; + case PAIR: + default: + tsp_warnf("could not convert type %s into string", type_str(v->t)); + } } + v = mk_fn(st, ret); + free(ret); + return v; } /* TODO string and symbol: multi arguments to concat */ @@ -61,8 +90,8 @@ static Val prim_string(Tsp st, Hash env, Val args) { Val v; - tsp_arg_num(args, "string", 1); - if (!(v = tisp_eval(st, env, car(args)))) + tsp_arg_min(args, "string", 1); + if (!(v = tisp_eval_list(st, env, args))) return NULL; return val_string(st, v, mk_str); } @@ -71,8 +100,8 @@ static Val prim_symbol(Tsp st, Hash env, Val args) { Val v; - tsp_arg_num(args, "symbol", 1); - if (!(v = tisp_eval(st, env, car(args)))) + tsp_arg_min(args, "symbol", 1); + if (!(v = tisp_eval_list(st, env, args))) return NULL; return val_string(st, v, mk_sym); }