commit 6f472dca9eddb30dc8fbb108200442242dbba022
parent eaa2a603032f55ad4d61b3e31df8de7534f6ddbb
Author: sin <sin@2f30.org>
Date: Thu, 15 May 2014 16:02:19 +0100
Add plus builtin
Diffstat:
M | parser.c | | | 27 | +++++++++++++++++++++++++++ |
1 file changed, 27 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 *evalif(struct object *);
static struct object *evalok(struct object *);
+static struct object *evalplus(struct object *);
static struct object *evalquote(struct object *);
static struct object *evalset(struct object *);
@@ -73,6 +74,7 @@ struct {
{ .name = "define", .fn = evaldefine },
{ .name = "if", .fn = evalif },
{ .name = "ok", .fn = evalok },
+ { .name = "plus", .fn = evalplus },
{ .name = "quote", .fn = evalquote },
{ .name = "set", .fn = evalset },
};
@@ -352,6 +354,31 @@ evalok(struct object *o)
}
static struct object *
+doplus(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 doplus(cdr(o), 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)
+ return NULL;
+ return doplus(cdr(o), number(0));
+}
+
+static struct object *
evalquote(struct object *o)
{
struct object *ocar;