iris

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

commit a30a423629f315cb7fbbe1b9c1f65ffaf253f506
parent 2a05ed0a3743603003afeb9f36fa5912bdb9c10d
Author: sin <sin@2f30.org>
Date:   Fri, 16 May 2014 10:40:33 +0100

Fix plus/diff

Diffstat:
Mparser.c | 70+++++++++++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 43 insertions(+), 27 deletions(-)

diff --git a/parser.c b/parser.c @@ -326,31 +326,6 @@ evaldefine(struct object *o) } static struct object * -dodiff(struct object *o, struct object *n) -{ - if (!car(o)) - return n; - if (car(o)->type != ONumber) - return error("expected number"); - n->d.n.v -= car(o)->d.n.v; - return dodiff(cdr(o), n); -} - -static struct object * -evaldiff(struct object *o) -{ - struct object *ocar; - - if (o->type != OPair) - return NULL; - ocar = car(o); - if (ocar->type != OIdentifier || - strcmp(ocar->d.i.s, "diff") != 0) - return NULL; - return dodiff(cdr(o), number(0)); -} - -static struct object * evalif(struct object *o) { struct object *ocar; @@ -381,13 +356,54 @@ evalok(struct object *o) } static struct object * +dodiff(struct object *o, struct object *n) +{ + struct object *otmp; + + if (!car(o)) + return n; + otmp = eval(car(o)); + if (otmp->type != ONumber) + return error("expected number"); + n->d.n.v -= otmp->d.n.v; + return dodiff(cdr(o), 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) + return NULL; + n = eval(cadr(o)); + if (n->type == OError) + return n; + if (n->type != ONumber) + return error("expected number"); + if (!caddr(o)) { + n->d.n.v *= -1; + return n; + } + return dodiff(cddr(o), n); +} + +static struct object * doplus(struct object *o, struct object *n) { + struct object *otmp; + if (!car(o)) return n; - if (car(o)->type != ONumber) + otmp = eval(car(o)); + if (otmp->type != ONumber) return error("expected number"); - n->d.n.v += car(o)->d.n.v; + n->d.n.v += otmp->d.n.v; return doplus(cdr(o), n); }