fix if
This commit is contained in:
parent
dc3c31a407
commit
e38c7d5eed
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,5 +1,5 @@
|
|||||||
lisp
|
lisp
|
||||||
|
txt*
|
||||||
# Prerequisites
|
# Prerequisites
|
||||||
*.d
|
*.d
|
||||||
|
|
||||||
|
|||||||
12
README.md
12
README.md
@ -1 +1,13 @@
|
|||||||
* (define fac (lambda (n) (if (== n 0) 1 (* n (fac (+ n -1))))))
|
* (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
17
eval.c
@ -14,12 +14,6 @@ exprlen(Object *expr)
|
|||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
istrue(Object *o)
|
|
||||||
{
|
|
||||||
return o && o->type==OINT && o->num!=0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
islist(Object *obj)
|
islist(Object *obj)
|
||||||
{
|
{
|
||||||
@ -280,13 +274,20 @@ fnne(Object *env, Object *list)
|
|||||||
return newint(gc, cmp(env, list) != 0);
|
return newint(gc, cmp(env, list) != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* if test then else */
|
||||||
Object*
|
Object*
|
||||||
fnif(Object *env, Object *list)
|
fnif(Object *env, Object *list)
|
||||||
{
|
{
|
||||||
|
if(list->type != OCELL || list->cdr->type != OCELL)
|
||||||
|
error("Malformed if stmt");
|
||||||
Object *test = list->car;
|
Object *test = list->car;
|
||||||
test = eval(env, test);
|
test = eval(env, test);
|
||||||
if(istrue(test)) return eval(env, list->cdr->car);
|
if(test != &Nil)
|
||||||
if(list->cdr->cdr == &Nil) return &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);
|
return eval(env, list->cdr->cdr->car);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user