From: mike lowis Date: Mon, 7 Jun 2021 02:46:31 +0000 (-0400) Subject: reworked function definitions and scoping with new symbol table X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=2610ed129b8bfa758389c342666170f717d8b417;p=proto%2Fobnc.git reworked function definitions and scoping with new symbol table --- diff --git a/cerise/backend/c99/codegen.c b/cerise/backend/c99/codegen.c index ebffc48..d647f9d 100644 --- a/cerise/backend/c99/codegen.c +++ b/cerise/backend/c99/codegen.c @@ -300,12 +300,14 @@ void codegen_startproc(Parser* p, Symbol* proc) char* export = (proc->export ? "" : "static "); printf("\n%s%s %s_%s(", export, - (proc->type ? TypeNames[proc->type->form] : "void"), + (proc->type ? TypeNames[proc->type->base->form] : "void"), p->name, proc->name); - for (Symbol* arg = proc->desc; arg; arg = arg->next) + + for (Field* arg = proc->type->fields; arg; arg = arg->next) { - make_var(p, arg, false); + Symbol sym = { .type = arg->type, .name = arg->name }; + make_var(p, &sym, false); if (arg->next) { printf(", "); diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index ebf6fe7..4e49ea8 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -90,7 +90,7 @@ typedef struct Field { typedef struct Type { enum { FORM_BOOL, FORM_INT, FORM_REAL, FORM_ARRAY, FORM_STRING, - FORM_RECORD, + FORM_RECORD, FORM_PROC, FORM_COUNT } form; struct Field* fields; @@ -170,7 +170,6 @@ int consume(Parser* p); // src/sym.c Symbol* symbol_new(Parser* p, size_t scope, char* name, int class, bool export); -Symbol* symbol_addfield(Parser* p, Symbol* parent, char* name, int class, bool export); Symbol* symbol_get(Parser* p, char* name, int class); size_t symbol_openscope(Parser* p); void symbol_closescope(Parser* p, size_t scope); diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index 50f5fec..181fe65 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -515,6 +515,10 @@ RULE(proc_decl) char* name = expect_text(p, IDENT); bool export = accept(p, '*'); Symbol* proc = symbol_new(p, 0, name, SYM_PROC, export); + Type* proctype = calloc(1, sizeof(Type)); + proctype->form = FORM_PROC; + proc->type = proctype; + size_t scope = symbol_openscope(p); /* construct the proc type */ expect(p, '('); @@ -524,8 +528,8 @@ RULE(proc_decl) expect(p, ':'); type(p, item); proc->nargs++; - Symbol* arg = symbol_addfield(p, proc, name, SYM_VAR, false); - arg->type = item->type; + add_field(p, proctype, name, false)->type = item->type; + symbol_new(p, scope, name, SYM_VAR, export)->type = item->type; if (!matches(p, ')')) { expect(p, ','); @@ -535,11 +539,10 @@ RULE(proc_decl) if (accept(p, ':')) { type(p, item); - proc->type = item->type; + proctype->base = item->type; } codegen_startproc(p, proc); - size_t scope = symbol_openscope(p); /* parse the declarations */ if (accept(p, CONST)) @@ -557,12 +560,6 @@ RULE(proc_decl) var_decl(p, item); } -// /* TODO: Support nested procedures */ -// while (matches(p, PROCEDURE)) -// { -// proc_decl(p, item); -// } - /* parse the body of the procedure */ expect(p, BEGIN); if (!matches(p, RETURN) && !matches(p, END)) diff --git a/cerise/src/sym.c b/cerise/src/sym.c index 05c66f3..4b4d87e 100644 --- a/cerise/src/sym.c +++ b/cerise/src/sym.c @@ -4,7 +4,7 @@ static int sym_matches(Parser* p, int class, char* name, Symbol* sym) { -// printf("%s == %s\n", name, sym->name); +// printf("[%s] == %s\n", name, sym->name); if (name && sym->name && !strcmp(sym->name, name)) { if (class >= 0 && (int)sym->class != class) @@ -68,6 +68,7 @@ Symbol* symbol_new(Parser* p, size_t scope, char* name, int class, bool export) { error(p, "symbol '%s' is multiply defined in the current scope", name); } +// puts(""); /* insert */ p->syms[p->nsyms].name = name; @@ -105,38 +106,6 @@ void symbol_closescope(Parser* p, size_t scope) p->nsyms = scope; } -Symbol* symbol_addfield(Parser* p, Symbol* parent, char* name, int class, bool export) -{ - Symbol* prev = NULL; - Symbol* curr = parent->desc; - -// printf("\nsymbol_addfield(%s)\n", name); - while (curr) - { -// printf(" %s <- %s\n", curr->name, name); - if (curr->name && !strcmp(curr->name, name)) - { - error(p, "multiple definitions of '%s' in scope", name); - } - prev = curr; - curr = curr->next; - } - - Symbol* sym = calloc(1, sizeof(Symbol)); - sym->name = name; - sym->class = class; - sym->export = export; - if (prev) - { - prev->next = sym; - } - else - { - parent->desc = sym; - } - return sym; -} - /* Symbol Table Unit Tests *****************************************************************************/ #ifdef CERISE_TESTS diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 162b7eb..d9ddecd 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -39,13 +39,13 @@ procedure Foo*(e : Int, z : Int, q1 : TypeD, q2 : array 5 of Int) : Int const FOO = 2 type foo = Int var - z : Int + z1 : Int q : array 5 of array 10 of Int begin # e = q; c = 1; - z = 2; - return z; + z1 = 2; + return z1; end begin