]> git.mdlowis.com Git - proto/obnc.git/commitdiff
functions and function calls fully working now
authorMichael D. Lowis <mike.lowis@gentex.com>
Thu, 6 May 2021 20:50:16 +0000 (16:50 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Thu, 6 May 2021 20:50:16 +0000 (16:50 -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/tests/Module.m

index 93e18079164e8bdd323a13e6f3a3e6f795e94f3a..6c1fd3d5a9727463a0bb0662fa9bf5775d90d3ec 100644 (file)
@@ -161,15 +161,19 @@ void codegen_startproc(Parser* p, Symbol* proc)
     }
     else
     {
-        char* export = (proc->export ? "" : "static");
+        char* export = (proc->export ? "" : "static ");
         printf("\n%s%s %s_%s(",
             export,
-            TypeNames[proc->type->form],
+            (proc->type ? TypeNames[proc->type->form] : "void"),
             p->name,
             proc->name);
         for (Symbol* arg = proc->desc; arg; arg = arg->next)
         {
             printf("%s %s", TypeNames[arg->type->form], arg->name);
+            if (arg->next)
+            {
+                printf(", ");
+            }
         }
         printf(") {\n");
     }
@@ -273,3 +277,12 @@ void codegen_setarg(Parser* p, Item* item, bool firstarg)
 {
     load_var(p, item);
 }
+
+void codegen_return(Parser* p, Item* item)
+{
+    if (item)
+    {
+        load_var(p, item);
+        printf("    return _T%d;\n", item->reg);
+    }
+}
index 23caad8f49e7f315e5d2917373e7b3f55137848f..ad2db8be3ce4e87415f4302928fcdac2e72cebd3 100644 (file)
@@ -115,3 +115,8 @@ void codegen_setarg(Parser* p, Item* item, bool firstarg)
 {
     (void)p, (void)item, (void)firstarg;
 }
+
+void codegen_return(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
index 7f1dc3aadd6be6481683290a14b55c713fc42c30..dccec0c4595bf419474f520ed074428285d1ea61 100644 (file)
@@ -343,3 +343,8 @@ void codegen_setarg(Parser* p, Item* item, bool firstarg)
 {
     (void)p, (void)item, (void)firstarg;
 }
+
+void codegen_return(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
index e38d4246236bb9ca4e2c6f110f43d8e213d10a6a..de6938f8f8b297c140a94e40b2082cbae97b8024 100644 (file)
@@ -222,6 +222,7 @@ void codegen_endif(Parser* p, long elsifs, Item* item);
 void codegen_prepcall(Parser* p, Item* item);
 void codegen_call(Parser* p, Item* item, Item* args);
 void codegen_setarg(Parser* p, Item* item, bool firstarg);
+void codegen_return(Parser* p, Item* item);
 
 /*
 
index a46e0fe7785fae65b353299e6f11e7ef50475bdb..da646dd378c6e388d034af53bc20f9d3443dcf04 100644 (file)
@@ -4,6 +4,31 @@
 #include <fcntl.h>
 #include <sys/stat.h>
 
+/* TODO
+    * Implement logical operators: and, or, not
+    * Implement arrays and array access
+    * Implement module scoped identifiers
+    * Implement strings types
+    * Implement records and record access
+    * Implement while, for, do/repeat loops
+    * Track uninitialized variables/globals
+    * Implement pointers
+        * Ownership semantics: Ownership you can count on
+        * Concurrency: Biased reference counting
+    * Nested procedure definitions
+    * Implement record extension
+    * Cleanup the lexer
+*/
+
+/* Order of Operations for Implementing Memory Management
+
+    * Implement new operator
+    * Enforce single owner pointers
+    * Implement borrowed pointers
+    * Implement biased refcounting for borrowed pointers
+
+*/
+
 //#define TRACE
 #ifdef TRACE
     static int Indent = 0;
@@ -61,36 +86,6 @@ 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++;
-////        }
-////    }
-//
-//    Symbol* args = item->sym;
-//
-//}
-
 RULE(qualident)
 {
     char* name = expect_text(p, IDENT);
@@ -101,17 +96,6 @@ RULE(qualident)
         item->imm.s = sym->name;
     }
 
-
-//    item->mode = sym->class;
-//    item->type = sym->type;
-//    item->imm  = sym->imm;
-
-//    if (sym->class == SYM_CONST)
-//    {
-//        item->mode = ITEM_CONST;
-//        item->imm = sym->imm;
-//    }
-
 //    if (accept(p, '.'))
 //    {
 //        expect(p, IDENT);
@@ -190,8 +174,6 @@ RULE(factor)
             designator(p, item);
             if (accept(p, '('))
             {
-//                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};
@@ -206,19 +188,36 @@ RULE(factor)
                     *currarg = argval;
                     currarg = &(argval->next);
                     args = args->next;
+                    if (args)
+                    {
+                        expect(p, ',');
+                    }
                 }
+
                 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);
+                    bool comma = accept(p, ',');
+                    if (comma && matches(p, ')'))
+                    {
+                        error(p, "trailing comma in argument list");
+                    }
+                    else
+                    {
+                        error(p, "too many arguments to function '%s'", proc->name);
+                    }
                 }
                 codegen_call(p, item, arglist.next);
                 expect(p, ')');
             }
             break;
+
+        default:
+            printf("unknown factor: %d\n", peek(p)->type);
+            break;
     }
 }
 
@@ -518,10 +517,10 @@ RULE(proc_decl)
         var_decl(p, item);
     }
 
-    while (matches(p, PROCEDURE))
-    {
-        proc_decl(p, item);
-    }
+//    while (matches(p, PROCEDURE))
+//    {
+//        proc_decl(p, item);
+//    }
 
     /* parse the body of the procedure */
     expect(p, BEGIN);
@@ -532,8 +531,13 @@ RULE(proc_decl)
     if (accept(p, RETURN))
     {
         expression(p, item);
+        codegen_return(p, item);
         expect(p, ';');
     }
+    else
+    {
+        codegen_return(p, NULL);
+    }
     expect(p, END);
 
     symbol_closescope(p);
index 115f3c31956891408e3577ae3a02e8b3d94ceed9..fc5d28efcd1dee62eb70455ad46106f355788808 100644 (file)
@@ -20,22 +20,28 @@ var
   a : Bool
   b : Int
   c : Int
+  d : Real
 
 procedure Foo*(e : Int, z : Int) : Int
   const FOO = 2
   type foo = Int
   var z : Int
+
+#  procedure Bar() : Int
+#  begin
+#  end
+
 begin
-  c = b * b;
+#  c = b * b;
   return c;
 end
 
 begin
-  a = true;
-  a = A;
-  b = 24;
-  b = B;
-
+#  a = true;
+#  a = A;
+#  b = 24;
+#  b = B;
+#
 #  # Unary ops
 #  b = +b;
 #  b = -b;
@@ -65,7 +71,7 @@ begin
 #  # Complex arithmetic
 #  c = b + b * b + b;
 #
-
+#
 #  # If statements
 #  if 1 == 1 then
 #    c = 1;
@@ -92,6 +98,7 @@ begin
 #  else
 #    c = 3;
 #  end
-
-    c = Foo(1,2);
+#
+  # Function calls
+  c = Foo(1,2);
 end