]> git.mdlowis.com Git - proto/obnc.git/commitdiff
reworked function definitions and scoping with new symbol table
authormike lowis <mike@mdlowis.com>
Mon, 7 Jun 2021 02:46:31 +0000 (22:46 -0400)
committermike lowis <mike@mdlowis.com>
Mon, 7 Jun 2021 02:46:31 +0000 (22:46 -0400)
cerise/backend/c99/codegen.c
cerise/inc/cerise.h
cerise/src/grammar.c
cerise/src/sym.c
cerise/tests/Module.m

index ebffc4841e541b35b19d0423cc22bce605611c65..d647f9da93df6a45caf9ed6c4556aff0364261c2 100644 (file)
@@ -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(", ");
index ebf6fe7c6d96ec73f300790c80ec172deae66746..4e49ea83f1313d66f4c54802f6173459f8131efc 100644 (file)
@@ -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);
index 50f5fecdf6b94cfd4e176b9e50c4950ef4126801..181fe6540cea500099e19f87f0780f56d2440a6a 100644 (file)
@@ -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))
index 05c66f380dbf16074cb24d6b1b003da4f2ad2259..4b4d87ee6605a6c20909bb032162653f6e019da3 100644 (file)
@@ -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
index 162b7ebafdeb17310702e7031e0fe0f4d47c6e4f..d9ddecd8b4b4075ac08169880603a4c38c0c91b6 100644 (file)
@@ -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