iris

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

commit 07c4f4c7c10d9fab9f86a93096ebea0ab29b8ec8
parent 6c977b947bfd7986d1f0daf541bcd40abc949b91
Author: sin <sin@2f30.org>
Date:   Thu, 15 May 2014 12:29:40 +0100

Implement set

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

diff --git a/parser.c b/parser.c @@ -62,6 +62,7 @@ struct object { static struct object *evaldefine(struct object *); static struct object *evalok(struct object *); static struct object *evalquote(struct object *); +static struct object *evalset(struct object *); struct { char *name; @@ -71,6 +72,7 @@ struct { { .name = "define", .fn = evaldefine }, { .name = "ok", .fn = evalok }, { .name = "quote", .fn = evalquote }, + { .name = "set", .fn = evalset }, }; static int @@ -341,6 +343,35 @@ evalquote(struct object *o) return NULL; } +static struct object * +evalset(struct object *o) +{ + struct object *ocar; + struct object *var, *val; + + if (o->type == OPair) { + ocar = car(o); + if (ocar->type == OIdentifier && + strcmp(ocar->d.i.s, "set") == 0) { + var = cadr(o); + if (var->type != OIdentifier) + return error("expected identifier"); + if (!lookupsym(var->d.i.s)) + return error("unbound identifier"); + if (!caddr(o)) + return error("expected sexpression"); + if (cadddr(o)) + return error("multiple arguments to set"); + val = eval(caddr(o)); + if (val->type == OError) + return val; + addsym(var->d.i.s, val); + return lookupsym("ok"); + } + } + return NULL; +} + struct object * eval(struct object *o) {