]> git.mdlowis.com Git - proto/obnc.git/commitdiff
Added logic for parsing args and adding them to symbol table
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 5 May 2021 16:35:35 +0000 (12:35 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 5 May 2021 16:35:35 +0000 (12:35 -0400)
cerise/backend/c99/codegen.c
cerise/backend/test/codegen.c
cerise/backend/x86_64/codegen.c
cerise/inc/cerise.h
cerise/src/grammar.c
cerise/src/lex.c
cerise/src/parse.c
cerise/src/sym.c
cerise/tests/Module.m

index 6f0918f7421d360fe9d7949690620dddb7b42759..08e6f44bcf311a01fd4fb7d1ff44910cdb6cfee4 100644 (file)
@@ -153,16 +153,16 @@ void codegen_main(Parser* p)
     printf("}\n");
 }
 
-void codegen_startproc(Parser* p, char* name, bool exported)
+void codegen_startproc(Parser* p, Symbol* proc)
 {
-    char* export = (exported ? "" : "static");
-    if (!name)
+    if (!proc)
     {
-        printf("\n%svoid %s(void) {\n", export, p->name);
+        printf("\nvoid %s(void) {\n", p->name);
     }
     else
     {
-        printf("\n%svoid %s_%s(void) {\n", export, p->name, name);
+        char* export = (proc->export ? "" : "static");
+        printf("\n%svoid %s_%s(void) {\n", export, p->name, proc->name);
     }
 }
 
index b25a13d8b744e057f3b313fe90d44b8b853d131a..47a2a62c395602f0966688f5ec2a365f27dae850 100644 (file)
@@ -61,9 +61,9 @@ void codegen_main(Parser* p)
     (void)p;
 }
 
-void codegen_startproc(Parser* p, char* name, bool exported)
+void codegen_startproc(Parser* p, Symbol* proc)
 {
-    (void)p, (void)name, (void)exported;
+    (void)p, (void)proc;
 }
 
 void codegen_endproc(Parser* p)
index b5752a7e56935d1909fdef487be7348783d39e23..4a4f0ee61fe68edc59f61cd543e985cb45bc1d3c 100644 (file)
@@ -236,14 +236,13 @@ void codegen_main(Parser* p)
     printf("    ret\n");
 }
 
-void codegen_startproc(Parser* p, char* name, bool exported)
+void codegen_startproc(Parser* p, Symbol* proc)
 {
-    (void)exported;
     printf("    .text\n");
-    if (name)
+    if (proc)
     {
-        printf("    .globl %s_%s\n", p->name, name);
-        printf("%s_%s:\n", p->name, name);
+        printf("    .globl %s_%s\n", p->name, proc->name);
+        printf("%s_%s:\n", p->name, proc->name);
     }
     else
     {
index 09f70127042d31eb2d2a6f3d90ca202fc182b02d..b8db0ac77f70ebb70c3b99ddb0abf8ce526d50d7 100644 (file)
@@ -175,6 +175,7 @@ int consume(Parser* p);
 
 // src/sym.c
 Symbol* symbol_new(Parser* p, char* name, int class, bool export);
+Symbol* symbol_addfield(Parser* p, Symbol* parent, char* name, int class, bool export);
 Symbol* symbol_get(Parser* p, int class, char* name);
 void symbol_openscope(Parser* p);
 void symbol_closescope(Parser* p);
@@ -208,7 +209,7 @@ void codegen_setstr(Item* item, char* val);
 void codegen_imports(Parser* p);
 void codegen_var(Parser* p, Symbol* sym);
 void codegen_main(Parser* p);
-void codegen_startproc(Parser* p, char* name, bool exported);
+void codegen_startproc(Parser* p, Symbol* func);
 void codegen_endproc(Parser* p);
 void codegen_unop(Parser* p, int op, Item* a);
 void codegen_binop(Parser* p, int op, Item* a, Item* b);
index 6cf40c53363e03df39eac25793b564686c826e24..3b23f08935a7ff74af5cb3660bdb02ad827d421e 100644 (file)
@@ -450,11 +450,9 @@ RULE(proc_decl)
     expect(p, PROCEDURE);
     char* name = expect_text(p, IDENT);
     bool export = accept(p, '*');
-    Symbol* sym = symbol_new(p, name, SYM_PROC, export);
-    Symbol** args = &(sym->desc);
-    symbol_openscope(p);
-    codegen_startproc(p, name, export);
-    (void)args;
+    Symbol* proc = symbol_new(p, name, SYM_PROC, export);
+//    Symbol** args = &(sym->desc);
+//    (void)args;
 
     /* construct the proc type */
     expect(p, '(');
@@ -463,17 +461,20 @@ RULE(proc_decl)
         char* name = expect_text(p, IDENT);
         expect(p, ':');
         type(p, item);
-        sym->nargs++;
-        // TODO: Added argument symbol here...
-        (void)name;
+        proc->nargs++;
+        Symbol* arg = symbol_addfield(p, proc, name, SYM_VAR, false);
+        arg->type = item->type;
     }
     expect(p, ')');
     if (accept(p, ':'))
     {
         type(p, item);
-        sym->type = item->type;
+        proc->type = item->type;
     }
 
+    codegen_startproc(p, proc);
+    symbol_openscope(p);
+
     /* parse the declarations */
     if (accept(p, CONST))
     {
@@ -507,11 +508,11 @@ RULE(proc_decl)
         expect(p, ';');
     }
     expect(p, END);
+
     symbol_closescope(p);
     codegen_endproc(p);
 }
 
-
 RULE(import_list)
 {
     (void)item;
@@ -575,7 +576,7 @@ RULE(module)
         proc_decl(p, item);
     }
 
-    codegen_startproc(p, NULL, true);
+    codegen_startproc(p, NULL);
     if (accept(p, BEGIN))
     {
         codegen_imports(p);
index 5c52bb9e47cba5dd6dbba71dc4653a04aab920c7..72e87d5567e64e9f2e483c448998d48a8dbe48f0 100644 (file)
@@ -346,6 +346,68 @@ void lexprintpos(Parser* p, FILE* file, Tok* tok)
     fprintf(file, "%s:%zu:%zu:", tok->file, line, col);
 }
 
+char* TokTypes[] = {
+    ['"'] = "\"",
+    ['='] = "=",
+    ['.'] = ".",
+    ['<'] = "<",
+    ['>'] = ">",
+    ['!'] = "!",
+    ['('] = "(",
+    [')'] = ")",
+    ['['] = "[",
+    [']'] = "]",
+    ['{'] = "{",
+    ['}'] = "}",
+    [','] = ",",
+    [':'] = ":",
+    [';'] = ";",
+    ['^'] = "^",
+    ['+'] = "+",
+    ['-'] = "-",
+    ['*'] = "*",
+    ['/'] = "/",
+    ['|'] = "|",
+    ['%'] = "%",
+    [EQ] = "==",
+    [NEQ] = "!=",
+    [LTEQ] = "<=",
+    [GTEQ] = ">=",
+    [DOTDOT] = "..",
+    [AND] = "and",
+    [ARRAY] = "array",
+    [BEGIN] = "begin",
+    [BY] = "by",
+    [CASE] = "case",
+    [CONST] = "const",
+    [DIV] = "div",
+    [DO] = "do",
+    [ELSE] = "else",
+    [ELSIF] = "elsif",
+    [END] = "end",
+    [FOR] = "for",
+    [IF] = "if",
+    [IMPORT] = "import",
+    [IS] = "is",
+    [MOD] = "mod",
+    [MODULE] = "module",
+    [NIL] = "nil",
+    [NOT] = "not",
+    [OF] = "of",
+    [OR] = "or",
+    [POINTER] = "pointer",
+    [PROCEDURE] = "procedure",
+    [RECORD] = "record",
+    [REPEAT] = "repeat",
+    [RETURN] = "return",
+    [THEN] = "then",
+    [TO] = "to",
+    [TYPE] = "type",
+    [UNTIL] = "until",
+    [VAR] = "var",
+    [WHILE] = "while",
+};
+
 #ifdef CERISE_TESTS
 #include "atf.h"
 
index 29ed6e1b92017debb53e3a6e02f070f4886d05d2..c4e5d0613df6b142fa55396aac84390d5ed30cf7 100644 (file)
@@ -1,5 +1,8 @@
 #include "cerise.h"
 
+/* for token mismatch errors */
+extern char* TokTypes[];
+
 Tok* peek(Parser* p)
 {
     if (NONE == p->tok.type)
@@ -49,7 +52,23 @@ bool accept(Parser* p, TokType type)
 void expect(Parser* p, TokType type)
 {
     if (!accept(p, type))
-        error(p, "Unexpected token");
+    {
+        char* expect = TokTypes[type];
+        char* recv = TokTypes[peek(p)->type];
+
+        if (expect && recv)
+        {
+            error(p, "expected '%s', recieved '%s'", expect, recv);
+        }
+        else if (expect)
+        {
+            error(p, "expected '%s'", expect);
+        }
+        else
+        {
+            error(p, "Unexpected token");
+        }
+    }
 }
 
 Tok* expect_val(Parser* p, TokType type)
index fde58ac127f8fa8f3997dbbb1431e8fd6a8659df..c127493e26fda6a0c7de516763b3567a65938707 100644 (file)
@@ -61,17 +61,47 @@ Symbol* symbol_new(Parser* p, char* name, int class, bool export)
     {
         p->scope->desc = sym;
     }
-//    p->scope->desc = sym;
-    assert(sym);
     return sym;
 }
 
+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* symbol_get(Parser* p, int class, char* name)
 {
     for (Symbol* scope = p->scope; scope; scope = scope->next)
     {
 //       printf("SCOPE(n: %p):\n", scope->next);
-
         if (sym_matches(p, class, name, scope))
         {
             return scope;
index 051012e705fff8e2f6542f6db2b62d4758e6d000..77d2f1a3252a509ef31fcf856455e215b81d0257 100644 (file)
@@ -18,13 +18,12 @@ type
 
 var
   a : Bool
-  z : Int
   b : Int
   c : Int
 
 procedure Foo*(e : Int) : Int
   const FOO = 2
-#  type foo : Int
+  type foo = Int
   var z : Int
 begin
   c = b * b;