iris

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

commit 8cef3fb9e1ef5a63979d9b322df0f596dfdcbc8d
parent fd3af754458188e6727981c9746ab96379225a67
Author: sin <sin@2f30.org>
Date:   Wed, 14 May 2014 17:17:58 +0100

Add environment handling

Diffstat:
Msym.c | 56++++++++++++++++++++++++++++++++++++++++++++------------
Msym.h | 3+++
2 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/sym.c b/sym.c @@ -9,13 +9,17 @@ struct symentry { struct object *o; }; +#define NRMAXENV 16 #define NRMAXSYM 1024 -/* we will switch to a hash table later */ -struct symtab { - struct symentry e[NRMAXSYM]; - size_t sz; -} symtab; +struct env { + struct symtab { + struct symentry se[NRMAXSYM]; + size_t sz; + } symtab; +} env[NRMAXENV]; + +static struct env *topenv; struct object * addsym(const char *s, struct object *o) @@ -26,25 +30,53 @@ addsym(const char *s, struct object *o) otmp = lookupsym(s); if (otmp) return otmp; - if (symtab.sz + 1 > LEN(symtab.e)) + if (topenv->symtab.sz + 1 > LEN(topenv->symtab.se)) return NULL; - se = &symtab.e[symtab.sz]; + se = &topenv->symtab.se[topenv->symtab.sz]; se->s = s; se->o = o; - symtab.sz++; + topenv->symtab.sz++; return o; } struct object * lookupsym(const char *s) { + struct env *e; struct symentry *se; size_t i; - for (i = 0; i < symtab.sz; i++) { - se = &symtab.e[i]; - if (strcmp(se->s, s) == 0) - return se->o; + for (e = topenv; e >= &env[0]; e--) { + for (i = 0; i < e->symtab.sz; i++) { + se = &e->symtab.se[i]; + if (strcmp(se->s, s) == 0) + return se->o; + } } return NULL; } + +int +initenv(void) +{ + topenv = &env[0]; + return 0; +} + +int +pushenv(void) +{ + if (topenv + 1 >= &env[NRMAXENV]) + return -1; + topenv++; + return 0; +} + +int +popenv(void) +{ + if (topenv - 1 < &env[0]) + return -1; + topenv--; + return 0; +} diff --git a/sym.h b/sym.h @@ -3,3 +3,6 @@ struct object; struct object *addsym(const char *, struct object *); struct object *lookupsym(const char *); +int initenv(); +int pushenv(); +int popenv();