iris

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

commit 25a0f102da79b5900757140865be17f248aec065
parent 004c8656b46949e7377106b86ff5ca853a1096b7
Author: sin <sin@2f30.org>
Date:   Fri, 16 May 2014 11:37:51 +0100

Add boolean proc and clean up a bit

Diffstat:
Mparser.c | 77+++++++++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 39 insertions(+), 38 deletions(-)

diff --git a/parser.c b/parser.c @@ -73,6 +73,7 @@ static struct object *evalquote(struct object *); static struct object *evalset(struct object *); /* primitive procedures */ +static struct object *evalboolean(struct object *); static struct object *evaldiff(struct object *); static struct object *evalplus(struct object *); @@ -93,8 +94,9 @@ struct { struct object *o; struct object *(*fn)(struct object *); } procs[] = { - { .name = "diff", .fn = evaldiff }, - { .name = "plus", .fn = evalplus }, + { .name = "boolean", .fn = evalboolean }, + { .name = "diff", .fn = evaldiff }, + { .name = "plus", .fn = evalplus }, }; static struct object * @@ -319,14 +321,12 @@ cdr(struct object *o) static struct object * evaldefine(struct object *o) { - struct object *ocar; struct object *var, *val; if (o->type != OPair) return NULL; - ocar = car(o); - if (ocar->type != OIdentifier || - strcmp(ocar->d.i.s, "define") != 0) + if (car(o)->type != OIdentifier || + strcmp(car(o)->d.i.s, "define") != 0) return NULL; var = cadr(o); if (var->type != OIdentifier) @@ -345,14 +345,12 @@ 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) + if (car(o)->type != OIdentifier || + strcmp(car(o)->d.i.s, "if") != 0) return NULL; predicate = eval(cadr(o)); if (predicate->type == OError) @@ -375,28 +373,22 @@ evalok(struct object *o) static struct object * evalquote(struct object *o) { - struct object *ocar; - - if (o->type == OPair) { - ocar = car(o); - if (ocar->type == OIdentifier && - strcmp(ocar->d.i.s, "quote") == 0) + if (o->type == OPair) + if (car(o)->type == OIdentifier && + strcmp(car(o)->d.i.s, "quote") == 0) return cadr(o); - } return NULL; } static struct object * evalset(struct object *o) { - struct object *ocar; struct object *var, *val; if (o->type != OPair) return NULL; - ocar = car(o); - if (ocar->type != OIdentifier || - strcmp(ocar->d.i.s, "set") != 0) + if (car(o)->type != OIdentifier || + strcmp(car(o)->d.i.s, "set") != 0) return NULL; var = cadr(o); if (var->type != OIdentifier) @@ -415,6 +407,24 @@ evalset(struct object *o) } static struct object * +evalboolean(struct object *o) +{ + struct object *arg; + + if (o->type != OPair) + return NULL; + if (car(o)->type != OIdentifier || + strcmp(car(o)->d.i.s, "boolean") != 0) + return NULL; + arg = eval(cadr(o)); + if (arg->type == OError) + return arg; + if (arg->type == OBoolean) + return boolean(true); + return boolean(false); +} + +static struct object * dodiff(struct object *o, struct object *n) { struct object *otmp; @@ -433,14 +443,12 @@ dodiff(struct object *o, struct object *n) static struct object * evaldiff(struct object *o) { - struct object *ocar; struct object *n; if (o->type != OPair) return NULL; - ocar = car(o); - if (ocar->type != OIdentifier || - strcmp(ocar->d.i.s, "diff") != 0) + if (car(o)->type != OIdentifier || + strcmp(car(o)->d.i.s, "diff") != 0) return NULL; n = eval(cadr(o)); if (n->type == OError) @@ -473,13 +481,10 @@ doplus(struct object *o, struct object *n) static struct object * evalplus(struct object *o) { - struct object *ocar; - if (o->type != OPair) return NULL; - ocar = car(o); - if (ocar->type != OIdentifier || - strcmp(ocar->d.i.s, "plus") != 0) + if (car(o)->type != OIdentifier || + strcmp(car(o)->d.i.s, "plus") != 0) return NULL; return doplus(cdr(o), number(0)); } @@ -531,22 +536,18 @@ eval(struct object *o) static void printpair(struct object *o) { - struct object *ocar, *ocdr; - - ocar = car(o); - ocdr = cdr(o); - print(ocar); - switch (ocdr->type) { + print(car(o)); + switch (cdr(o)->type) { case OPair: putchar(' '); - printpair(ocdr); + printpair(cdr(o)); case OEmptylist: return; default: putchar(' '); putchar('.'); putchar(' '); - print(ocdr); + print(cdr(o)); } }