tisp

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

commit 9dd2e4f9c89196fa7c70bbbc039eb3afef364b38
parent 8b19ac55a39fb0d743b921797b8e173fa45157ef
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Tue, 10 Mar 2020 16:35:25 -0700

Always print void type as #<void>

Let REPL choose to print void or not

Diffstat:
test.c | 120++++++++++++++++++++++++++++++++++++++++----------------------------------------
tisp.c | 18++++--------------
2 files changed, 64 insertions(+), 74 deletions(-)

diff --git a/test.c b/test.c @@ -58,14 +58,14 @@ char *tests[][2] = { { "t", "t" }, { "()", "()" }, - { "comments", NULL }, - { "; commment", "" }, - { "; (+ 1 1)", "" }, - { "(+ 1 ; more comments\n1)", "2" }, + { "comments", NULL }, + { "; commment", "#<void>" }, + { "; (+ 1 1)", "#<void>" }, + { "(+ 1 ; more comments\n1)", "2" }, - { "whitespace", NULL }, - { "\t \n \n\n\t\n \t\n", "" }, - { "\t \t(+ \t\t5 \n \n5 \n\t)", "10" }, + { "whitespace", NULL }, + { "\t \n \n\n\t\n \t\n", "#<void>" }, + { "\t \t(+ \t\t5 \n \n5 \n\t)", "10" }, { "quote", NULL }, { "(quote 1)", "1" }, @@ -100,13 +100,13 @@ char *tests[][2] = { { "(cdr (cons 1 (cons 2 3)))", "(2 . 3)" }, { "(cdr (cdr (cons 1 (cons 2 3))))", "3" }, - { "void", NULL }, - { "(void)", "" }, + { "void", NULL }, + { "(void)", "#<void>" }, - { "do", NULL }, - { "(do (+ 1 2) (+ 2 2))", "4" }, - { "(do (+ -4 8) (- 1 2) (* 80 0) (+ 39 -3))", "36" }, - { "(do (mod 80 2) (/ 4 2) (void))", "" }, + { "do", NULL }, + { "(do (+ 1 2) (+ 2 2))", "4" }, + { "(do (+ -4 8) (- 1 2) (* 80 0) (+ 39 -3))", "36" }, + { "(do (mod 80 2) (/ 4 2) (void))", "#<void>" }, { "eval", NULL }, { "(eval ''hey)", "hey" }, @@ -116,15 +116,15 @@ char *tests[][2] = { { "(eval ''(mod 9 3))", "(mod 9 3)" }, { "(do (define bar '(/ 25 5)) (eval bar))", "5" }, - { "cond", NULL }, - { "(cond)", "" }, - { "(cond (t 1))", "1" }, - { "(cond ((= 1 1) 1) ((= 1 2) 2) (t 3))", "1" }, - { "(cond ((= 1 2) 1) ((= 1 2) 2) (else (+ 1 2)))", "3" }, - { "(cond ((= 1 2) 1) ((= 1 1) 2) (else 3))", "2" }, - { "(cond ((= 1 2) 1) ((= 1 3) 2))", "" }, - { "(cond ((= 1 2) 1) (\"foo\" 2) (else 3))", "2" }, - { "(cond (() (+ 1 2)))", "" }, + { "cond", NULL }, + { "(cond)", "#<void>" }, + { "(cond (t 1))", "1" }, + { "(cond ((= 1 1) 1) ((= 1 2) 2) (t 3))", "1" }, + { "(cond ((= 1 2) 1) ((= 1 2) 2) (else (+ 1 2)))", "3" }, + { "(cond ((= 1 2) 1) ((= 1 1) 2) (else 3))", "2" }, + { "(cond ((= 1 2) 1) ((= 1 3) 2))", "#<void>" }, + { "(cond ((= 1 2) 1) (\"foo\" 2) (else 3))", "2" }, + { "(cond (() (+ 1 2)))", "#<void>" }, { "eq", NULL }, { "(=)", "t" }, @@ -164,30 +164,30 @@ char *tests[][2] = { { "(= '((1 2) 3 4) '((1 2) 3 4))", "t" }, { "(= '((1 b) 3 4) '((1 2) 3 4))", "()" }, - { "define", NULL }, - { "(define foo 4)", "" }, - { "foo", "4" }, - { "(define bar foo)", "" }, - { "bar", "4" }, - { "(set! foo 5)", "5" }, - { "foo", "5" }, - { "(set! foo (+ foo bar))", "9" }, - { "foo", "9" }, - { "(define add +)", "" }, - { "(add foo bar)", "13" }, - { "(define (one x) (add x 1))", "" }, - { "(one foo)", "10" }, + { "define", NULL }, + { "(define foo 4)", "#<void>" }, + { "foo", "4" }, + { "(define bar foo)", "#<void>" }, + { "bar", "4" }, + { "(set! foo 5)", "5" }, + { "foo", "5" }, + { "(set! foo (+ foo bar))", "9" }, + { "foo", "9" }, + { "(define add +)", "#<void>" }, + { "(add foo bar)", "13" }, + { "(define (one x) (add x 1))", "#<void>" }, + { "(one foo)", "10" }, { "(define (more x)" " (define term 3)" - " (+ x term))", "" }, - { "(more 8)", "11" }, + " (+ x term))", "#<void>" }, + { "(more 8)", "11" }, { "(define (add2 x)" - " (+ x 1) (+ x 2))", "" }, - { "(add2 2)", "4" }, - { "(set! add2 2)", "2" }, - { "add2", "2" }, - { "(set! add2 \"2\")", "\"2\"" }, - { "add2", "\"2\"" }, + " (+ x 1) (+ x 2))", "#<void>" }, + { "(add2 2)", "4" }, + { "(set! add2 2)", "2" }, + { "add2", "2" }, + { "(set! add2 \"2\")", "\"2\"" }, + { "add2", "\"2\"" }, { "lambda", NULL }, { "((lambda (x) x) 3)", "3" }, @@ -196,22 +196,22 @@ char *tests[][2] = { { "((lambda (a b) (+ a b)) 2 2)", "4" }, { "((lambda () 5))", "5" }, - { "control", NULL }, - { "(if t 1 2)", "1" }, - { "(if () 1 2)", "2" }, - { "(if (integer? 3) t ())", "t" }, - { "(if (ratio? car) (cons 1 2) (car '(1 2)))", "1" }, - { "(when t 'foo)", "foo" }, - { "(when () 'b ar)", "" }, - { "(when (= 1 1) 4)", "4" }, - { "(unless t 'foo)", "" }, - { "(unless () 'bar)", "bar" }, - { "(unless 3 4)", "" }, - { "(unless (< 5 4) 7)", "7" }, - { "(switch 5 (3 'yes) (5 'no))", "no" }, - { "(switch (+ 1 2) ((mod 8 5) 'yes) (err 'no))", "yes" }, - { "(switch 2 (3 'yes) (5 'no))", "" }, - { "(switch \"foo\" (e \"bar\") (\"foo\" 'zar) ('baz 3))", "zar" }, + { "control", NULL }, + { "(if t 1 2)", "1" }, + { "(if () 1 2)", "2" }, + { "(if (integer? 3) t ())", "t" }, + { "(if (ratio? car) (cons 1 2) (car '(1 2)))", "1" }, + { "(when t 'foo)", "foo" }, + { "(when () 'b ar)", "#<void>" }, + { "(when (= 1 1) 4)", "4" }, + { "(unless t 'foo)", "#<void>" }, + { "(unless () 'bar)", "bar" }, + { "(unless 3 4)", "#<void>" }, + { "(unless (< 5 4) 7)", "7" }, + { "(switch 5 (3 'yes) (5 'no))", "no" }, + { "(switch (+ 1 2) ((mod 8 5) 'yes) (err 'no))", "yes" }, + { "(switch 2 (3 'yes) (5 'no))", "#<void>" }, + { "(switch \"foo\" (e \"bar\") (\"foo\" 'zar) ('baz 3))", "zar" }, { "logic", NULL }, { "(not ())", "t" }, @@ -348,7 +348,7 @@ char *tests[][2] = { { "(swap (list 1/2 1/4 1/9 1/16))", "(1/4 1/2 1/9 1/16)" }, { "stack!", NULL }, - { "(define s '(1 2 3 4 5))", "" }, + { "(define s '(1 2 3 4 5))", "#<void>" }, { "(peek s)", "1" }, { "(pop! s)", "1" }, { "s", "(2 3 4 5)" }, diff --git a/tisp.c b/tisp.c @@ -742,23 +742,13 @@ tisp_eval(Tsp st, Hash env, Val v) /* print */ -/* print value of a list, display as #<void> if element inside returns void */ -/* TODO remove need for list_print by using repl to skip printing */ -static void -list_print(FILE *f, Val v) -{ - if (v->t == NONE) - fprintf(f, "#<void>"); - else - tisp_print(f, v); -} - /* main print function */ void tisp_print(FILE *f, Val v) { switch (v->t) { case NONE: + fprintf(f, "#<void>"); break; case NIL: fprintf(f, "()"); @@ -791,14 +781,14 @@ tisp_print(FILE *f, Val v) break; case PAIR: putc('(', f); - list_print(f, car(v)); + tisp_print(f, car(v)); for (v = cdr(v); !nilp(v); v = cdr(v)) { if (v->t == PAIR) { putc(' ', f); - list_print(f, car(v)); + tisp_print(f, car(v)); } else { fprintf(f, " . "); - list_print(f, v); + tisp_print(f, v); break; } }