]> git.mdlowis.com Git - proto/obnc.git/commitdiff
implemented code generation for if statements. Only the C backend for now
authorMichael D. Lowis <mike.lowis@gentex.com>
Fri, 30 Apr 2021 13:13:54 +0000 (09:13 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Fri, 30 Apr 2021 13:13:54 +0000 (09:13 -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 bb14f9a69bc2abc01336bdea3f475e93c97d7d14..2df1a3d83059ca2c4046751d97a88526ec5323a6 100644 (file)
@@ -206,3 +206,26 @@ void codegen_store(Parser* p, Item* a, Item* b)
         assert(!"bad store op");
     }
 }
+
+void codegen_if(Parser* p, Item* item)
+{
+    load_var(p, item);
+    printf("    if (_T%d) {\n", item->reg);
+}
+
+void codegen_else(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+    printf("    } else {\n");
+}
+
+void codegen_endif(Parser* p, long elsifs, Item* item)
+{
+    (void)p, (void)item;
+    printf("    ");
+    for (long i = 0; i < elsifs; i ++)
+    {
+        printf("}");
+    }
+    printf("}\n");
+}
index fa252c69998e946c3b0887f0c36bab5e15ccd211..2826e8db66a99c72610a17580099c6b6949fd81d 100644 (file)
@@ -85,3 +85,18 @@ void codegen_store(Parser* p, Item* a, Item* b)
 {
     (void)p, (void)a, (void)b;
 }
+
+void codegen_if(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
+
+void codegen_else(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
+
+void codegen_endif(Parser* p, long elsifs, Item* item)
+{
+    (void)p, (void)elsifs, (void)item;
+}
index 4f8602c6ae8aa8d154c06d8a8859b139cb26b951..904ca9d327349d969be8b7b95fa9412c2233211d 100644 (file)
@@ -314,3 +314,18 @@ void codegen_store(Parser* p, Item* a, Item* b)
 //        assert(!"bad store op");
     }
 }
+
+void codegen_if(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
+
+void codegen_else(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
+
+void codegen_endif(Parser* p, long elsifs, Item* item)
+{
+    (void)p, (void)elsifs, (void)item;
+}
index ec85ec6abc1042f7b0227db6381d2f57dd322bb9..f416a538e2b9a0299384eafb560369dda2e60148 100644 (file)
@@ -208,3 +208,7 @@ 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);
 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);
+
index 51b154784492ca83644f09d070070071cde78146..15e3136096b9a72d186b0457153fa240f30286c3 100644 (file)
@@ -383,30 +383,26 @@ RULE(statement_seq)
             expect(p, IF);
             expression(p, item);
             check_bool(p, item);
-            // CFJump(item)
+            codegen_if(p, item);
             expect(p, THEN);
             statement_seq(p, &(Item){0});
-            // L = 0
+            int elsifs = 0;
             while (accept(p, ELSIF))
             {
-                // FJump(L)
-                // FixLink(item->imm.i)
+                elsifs++;
+                codegen_else(p, item);
                 expression(p, item);
                 check_bool(p, item);
-                // CFJump(item)
+                codegen_if(p, item);
                 expect(p, THEN);
                 statement_seq(p, &(Item){0});
             }
             if (accept(p, ELSE))
             {
-                // FJump(L)
-                // FixLink(item->imm.i)
+                codegen_else(p, item);
                 statement_seq(p, &(Item){0});
             }
-            else
-            {
-                // FixLink(item->imm.i)
-            }
+            codegen_endif(p, elsifs, item);
             expect(p, END);
         }
         else
index d73b8e4be51f836377684ccfc01d31ebedad6950..f7bc47c1f04b921b035d281ffbe735d9f0afc149 100644 (file)
@@ -55,10 +55,31 @@ begin
 #  # Complex arithmetic
 #  c = b + b * b + b;
 #
+
+
+  # If statements
+  if 1 == 1 then
+    c = 1;
+  end
+
+  if 1 == 1 then
+    c = 1;
+  else
+    c = 1;
+  end
+
+  if 1 == 1 then
+    c = 1;
+  elsif 2 == 2 then
+    c = 1;
+  end
+
   if 1 == 1 then
     c = 1;
   elsif 2 == 2 then
     c = 2;
+  elsif 2 == 2 then
+    c = 2;
   else
     c = 3;
   end