iris

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

commit 5e7446c16a6a4e0cf40fdc4b6600c66f209347e4
parent 8554c3e6eb3f4f23edfaa1353d1b4297b93205a7
Author: sin <sin@2f30.org>
Date:   Wed May 14 11:40:29 +0100

Make functions uniform, pass around errors as objects

Diffstat:
parser.c | 82+++++++++++++++++++++++--------------------------------------------------------
1 file changed, 24 insertions(+), 58 deletions(-)
diff --git a/parser.c b/parser.c @@ -43,7 +43,7 @@ struct object { } n; /* character */ struct { - char v; + char c; } c; /* string */ struct { @@ -59,101 +59,68 @@ newobject(void) } static struct object * -error(struct tok *t) +error(char *s) { struct object *o; - char *l; - l = lexeme(t); - if (!l) - return NULL; o = newobject(); o->type = OError; - o->d.err.s = estrdup(l); - free(l); + o->d.err.s = estrdup(s); return o; } static struct object * -eof(struct tok *t) +eof(void) { struct object *o; - char *l; - l = lexeme(t); - if (!l) - return NULL; o = newobject(); o->type = OEof; - o->d.eof.s = estrdup(l); - free(l); + o->d.eof.s = "eof"; return o; } static struct object * -boolean(struct tok *t) +boolean(bool v) { struct object *o; - char *l; - l = lexeme(t); - if (!l) - return NULL; o = newobject(); o->type = OBoolean; - if (l[1] == 'f') - o->d.b.v = false; - if (l[1] == 't') - o->d.b.v = true; - free(l); + o->d.b.v = v; return o; } static struct object * -number(struct tok *t) +number(long v) { struct object *o; - char *l; - l = lexeme(t); - if (!l) - return NULL; o = newobject(); o->type = ONumber; - o->d.n.v = strtol(l, 0, 10); - free(l); + o->d.n.v = v; return o; } static struct object * -character(struct tok *t) +character(char c) { struct object *o; - char *l; - l = lexeme(t); - if (!l) - return NULL; o = newobject(); o->type = OCharacter; - o->d.c.v = l[2]; - free(l); + o->d.c.c = c; return o; } static struct object * -string(struct tok *t) +string(char *s) { struct object *o; - char *l; - l = lexeme(t); - if (!l) - return NULL; o = newobject(); o->type = OString; - o->d.s.s = estrdup(l); - free(l); + o->d.s.s = estrdup(s); return o; } @@ -161,25 +128,26 @@ struct object * sexpression(FILE *in) { struct tok t; + char *l; initenv(); t = gettok(in); + l = lexeme(&t); switch (t.type) { case TEof: - return eof(&t); + return eof(); case TError: - return error(&t); + return error(l); case TBoolean: - return boolean(&t); + return boolean(l[1] == 't' ? true : false); case TNumber: - return number(&t); + return number(strtol(l, 0, 10)); case TCharacter: - return character(&t); + return character(l[2]); case TString: - return string(&t); + return string(l); default: - fprintf(stderr, "unhandled token type: %d\n", t.type); - break; + return error("unhandled token"); } return NULL; } @@ -188,15 +156,13 @@ struct object * eval(struct object *o) { if (!o) - fprintf(stderr, "asked to eval a nil-object...wtf?"); + return error("asked to eval nil-object"); return o; } void print(struct object *o) { - if (!o) - return; switch (o->type) { case OError: printf("%s", o->d.err.s); @@ -211,7 +177,7 @@ print(struct object *o) printf("%ld", o->d.n.v); break; case OCharacter: - printf("#\\%c", o->d.c.v); + printf("#\\%c", o->d.c.c); break; case OString: printf("%s", o->d.s.s);