]> git.mdlowis.com Git - proto/obnc.git/commitdiff
added scaffolding for arithmetic operators. will fill in details later for constant...
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 20 Apr 2021 20:55:15 +0000 (16:55 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 20 Apr 2021 20:55:15 +0000 (16:55 -0400)
cerise/cerise.h
cerise/codegen.c
cerise/lex.c
cerise/parser.c

index 36f9ce1bb779b85c0474d04c9fc5ca06b5a5bfe6..cabcd7ac08c1f9b4cf080249b527d32e6b26d1a2 100644 (file)
@@ -132,8 +132,13 @@ extern Type BoolType, IntType, RealType, StringType;
 void codegen_setint(Item* item, Type* type, long long val);
 void codegen_setreal(Item* item, double val);
 void codegen_setstr(Item* item, char* val);
+
 void codegen_not(Item* item);
+void codegen_add(Item* a, Item* b);
+void codegen_sub(Item* a, Item* b);
 void codegen_mul(Item* a, Item* b);
+void codegen_div(Item* a, Item* b);
+void codegen_mod(Item* a, Item* b);
 
 /* Option Parsing
  *****************************************************************************/
index 48e527c2f38de3b6ddb24ae8dda653d3d4569134..f00bbadddf58f9a7747a0c5c1972a28261f3b8b2 100644 (file)
@@ -119,6 +119,18 @@ void codegen_not(Item* item)
     }
 }
 
+void codegen_add(Item* a, Item* b)
+{
+    (void)a, (void)b;
+    assert(!"not implemented");
+}
+
+void codegen_sub(Item* a, Item* b)
+{
+    (void)a, (void)b;
+    assert(!"not implemented");
+}
+
 void codegen_imul(Item* a, Item* b)
 {
     if ((b->mode == ITEM_CONST) && (b->mode == ITEM_CONST))
@@ -154,3 +166,15 @@ void codegen_mul(Item* a, Item* b)
         codegen_imul(a,b);
     }
 }
+
+void codegen_div(Item* a, Item* b)
+{
+    (void)a, (void)b;
+    assert(!"not implemented");
+}
+
+void codegen_mod(Item* a, Item* b)
+{
+    (void)a, (void)b;
+    assert(!"not implemented");
+}
index 2f47a530da9006993bafb80ed342837896d2a0cd..5c52bb9e47cba5dd6dbba71dc4653a04aab920c7 100644 (file)
@@ -39,7 +39,7 @@ static const char Chars[256] = {
     ['('] = 6, [')'] = 6, ['['] = 6, [']'] = 6, ['{'] = 6,
     ['}'] = 6, [','] = 6, [':'] = 6, [';'] = 6,
     ['^'] = 6, ['+'] = 6, ['-'] = 6, ['*'] = 6, ['/'] = 6,
-    ['|'] = 6,
+    ['|'] = 6, ['%'] = 6,
 
     /* number digits */
     ['0'] = 7, ['1'] = 7, ['2'] = 7, ['3'] = 7, ['4'] = 7,
index 360c774660e3b64c46fdfbd48023bde19051e7d2..b9dedc22e639a3afbe8ae49fdbed15f81b16146e 100644 (file)
@@ -333,22 +333,26 @@ RULE(term)
     // term = factor {MulOperator factor}.
     //     MulOperator = "*" | "/" | "and".
     factor(p, item);
-    while (matches_oneof(p, (int[]){'*', '/', AND, 0}))
+    while (matches_oneof(p, (int[]){'*', '/', '%', AND, 0}))
     {
         Item right = { 0 };
         int op = consume(p);
         factor(p, &right);
         switch(op)
         {
-            case '/':
+            case '*':
                 check_num2(p, item, &right);
-//                codegen_div(item, &right);
-                assert(!"not implemented");
+                codegen_mul(item, &right);
                 break;
 
-            case '*':
+            case '/':
                 check_num2(p, item, &right);
-                codegen_mul(item, &right);
+                codegen_div(item, &right);
+                break;
+
+            case '%':
+                check_int2(p, item, &right);
+                codegen_mod(item, &right);
                 break;
 
             case AND:
@@ -529,7 +533,7 @@ TEST_SUITE(Grammar)
             "import A, B = ModB, C;");
     }
 
-    TEST(Should parse constant declarations)
+    TEST(Should parse constant declarations and expressions)
     {
         parse_rule(const_decl, 0,
             "FOO = 123");
@@ -552,9 +556,27 @@ TEST_SUITE(Grammar)
         parse_rule(const_decl, 0,
             "FOO = -1");
         parse_rule(const_decl, 0,
-            "FOO = 1 + 2 + 3");
+            "FOO = +1.0");
+        parse_rule(const_decl, 0,
+            "FOO = -1.0");
+        parse_rule(const_decl, 0,
+            "FOO = 1 + 2");
+        parse_rule(const_decl, 0,
+            "FOO = 1.0 + 2.0");
+        parse_rule(const_decl, 0,
+            "FOO = 1 - 2");
+        parse_rule(const_decl, 0,
+            "FOO = 1.0 - 2.0");
+        parse_rule(const_decl, 0,
+            "FOO = 1 * 2");
+        parse_rule(const_decl, 0,
+            "FOO = 1.0 * 2.0");
+        parse_rule(const_decl, 0,
+            "FOO = 1 / 2");
+        parse_rule(const_decl, 0,
+            "FOO = 1.0 / 2.0");
         parse_rule(const_decl, 0,
-            "FOO = 1 + 2 * 4");
+            "FOO = 1 % 2");
     }
 }
 #endif