This commit is contained in:
yoyo 2024-09-08 11:31:39 +09:00
parent dc3c31a407
commit e38c7d5eed
3 changed files with 22 additions and 9 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
lisp
txt*
# Prerequisites
*.d

View File

@ -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 ())
```

17
eval.c
View File

@ -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);
}