iris

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

commit 21a40ab0d84135e5e763e99205cb0a5a39984c40
parent 6b79afc84cc8ec8867b173d9036ee25bf666cf23
Author: sin <sin@2f30.org>
Date:   Thu May 15 11:34:23 +0100

Parse define

Diffstat:
parser.c | 89++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 82 insertions(+), 7 deletions(-)
diff --git a/parser.c b/parser.c @@ -59,6 +59,8 @@ struct object { } d; }; +static struct object *evaldefine(struct object *); +static struct object *evalok(struct object *); static struct object *evalquote(struct object *); struct { @@ -66,7 +68,9 @@ struct { struct object *o; struct object *(*fn)(struct object *); } builtins[] = { - { .name = "quote", .fn = evalquote }, + { .name = "define", .fn = evaldefine }, + { .name = "ok", .fn = evalok }, + { .name = "quote", .fn = evalquote }, }; static int @@ -246,16 +250,87 @@ sexpression(FILE *in) return NULL; } -struct object * +static struct object * +car(struct object *o) +{ + return o->d.p.car; +} + +static struct object * +cdr(struct object *o) +{ + return o->d.p.cdr; +} + +#define caar(obj) car(car(obj)) +#define cadr(obj) car(cdr(obj)) +#define cdar(obj) cdr(car(obj)) +#define cddr(obj) cdr(cdr(obj)) +#define caaar(obj) car(car(car(obj))) +#define caadr(obj) car(car(cdr(obj))) +#define cadar(obj) car(cdr(car(obj))) +#define caddr(obj) car(cdr(cdr(obj))) +#define cdaar(obj) cdr(car(car(obj))) +#define cdadr(obj) cdr(car(cdr(obj))) +#define cddar(obj) cdr(cdr(car(obj))) +#define cdddr(obj) cdr(cdr(cdr(obj))) +#define caaaar(obj) car(car(car(car(obj)))) +#define caaadr(obj) car(car(car(cdr(obj)))) +#define caadar(obj) car(car(cdr(car(obj)))) +#define caaddr(obj) car(car(cdr(cdr(obj)))) +#define cadaar(obj) car(cdr(car(car(obj)))) +#define cadadr(obj) car(cdr(car(cdr(obj)))) +#define caddar(obj) car(cdr(cdr(car(obj)))) +#define cadddr(obj) car(cdr(cdr(cdr(obj)))) +#define cdaaar(obj) cdr(car(car(car(obj)))) +#define cdaadr(obj) cdr(car(car(cdr(obj)))) +#define cdadar(obj) cdr(car(cdr(car(obj)))) +#define cdaddr(obj) cdr(car(cdr(cdr(obj)))) +#define cddaar(obj) cdr(cdr(car(car(obj)))) +#define cddadr(obj) cdr(cdr(car(cdr(obj)))) +#define cdddar(obj) cdr(cdr(cdr(car(obj)))) +#define cddddr(obj) cdr(cdr(cdr(cdr(obj)))) + +static struct object * +evaldefine(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, "define") == 0) { + var = car(cdr(o)); + val = eval(car(cdr(cdr(o)))); + if (var->type != OIdentifier) + return error("expected an identifier"); + addsym(var->d.i.s, val); + return lookupsym("ok"); + } + } + return NULL; +} + +static struct object * +evalok(struct object *o) +{ + if (o->type == OIdentifier) + if (strcmp(o->d.i.s, "ok") == 0) + return o; + return NULL; +} + +static struct object * evalquote(struct object *o) { - struct object *car; + struct object *ocar; if (o->type == OPair) { - car = o->d.p.car; - if (car->type == OIdentifier && - strcmp(car->d.i.s, "quote") == 0) - return o->d.p.cdr->d.p.car; + ocar = car(o); + if (ocar->type == OIdentifier && + strcmp(ocar->d.i.s, "quote") == 0) + return cadr(o); } return NULL; }