iris

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

commit 17c7caca5822e8c7e266b4a1717109ca5989714d
parent 996fec60b1f3785382f4c87b707c0c76389add0b
Author: sin <sin@2f30.org>
Date:   Mon, 19 May 2014 15:01:14 +0100

Use a linked list for handling the environments

Diffstat:
Msym.c | 38++++++++++++++++++--------------------
Msym.h | 5++---
2 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/sym.c b/sym.c @@ -1,10 +1,10 @@ /* See LICENSE file for copyright and license details. */ #include <stddef.h> +#include <stdlib.h> #include <string.h> #include "sym.h" #include "util.h" -#define NRMAXENV 1024 #define NRMAXSYM 1024 struct symentry { @@ -17,7 +17,8 @@ struct env { struct symentry se[NRMAXSYM]; size_t sz; } symtab; -} env[NRMAXENV]; + struct env *next; +}; static struct env *topenv; @@ -50,7 +51,7 @@ lookupsym(char *s) struct symentry *se; size_t i; - for (e = topenv; e >= &env[0]; e--) { + for (e = topenv; e; e = e->next) { for (i = 0; i < e->symtab.sz; i++) { se = &e->symtab.se[i]; if (strcmp(se->s, s) == 0) @@ -64,31 +65,28 @@ int initenv(void) { if (!topenv) - topenv = &env[0]; + topenv = ecalloc(1, sizeof(*topenv)); return 0; } void -resetenv(void) -{ - topenv = &env[0]; -} - -int pushenv(void) { - if (topenv + 1 >= &env[NRMAXENV]) - return -1; - topenv++; - return 0; + struct env *e; + + e = ecalloc(1, sizeof(*e)); + e->next = topenv; + topenv = e; } -int +void popenv(void) { - if (topenv - 1 < &env[0]) - return -1; - memset(&topenv->symtab, 0, sizeof(topenv->symtab)); - topenv--; - return 0; + struct env *tmp; + + if (topenv->next) { + tmp = topenv->next; + free(topenv); + topenv = tmp; + } } diff --git a/sym.h b/sym.h @@ -4,6 +4,5 @@ struct object; struct object *addsym(char *, struct object *); struct object *lookupsym(char *); int initenv(void); -void resetenv(void); -int pushenv(void); -int popenv(void); +void pushenv(void); +void popenv(void);