diff --git a/.gitignore b/.gitignore index c72ba40..80167fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ lisp - +txt* # Prerequisites *.d diff --git a/README.md b/README.md index 377302c..7c24007 100644 --- a/README.md +++ b/README.md @@ -1 +1,13 @@ * (define fac (lambda (n) (if (== n 0) 1 (* n (fac (+ n -1)))))) + +* macro +```c + (macro and (expr . rest) + (if rest (list 'if expr (cons 'and rest)) expr)) + + (and (== 1 1) (== 0 0) (if nil nil 1) (+ 100 100)) + (and ()) +``` + + + diff --git a/eval.c b/eval.c index ac85839..30b9237 100644 --- a/eval.c +++ b/eval.c @@ -14,12 +14,6 @@ exprlen(Object *expr) return l; } -int -istrue(Object *o) -{ - return o && o->type==OINT && o->num!=0; -} - static int islist(Object *obj) { @@ -280,13 +274,20 @@ fnne(Object *env, Object *list) return newint(gc, cmp(env, list) != 0); } +/* if test then else */ Object* fnif(Object *env, Object *list) { + if(list->type != OCELL || list->cdr->type != OCELL) + error("Malformed if stmt"); Object *test = list->car; test = eval(env, test); - if(istrue(test)) return eval(env, list->cdr->car); - if(list->cdr->cdr == &Nil) return &Nil; + if(test != &Nil) + return eval(env, list->cdr->car); + if(list->cdr->cdr == &Nil) + return &Nil; + if(list->cdr->cdr->type != OCELL) + error("Malformed else stmt"); return eval(env, list->cdr->cdr->car); }