commit 8e03ac1edfcf6737f23930a6a19527fcdc4b9dc0
parent 829722842f8d4a94de4c49d902cc6b8c9ba29584
Author: sin <sin@2f30.org>
Date: Wed, 14 May 2014 16:51:55 +0100
Add symtable handling
Diffstat:
M | Makefile | | | 2 | +- |
A | sym.c | | | 48 | ++++++++++++++++++++++++++++++++++++++++++++++++ |
A | sym.h | | | 5 | +++++ |
3 files changed, 54 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
@@ -1,6 +1,6 @@
BIN = iris
VER = 0.0
-SRC = debug.c lexer.c parser.c repl.c util.c
+SRC = debug.c lexer.c parser.c repl.c sym.c util.c
OBJ = ${SRC:.c=.o}
CC = cc
diff --git a/sym.c b/sym.c
@@ -0,0 +1,48 @@
+/* See LICENSE file for copyright and license details. */
+#include <stddef.h>
+#include <string.h>
+#include "sym.h"
+#include "util.h"
+
+struct symentry {
+ const char *s;
+ struct object *o;
+};
+
+#define NRMAXSYM 1024
+
+/* we will switch to a hash table later */
+struct symtab {
+ struct symentry e[NRMAXSYM];
+ size_t sz;
+} symtab;
+
+int
+addsym(const char *s, struct object *o)
+{
+ struct symentry *se;
+
+ if (lookupsym(s))
+ return 0;
+ if (symtab.sz + 1 > LEN(symtab.e))
+ return -1;
+ se = &symtab.e[symtab.sz];
+ se->s = s;
+ se->o = o;
+ symtab.sz++;
+ return 0;
+}
+
+struct object *
+lookupsym(const char *s)
+{
+ 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;
+ }
+ return NULL;
+}
diff --git a/sym.h b/sym.h
@@ -0,0 +1,5 @@
+/* See LICENSE file for copyright and license details. */
+struct object;
+
+int addsym(const char *, struct object *);
+struct object *lookupsym(const char *);