From b0feb6d8ff8a206662f9c4c7c983c904c0713c80 Mon Sep 17 00:00:00 2001 From: mike lowis Date: Fri, 4 Jun 2021 22:42:45 -0400 Subject: [PATCH] new symbol table functioning --- cerise/src/grammar.c | 43 ++++++++++++------------------------------- cerise/src/sym.c | 3 ++- cerise/tests/Module.m | 16 ++++++++++++++-- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index 4c3357a..50f5fec 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -19,28 +19,6 @@ static void name(Parser* p, Item* item) #endif -/* Default Symbol List - *****************************************************************************/ -static Symbol BoolSym = { - .class = SYM_TYPE, - .name = "Bool", - .type = &BoolType -}; - -static Symbol IntSym = { - .next = &BoolSym, - .class = SYM_TYPE, - .name = "Int", - .type = &IntType -}; - -static Symbol RealSym = { - .next = &IntSym, - .class = SYM_TYPE, - .name = "Real", - .type = &RealType -}; - /* Item Handling *****************************************************************************/ static void init_item(Item* item, Symbol* sym) @@ -473,7 +451,7 @@ RULE(var_decl) export = accept(p, '*'); sym = symbol_new(p, 0, name, SYM_VAR, export); first = (nsyms == 0 ? sym : first); - sym->global = (p->level <= 1 ? 1 : 0); +// sym->global = (p->level <= 1 ? 1 : 0); nsyms++; } while (accept(p, ',')); @@ -712,7 +690,7 @@ void compile(char* fname) char* fcontents = file_load(fname); Parser* p = &(Parser){ .name = NULL, - .scope = &RealSym, +// .scope = &RealSym, .file = &(LexFile){ .path = fname, .fbeg = fcontents, @@ -720,6 +698,12 @@ void compile(char* fname) }, .curr_reg = 0 }; + + symbol_new(p, 0, "Bool", SYM_TYPE, 0)->type = &BoolType; + symbol_new(p, 0, "Int", SYM_TYPE, 0)->type = &IntType; + symbol_new(p, 0, "Real", SYM_TYPE, 0)->type = &RealType; + symbol_new(p, 0, "String", SYM_TYPE, 0)->type = &StringType; + codegen_startmod(p); module(p, &(Item){0}); codegen_main(p); @@ -733,16 +717,13 @@ void compile(char* fname) Parser Ctx = {0}; -static Symbol InitialScope = { - .next = &RealSym, - .class = SYM_SCOPE, -}; - static void parse_init(char* fname, char* string) { memset(&Ctx, 0, sizeof(Ctx)); - Ctx.scope = &InitialScope; - InitialScope.desc = NULL; + symbol_new(&Ctx, 0, "Bool", SYM_TYPE, 0)->type = &BoolType; + symbol_new(&Ctx, 0, "Int", SYM_TYPE, 0)->type = &IntType; + symbol_new(&Ctx, 0, "Real", SYM_TYPE, 0)->type = &RealType; + symbol_new(&Ctx, 0, "String", SYM_TYPE, 0)->type = &StringType; LexFile* file = calloc(sizeof(LexFile), 1u); file->path = strdup(fname); file->fbeg = file->fpos = strdup(string); diff --git a/cerise/src/sym.c b/cerise/src/sym.c index 2f83d71..05c66f3 100644 --- a/cerise/src/sym.c +++ b/cerise/src/sym.c @@ -4,6 +4,7 @@ static int sym_matches(Parser* p, int class, char* name, Symbol* sym) { +// printf("%s == %s\n", name, sym->name); if (name && sym->name && !strcmp(sym->name, name)) { if (class >= 0 && (int)sym->class != class) @@ -82,7 +83,7 @@ Symbol* symbol_get(Parser* p, char* name, int class) Symbol* sym = NULL; size_t index = lookup(p, 0, name, class); - if (NIL_SYMBOL != index) + if (NIL_SYMBOL == index) { error(p, "unknown symbol '%s'", name); } diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index e23d994..162b7eb 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -19,6 +19,11 @@ type w,h : Int end end + TypeE = record + i : Int + a : array 5 of Int + end + TypeF = array 5 of TypeE var a : Bool @@ -28,6 +33,7 @@ var e : array 5 of array 10 of Int f : TypeD g : array 5 of Int + h : TypeF procedure Foo*(e : Int, z : Int, q1 : TypeD, q2 : array 5 of Int) : Int const FOO = 2 @@ -43,6 +49,9 @@ begin end begin + + h[1].i = 42; + # a = true; # a = A; # b = 24; @@ -117,7 +126,10 @@ begin # f.dim.h = 0; # f.label[0] = 42; - c = 4; - g[c] = 42; +# c = 4; +# g[c] = 42; # e[0][9] = 42; + + + end -- 2.49.0