iris

small scheme interpreter
git clone git://git.2f30.org/iris
Log | Files | Refs | LICENSE

commit 65ae92823f2db07e31caf8df8fadf6c3154401e2
parent 975ef834508906247bda5ad2d1492213eff4c0bf
Author: sin <sin@2f30.org>
Date:   Thu, 15 May 2014 13:36:38 +0100

Add if handling

Diffstat:
Mparser.c | 23+++++++++++++++++++++++
1 file changed, 23 insertions(+), 0 deletions(-)

diff --git a/parser.c b/parser.c @@ -60,6 +60,7 @@ struct object { }; static struct object *evaldefine(struct object *); +static struct object *evalif(struct object *); static struct object *evalok(struct object *); static struct object *evalquote(struct object *); static struct object *evalset(struct object *); @@ -70,6 +71,7 @@ struct { struct object *(*fn)(struct object *); } builtins[] = { { .name = "define", .fn = evaldefine }, + { .name = "if", .fn = evalif }, { .name = "ok", .fn = evalok }, { .name = "quote", .fn = evalquote }, { .name = "set", .fn = evalset }, @@ -320,6 +322,27 @@ evaldefine(struct object *o) } static struct object * +evalif(struct object *o) +{ + struct object *ocar; + struct object *predicate; + + if (o->type != OPair) + return NULL; + ocar = car(o); + if (ocar->type != OIdentifier || + strcmp(ocar->d.i.s, "if") != 0) + return NULL; + predicate = eval(cadr(o)); + if (predicate->type == OError) + return predicate; + if (predicate->type == OBoolean && + predicate->d.b.v == false) + return eval(cadddr(o)); + return eval(caddr(o)); +} + +static struct object * evalok(struct object *o) { if (o->type == OIdentifier)