tisp

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

commit 17a56db499f8a126beaf1febce5451f6a9406f07
parent 49c87d325f4a4e83960a1c4d34a13a85c6186c01
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Sat, 12 Jan 2019 18:56:00 -0800

Add void primitive to return nothing

Allow for void type to be printed in a list.

Diffstat:
doc/tisp.1.md | 5+++++
tisp.c | 38+++++++++++++++++++++++++++-----------
2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/doc/tisp.1.md b/doc/tisp.1.md @@ -144,6 +144,11 @@ as the cdr. Returns the given argument unevaluated. +#### void + +Returns nothing. Used to insert a void type in a list or force a function not +to return anything. + #### = Tests if multiple values equal. Returns nil if any are not, and `t` otherwise. diff --git a/tisp.c b/tisp.c @@ -600,6 +600,15 @@ tisp_eval(Env env, Val v) return v; } +static void +list_print(FILE *f, Val v) +{ + if (v->t == NONE) + fprintf(f, "#<void>"); + else + tisp_print(f, v); +} + /* TODO return str for error msgs? */ void tisp_print(FILE *f, Val v) @@ -635,16 +644,16 @@ tisp_print(FILE *f, Val v) break; case PAIR: putc('(', f); - tisp_print(f, car(v)); + list_print(f, car(v)); v = cdr(v); while (!nilp(v)) { if (v->t == PAIR) { putc(' ', f); - tisp_print(f, car(v)); + list_print(f, car(v)); v = cdr(v); } else { fprintf(f, " . "); - tisp_print(f, v); + list_print(f, v); break; } } @@ -688,6 +697,19 @@ prim_cons(Env env, Val args) } static Val +prim_quote(Env env, Val args) +{ + tsp_arg_num(args, "quote", 1); + return car(args); +} + +static Val +prim_void(Env env, Val args) +{ + return env->none; +} + +static Val prim_eq(Env env, Val args) { Val v; @@ -702,13 +724,6 @@ prim_eq(Env env, Val args) } static Val -prim_quote(Env env, Val args) -{ - tsp_arg_num(args, "quote", 1); - return car(args); -} - -static Val prim_cond(Env env, Val args) { Val v, cond; @@ -808,8 +823,9 @@ tisp_env_init(size_t cap) hash_add(e->h, "car", mk_prim(prim_car)); hash_add(e->h, "cdr", mk_prim(prim_cdr)); hash_add(e->h, "cons", mk_prim(prim_cons)); - hash_add(e->h, "=", mk_prim(prim_eq)); hash_add(e->h, "quote", mk_prim(prim_quote)); + hash_add(e->h, "void", mk_prim(prim_void)); + hash_add(e->h, "=", mk_prim(prim_eq)); hash_add(e->h, "cond", mk_prim(prim_cond)); hash_add(e->h, "lambda", mk_prim(prim_lambda)); hash_add(e->h, "define", mk_prim(prim_define));