iris

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

commit 62ba241c35eec6a3c1734589070ca863023df2ff
parent f88435a9477e1761b31ef57de11179ece9ad9f07
Author: sin <sin@2f30.org>
Date:   Thu, 15 May 2014 10:36:03 +0100

Eval identifiers

Diffstat:
Mparser.c | 32++++++++++++++++++++++++++------
Msym.c | 6+++---
Msym.h | 4++--
3 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/parser.c b/parser.c @@ -59,8 +59,23 @@ struct object { } d; }; -/* builtin symbols */ -struct object *quotesym; +struct { + char *name; + struct object *o; +} builtins[] = { + { .name = "quote" }, +}; + +static int +builtin(char *name) +{ + size_t i; + + for (i = 0; i < LEN(builtins); i++) + if (strcmp(builtins[i].name, name) == 0) + return 1; + return 0; +} static struct object * newobject(void) @@ -219,7 +234,7 @@ sexpression(FILE *in) case TLparen: return pair(in); case TQuote: - return cons(quotesym, cons(sexpression(in), emptylist())); + return cons(lookupsym("quote"), cons(sexpression(in), emptylist())); default: return error("unhandled token"); } @@ -255,10 +270,11 @@ eval(struct object *o) case OString: return o; } - if (o->type == OIdentifier) { + if (o->type == OIdentifier && builtin(o->d.i.s) == 0) { otmp = lookupsym(o->d.i.s); if (!otmp) return error("unbound identifier"); + return otmp; } if (quoted(o)) return o->d.p.cdr->d.p.car; /* cadr */ @@ -323,8 +339,12 @@ print(struct object *o) int init(void) { + size_t i; + initenv(); - quotesym = identifier("quote"); - addsym("quote", quotesym); + for (i = 0; i < LEN(builtins); i++) { + builtins[i].o = identifier(builtins[i].name); + addsym(builtins[i].name, builtins[i].o); + } return 0; } diff --git a/sym.c b/sym.c @@ -8,7 +8,7 @@ #define NRMAXSYM 1024 struct symentry { - const char *s; + char *s; struct object *o; }; @@ -22,7 +22,7 @@ struct env { static struct env *topenv; struct object * -addsym(const char *s, struct object *o) +addsym(char *s, struct object *o) { struct object *otmp; struct symentry *se; @@ -40,7 +40,7 @@ addsym(const char *s, struct object *o) } struct object * -lookupsym(const char *s) +lookupsym(char *s) { struct env *e; struct symentry *se; diff --git a/sym.h b/sym.h @@ -1,8 +1,8 @@ /* See LICENSE file for copyright and license details. */ struct object; -struct object *addsym(const char *, struct object *); -struct object *lookupsym(const char *); +struct object *addsym(char *, struct object *); +struct object *lookupsym(char *); int initenv(void); void resetenv(void); int pushenv(void);