]> git.mdlowis.com Git - proto/obnc.git/commitdiff
started working on argument parsing
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 4 May 2021 19:29:01 +0000 (15:29 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 4 May 2021 19:29:01 +0000 (15:29 -0400)
cerise/backend/c99/codegen.c
cerise/backend/test/codegen.c
cerise/backend/x86_64/codegen.c
cerise/inc/cerise.h
cerise/oberon0/OSG.Mod
cerise/src/grammar.c
cerise/tests/Module.m

index 9ce8dc1dae081ce6b328a68d1f494dd66585e4c6..54a48738fba853570a30af37fa13f5686b94157a 100644 (file)
@@ -230,3 +230,38 @@ void codegen_endif(Parser* p, long elsifs, Item* item)
     }
     printf("}\n");
 }
+
+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 codegen_call(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+
+    printf("    const long _T%d = %s(", p->curr_reg, item->imm.s);
+    item->reg = p->curr_reg;
+    p->curr_reg++;
+    printf(");\n");
+}
+
+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%ld", (!firstarg ? ", " : ""), item->reg);
+//    }
+}
index f1cbc37233aa0d7d18fe600a7309384622df08c2..5de6cf1faaad39aae0c30ec72d9edcc412a1039d 100644 (file)
@@ -100,3 +100,18 @@ void codegen_endif(Parser* p, long elsifs, Item* item)
 {
     (void)p, (void)elsifs, (void)item;
 }
+
+void codegen_prepcall(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
+
+void codegen_call(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
+
+void codegen_setarg(Parser* p, Item* item, bool firstarg)
+{
+    (void)p, (void)item, (void)firstarg;
+}
index d2b44ff7d20e8007e1bbd44a875eec8518d500f2..c8b06d1c727d6ccae49fa0b54c46b5eb9187b4d0 100644 (file)
@@ -329,3 +329,18 @@ void codegen_endif(Parser* p, long elsifs, Item* item)
 {
     (void)p, (void)elsifs, (void)item;
 }
+
+void codegen_prepcall(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
+
+void codegen_call(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
+
+void codegen_setarg(Parser* p, Item* item, bool firstarg)
+{
+    (void)p, (void)item, (void)firstarg;
+}
index 1aae1fdbc9d9cc3f3ad3d6100fbe96be5c7e7da8..21a83e73dce02989d8abc39659c38df9054f0456 100644 (file)
@@ -98,6 +98,8 @@ typedef struct Symbol {
     } class;
     char* name;
     Type* type;
+    struct Symbol* desc;
+    long nargs;
     ImmValue imm;
     int export : 1;
     int global : 1;
@@ -212,6 +214,10 @@ 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_setarg(Parser* p, Item* item, bool firstarg);
+
 /*
 
 typedef struct AnfNode {
index bd64f266ded0978c2f03a7477e7943f606a6bfb6..f75fb1e5d0e6a759920e732b093c5d4e02ccf43e 100644 (file)
@@ -488,7 +488,8 @@ MODULE OSG; (* NW 19.12.94 / 20.10.07 / OSGX  9.5.2017*)
   END ValueParam;\r
 \r
   PROCEDURE OpenArrayParam*(VAR x: Item);\r
-  BEGIN loadAdr(x);\r
+  BEGIN\r
+    loadAdr(x);\r
     IF x.type.len >= 0 THEN\r
       Put1(Mov, RH, 0, x.type.len)\r
     ELSE\r
index 5ffb654378ed627f6ffa3143f7149f1fe836fe00..60765272eafffd6652147f957fb4df80fd135d0b 100644 (file)
@@ -61,31 +61,27 @@ static void init_item(Item* item, Symbol* sym)
  *****************************************************************************/
 static void expression(Parser* p, Item* item);
 
-RULE(param)
-{
-    (void)item;
-    Item arg = {0};
-    expression(p, &arg);
-    // if check_types()
-//    codegen_setarg(p, &arg);
-    // else check_array_type()
-}
-
 RULE(param_list)
 {
     (void)item;
     int nargs = 0;
-    Item arg = {0};
+    Item argdef = {0};
     if (!matches(p, ')'))
     {
-        // arg = <get from item>
-        param(p, &arg);
+        Item arg = {0};
+        expression(p, &arg);
+        // if check_types()
+        codegen_setarg(p, &arg, true);
+        // else check_array_type()
         nargs++;
+
         while (!matches(p, ')'))
         {
             expect(p, ',');
-            // arg = <get from item>
-            param(p, &arg);
+            expression(p, &arg);
+            // if check_types()
+            codegen_setarg(p, &arg, false);
+            // else check_array_type()
             nargs++;
         }
     }
@@ -191,8 +187,9 @@ RULE(factor)
             if (accept(p, '('))
             {
                 // TODO: check item is a procedure
+                codegen_prepcall(p, item);
                 param_list(p, item);
-                //codegen_call(p, item);
+                codegen_call(p, item);
                 expect(p, ')');
             }
             break;
@@ -303,6 +300,7 @@ RULE(type)
     }
     else if (accept(p, RECORD))
     {
+        expect(p, END);
     }
     else
     {
@@ -447,21 +445,26 @@ RULE(const_decl)
 RULE(proc_decl)
 {
     expect(p, PROCEDURE);
-    (void)expect_text(p, IDENT);
-    (void)accept(p, '*');
+    char* name = expect_text(p, IDENT);
+    bool export = accept(p, '*');
+    Symbol* sym = symbol_new(p, name, SYM_PROC, export);
+    Symbol** args = &(sym->desc);
 
     /* construct the proc type */
     expect(p, '(');
     while (!matches(p, ')'))
     {
-        (void)expect_text(p, IDENT);
+        char* name = expect_text(p, IDENT);
         expect(p, ':');
         type(p, item);
+        sym->nargs++;
+        // TODO: Added argument symbol here...
     }
     expect(p, ')');
     if (accept(p, ':'))
     {
         type(p, item);
+        sym->type = item->type;
     }
 
     /* parse the declarations */
@@ -487,7 +490,10 @@ RULE(proc_decl)
 
     /* parse the body of the procedure */
     expect(p, BEGIN);
-    statement_seq(p, item);
+    if (!matches(p, RETURN) && !matches(p, END))
+    {
+        statement_seq(p, item);
+    }
     if (accept(p, RETURN))
     {
         expression(p, item);
@@ -690,12 +696,33 @@ TEST_SUITE(Grammar)
         parse_rule(const_decl, 0, "FOO = 5, BAR = FOO - 3");
     }
 
-    TEST(Should module level variable declarations)
+    TEST(Should parse module level type declarations)
+    {
+        parse_rule(type_decl, 0, "a = Int");
+        parse_rule(type_decl, 0, "a = Real");
+        parse_rule(type_decl, 0, "a = Bool");
+        parse_rule(type_decl, 0, "a = array 42 of Int");
+        parse_rule(type_decl, 0, "a = array 42 of array 42 of Int");
+        parse_rule(type_decl, 0, "a = record end");
+    }
+
+    TEST(Should parse module level variable declarations)
     {
         parse_rule(var_decl, 0, "i : Int");
         parse_rule(var_decl, 0, "i : Real");
         parse_rule(var_decl, 0, "i : Bool");
         parse_rule(var_decl, 0, "x,y,z : Int");
     }
+
+    TEST(Should parse module level procedure declarations)
+    {
+        parse_rule(proc_decl, 0,
+            "procedure Foo*() : Int\n"
+            "begin\n"
+            "    return 1;\n"
+            "end"
+         );
+    }
+
 }
 #endif
index 9d8dbdf784d61a0f8945865e5976bb7d89ec1dc7..74a78350eda69e6146f956024e06b65690c12ca9 100644 (file)
@@ -14,6 +14,8 @@ type
   TypeC = array 5 of array 10 of Int
   TypeD = record
 
+  end
+
 var
   a : Bool
   b : Int
@@ -91,5 +93,5 @@ begin
 #    c = 3;
 #  end
 
-#    c = c(1,2,3);
+    c = c(1,a,3);
 end