]> git.mdlowis.com Git - proto/obnc.git/commitdiff
function calls are almost working....
authormike lowis <mike@mdlowis.com>
Thu, 6 May 2021 03:52:24 +0000 (23:52 -0400)
committermike lowis <mike@mdlowis.com>
Thu, 6 May 2021 03:52:24 +0000 (23:52 -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/sym.c
cerise/src/type_checks.c
cerise/tests/Module.m

index 08e6f44bcf311a01fd4fb7d1ff44910cdb6cfee4..93e18079164e8bdd323a13e6f3a3e6f795e94f3a 100644 (file)
@@ -162,7 +162,16 @@ void codegen_startproc(Parser* p, Symbol* proc)
     else
     {
         char* export = (proc->export ? "" : "static");
-        printf("\n%svoid %s_%s(void) {\n", export, p->name, proc->name);
+        printf("\n%s%s %s_%s(",
+            export,
+            TypeNames[proc->type->form],
+            p->name,
+            proc->name);
+        for (Symbol* arg = proc->desc; arg; arg = arg->next)
+        {
+            printf("%s %s", TypeNames[arg->type->form], arg->name);
+        }
+        printf(") {\n");
     }
 }
 
@@ -240,20 +249,21 @@ void codegen_endif(Parser* p, long elsifs, Item* item)
 
 void codegen_prepcall(Parser* p, Item* item)
 {
-//            printf("    const %s _T%d = %lld;\n",
-//                TypeNames[item->type->form], p->curr_reg, item->imm.i);
-//            item->reg = p->curr_reg;
-//            p->curr_reg++;
-    printf("    const long _T%d = %s(", p->curr_reg, item->imm.s);
-    item->reg = p->curr_reg;
-    p->curr_reg++;
+    (void)p, (void)item;
 }
 
-void codegen_call(Parser* p, Item* item)
+void codegen_call(Parser* p, Item* item, Item* args)
 {
-    (void)p, (void)item;
-
     printf("    const long _T%d = %s(", p->curr_reg, item->imm.s);
+    while (args)
+    {
+        printf("_T%d", args->reg);
+        if (args->next)
+        {
+            printf(", ");
+        }
+        args = args->next;
+    }
     item->reg = p->curr_reg;
     p->curr_reg++;
     printf(");\n");
@@ -262,13 +272,4 @@ void codegen_call(Parser* p, Item* item)
 void codegen_setarg(Parser* p, Item* item, bool firstarg)
 {
     load_var(p, item);
-    (void)p, (void)item;
-    if (item->mode == ITEM_CONST)
-    {
-        printf("%s%lld", (!firstarg ? ", " : ""), item->imm.i);
-    }
-    else
-    {
-        printf("%s_T%d", (!firstarg ? ", " : ""), item->reg);
-    }
 }
index 47a2a62c395602f0966688f5ec2a365f27dae850..23caad8f49e7f315e5d2917373e7b3f55137848f 100644 (file)
@@ -106,9 +106,9 @@ void codegen_prepcall(Parser* p, Item* item)
     (void)p, (void)item;
 }
 
-void codegen_call(Parser* p, Item* item)
+void codegen_call(Parser* p, Item* item, Item* args)
 {
-    (void)p, (void)item;
+    (void)p, (void)item, (void)args;
 }
 
 void codegen_setarg(Parser* p, Item* item, bool firstarg)
index 4a4f0ee61fe68edc59f61cd543e985cb45bc1d3c..7f1dc3aadd6be6481683290a14b55c713fc42c30 100644 (file)
@@ -334,9 +334,9 @@ void codegen_prepcall(Parser* p, Item* item)
     (void)p, (void)item;
 }
 
-void codegen_call(Parser* p, Item* item)
+void codegen_call(Parser* p, Item* item, Item* args)
 {
-    (void)p, (void)item;
+    (void)p, (void)item, (void)args;
 }
 
 void codegen_setarg(Parser* p, Item* item, bool firstarg)
index b8db0ac77f70ebb70c3b99ddb0abf8ce526d50d7..e38d4246236bb9ca4e2c6f110f43d8e213d10a6a 100644 (file)
@@ -112,10 +112,12 @@ typedef struct Module {
     char* alias;
 } Module;
 
-typedef struct {
+typedef struct Item {
+    struct Item* next;
     enum {
         ITEM_CONST, ITEM_VAR, ITEM_MVAR, ITEM_REG
     } mode;
+    Symbol* sym;
     Type* type;
     int reg;
     ImmValue imm;
@@ -217,9 +219,8 @@ void codegen_store(Parser* p, Item* a, Item* b);
 void codegen_if(Parser* p, Item* item);
 void codegen_else(Parser* p, Item* item);
 void codegen_endif(Parser* p, long elsifs, Item* item);
-
 void codegen_prepcall(Parser* p, Item* item);
-void codegen_call(Parser* p, Item* item);
+void codegen_call(Parser* p, Item* item, Item* args);
 void codegen_setarg(Parser* p, Item* item, bool firstarg);
 
 /*
index 37f724b3daca4d87167ba3dcacd688b8287c8edc..a46e0fe7785fae65b353299e6f11e7ef50475bdb 100644 (file)
@@ -61,39 +61,42 @@ static void init_item(Item* item, Symbol* sym)
  *****************************************************************************/
 static void expression(Parser* p, Item* item);
 
-RULE(param_list)
-{
-    (void)item;
-    int nargs = 0;
-    Item argdef = {0};
-    (void)argdef;
-    if (!matches(p, ')'))
-    {
-        Item arg = {0};
-        expression(p, &arg);
-        // if check_types()
-        codegen_setarg(p, &arg, true);
-        // else check_array_type()
-        nargs++;
-
-        while (!matches(p, ')'))
-        {
-            expect(p, ',');
-            expression(p, &arg);
-            // if check_types()
-            codegen_setarg(p, &arg, false);
-            // else check_array_type()
-            nargs++;
-        }
-    }
-}
+//RULE(param_list)
+//{
+////    (void)item;
+////    int nargs = 0;
+////    Item argdef = {0};
+////    (void)argdef;
+////    if (!matches(p, ')'))
+////    {
+////        Item arg = {0};
+////        expression(p, &arg);
+////        // if check_types()
+////        codegen_setarg(p, &arg, true);
+////        // else check_array_type()
+////        nargs++;
+////
+////        while (!matches(p, ')'))
+////        {
+////            expect(p, ',');
+////            expression(p, &arg);
+////            // if check_types()
+////            codegen_setarg(p, &arg, false);
+////            // else check_array_type()
+////            nargs++;
+////        }
+////    }
+//
+//    Symbol* args = item->sym;
+//
+//}
 
 RULE(qualident)
 {
     char* name = expect_text(p, IDENT);
     Symbol* sym = symbol_get(p, -1, name);
     init_item(item, sym);
-    if (sym->class == SYM_VAR)
+    if (sym->class == SYM_VAR || sym->class == SYM_PROC)
     {
         item->imm.s = sym->name;
     }
@@ -187,10 +190,32 @@ RULE(factor)
             designator(p, item);
             if (accept(p, '('))
             {
-                // TODO: check item is a procedure
-                codegen_prepcall(p, item);
-                param_list(p, item);
-                codegen_call(p, item);
+//                codegen_prepcall(p, item);
+//                param_list(p, item);
+                Symbol* proc = symbol_get(p, SYM_PROC, item->imm.s);
+                Symbol* args = proc->desc;
+                Item arglist = {0};
+                Item** currarg = &(arglist.next);
+                while (args && !matches(p, ')'))
+                {
+                    Item argdef = { .type = args->type };
+                    Item* argval = calloc(1, sizeof(Item));
+                    expression(p, argval);
+                    check_types(p, &argdef, argval);
+                    codegen_setarg(p, argval, (arglist.next == 0));
+                    *currarg = argval;
+                    currarg = &(argval->next);
+                    args = args->next;
+                }
+                if (args)
+                {
+                    error(p, "too few arguments to function '%s'", proc->name);
+                }
+                else if (!matches(p, ')'))
+                {
+                    error(p, "too many arguments to function '%s'", proc->name);
+                }
+                codegen_call(p, item, arglist.next);
                 expect(p, ')');
             }
             break;
@@ -462,6 +487,10 @@ RULE(proc_decl)
         proc->nargs++;
         Symbol* arg = symbol_addfield(p, proc, name, SYM_VAR, false);
         arg->type = item->type;
+        if (!matches(p, ')'))
+        {
+            expect(p, ',');
+        }
     }
     expect(p, ')');
     if (accept(p, ':'))
index c127493e26fda6a0c7de516763b3567a65938707..6f1e15cf6a4d69429aa61032c5183577bc8ed1e0 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);
 
-    if (sym->name && !strcmp(sym->name, name))
+    if (name && sym->name && !strcmp(sym->name, name))
     {
         if (class >= 0 && (int)sym->class != class)
         {
@@ -22,6 +22,10 @@ static int sym_matches(Parser* p, int class, char* name, Symbol* sym)
                     error(p, "'%s' is not a type", name);
                     break;
 
+                case SYM_PROC:
+                    error(p, "'%s' is not a procedure", name);
+                    break;
+
                 default:
                     assert(!"unknown identifier type");
                     break;
@@ -96,7 +100,6 @@ Symbol* symbol_addfield(Parser* p, Symbol* parent, char* name, int class, bool e
     return sym;
 }
 
-
 Symbol* symbol_get(Parser* p, int class, char* name)
 {
     for (Symbol* scope = p->scope; scope; scope = scope->next)
@@ -140,7 +143,4 @@ void symbol_closescope(Parser* p)
  *****************************************************************************/
 #ifdef CERISE_TESTS
 #include "atf.h"
-
-
-
 #endif
\ No newline at end of file
index 7301781ffc7202357263b49a343a033d095db11e..a50130efd5c411cbb62317f49608a707e4061364 100644 (file)
@@ -68,6 +68,7 @@ void check_bools(Parser* p, Item* a, Item* b)
 
 void check_types(Parser* p, Item* a, Item* b)
 {
+//    printf("%d %d\n", a->type->form, b->type->form);
     if (a->type->form == FORM_REAL)
     {
         check_reals(p, a, b);
index 77d2f1a3252a509ef31fcf856455e215b81d0257..115f3c31956891408e3577ae3a02e8b3d94ceed9 100644 (file)
@@ -21,7 +21,7 @@ var
   b : Int
   c : Int
 
-procedure Foo*(e : Int) : Int
+procedure Foo*(e : Int, z : Int) : Int
   const FOO = 2
   type foo = Int
   var z : Int
@@ -93,5 +93,5 @@ begin
 #    c = 3;
 #  end
 
-    c = c(1,a,3);
+    c = Foo(1,2);
 end