tisp

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

commit d639a03bceac93f1cfe8c698381893f2ef117790
parent 16c8d8df6b746c5fde58b273e9974d4502b74ec3
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Tue, 25 Jun 2019 13:43:01 -0700

Test equality of pairs with = primitive

Diffstat:
test.c | 37++++++++++++++++++++++---------------
tisp.c | 11++++++-----
2 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/test.c b/test.c @@ -115,15 +115,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", 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) ((= 1 3) 2))", "()" }, { "(cond ((= 1 2) 1) (\"foo\" 2) (else 3))", "2" }, - { "(cond (() (+ 1 2)))", "()" }, + { "(cond (() (+ 1 2)))", "()" }, { "eq", NULL }, { "(=)", "t" }, @@ -155,6 +155,13 @@ char *tests[][2] = { { "(= car car)", "t" }, { "(= car cdr)", "()" }, { "(= quote quote quote)", "t" }, + { "(= '(1 2 3) '(1 2 3))", "t" }, + { "(= '(a b c) '(a b c))", "t" }, + { "(= '(a b c) '(a b d))", "()" }, + { "(= '(1 2 3) '(1 2))", "()" }, + { "(= '(1 2 3) '(1))", "()" }, + { "(= '((1 2) 3 4) '((1 2) 3 4))", "t" }, + { "(= '((1 b) 3 4) '((1 2) 3 4))", "()" }, { "define", NULL }, { "(define foo 4)", "" }, @@ -331,16 +338,16 @@ char *tests[][2] = { { "(sgn 0)", "0" }, { "(sgn 0.0)", "0" }, - { "max/min", NULL }, - { "(max 9346 13297)", "13297" }, - { "(max -3 8)", "8" }, - { "(max 3/2 1/2)", "3/2" }, - { "(max 0 -.5)", "0" }, - { "(min 4 48)", "4" }, + { "max/min", NULL }, + { "(max 9346 13297)", "13297" }, + { "(max -3 8)", "8" }, + { "(max 3/2 1/2)", "3/2" }, + { "(max 0 -.5)", "0" }, + { "(min 4 48)", "4" }, { "(min -80 -148)", "-148" }, - { "(min 7/2 -3)", "-3" }, - { "(min 1/2 1/3)", "1/3" }, - { "(min .05 .06)", "0.05" }, + { "(min 7/2 -3)", "-3" }, + { "(min 1/2 1/3)", "1/3" }, + { "(min .05 .06)", "0.05" }, { NULL, NULL }, }; diff --git a/tisp.c b/tisp.c @@ -166,10 +166,9 @@ int list_len(Val v) { int len = 0; - Val a; - for (a = v; a->t == PAIR; a = cdr(a)) + for (; v->t == PAIR; v = cdr(v)) len++; - return a->t == NIL ? len : -1; + return nilp(v) ? len : -1; } /* return last element in list */ @@ -185,14 +184,16 @@ list_last(Val v) static int vals_eq(Val a, Val b) { - if (a->t & NUMBER && b->t & NUMBER) { + if (a->t & NUMBER && b->t & NUMBER) { /* NUMBERs */ if (num(a) != num(b) || den(a) != den(b)) return 0; return 1; } if (a->t != b->t) return 0; - if (a != b) /* PRIMITIVE, STRING, SYMBOL */ + if (a->t == PAIR) /* PAIR */ + return vals_eq(car(a), car(b)) && vals_eq(cdr(a), cdr(b)); + if (a != b) /* PROCEDUREs, STRING, SYMBOL, NIL, VOID */ return 0; return 1; }