]> git.mdlowis.com Git - proto/obnc.git/commitdiff
added handling of most operators
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 28 Apr 2021 18:50:06 +0000 (14:50 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 28 Apr 2021 18:50:06 +0000 (14:50 -0400)
.gitignore
cerise/backend/c99/codegen.c
cerise/src/parser.c
cerise/tests/Module.m

index 3cb8f4602605fd22503c29bcbe2393628e42f482..327418d59164ea5c6c9433db31ba5183ebc157f2 100644 (file)
@@ -18,3 +18,4 @@ y.tab.*
 cerisec
 cerisec-test
 
+cerisec-x86_64
index 1d8da23eb3509b5fddc40dc767757b4a1f06d2c3..bb14f9a69bc2abc01336bdea3f475e93c97d7d14 100644 (file)
@@ -29,12 +29,22 @@ static char* TypeNames[] = {
     [FORM_STRING] = "char*"
 };
 
-static char* BinaryOps[] = {
-    ['+'] = "+",
-    ['-'] = "-",
-    ['*'] = "*",
-    ['/'] = "/",
-    ['%'] = "%",
+static char* Operators[] = {
+    ['+']  = "+",
+    ['-']  = "-",
+    ['*']  = "*",
+    ['/']  = "/",
+    ['%']  = "%",
+    [AND]  = "&&",
+    [OR]   = "||",
+    [NOT]  = "!",
+    [EQ]   = "==",
+    [NEQ]  = "!=",
+    ['<']  = "<",
+    [LTEQ] = "<=",
+    ['>']  = ">",
+    [GTEQ] = ">=",
+//    [IS]   = ???
 };
 
 static void load_var(Parser* p, Item* item)
@@ -66,18 +76,30 @@ static void load_var(Parser* p, Item* item)
     }
 }
 
-/* Operators
+/* Operator Handling
  *****************************************************************************/
-void binary_op(Parser* p, int op, Item* a, Item* b)
+static void binary_op(Parser* p, int op, Item* a, Item* b)
 {
     char* type = TypeNames[a->type->form];
-    char* oper = BinaryOps[op];
+    char* oper = Operators[op];
+    assert(type && oper);
     printf("    const %s _T%d = _T%d %s _T%d;\n",
         type, p->curr_reg, a->reg, oper, b->reg);
     a->reg = p->curr_reg;
     p->curr_reg++;
 }
 
+static void unary_op(Parser* p, int op, Item* a)
+{
+    char* type = TypeNames[a->type->form];
+    char* oper = Operators[op];
+    assert(type && oper);
+    printf("    const %s _T%d = %s _T%d;\n",
+        type, p->curr_reg, oper, a->reg);
+    a->reg = p->curr_reg;
+    p->curr_reg++;
+}
+
 /* Public Interface
  *****************************************************************************/
 void codegen_setint(Item* item, Type* type, long long val)
@@ -150,7 +172,8 @@ void codegen_unop(Parser* p, int op, Item* a)
     }
     else
     {
-        assert(!"not supported");
+        load_var(p, a);
+        unary_op(p, op, a);
     }
 }
 
index 0c6f7469f50464c4905305e99e71e1bd6ebcbc4b..508cccbca502667bb599eb3ea2162ae96a512011 100644 (file)
@@ -533,56 +533,6 @@ RULE(const_decl)
     }
 }
 
-/* Code generation for
-find(obj);
-OSS.Get(sym);
-IF obj.class = OSG.SProc THEN
-  StandProc(obj.val)
-ELSE
-  OSG.MakeItem(x, obj, level);
-  selector(x);
-
-  IF sym = OSS.becomes THEN (*assignment*)
-
-    OSS.Get(sym);
-    expression(y);
-    IF (x.type.form IN {OSG.Boolean, OSG.Integer}) & (x.type.form = y.type.form) THEN
-      OSG.Store(x, y)
-    ELSE
-      OSS.Mark("incompatible assignment")
-    END
-
-//  ELSIF sym = OSS.eql THEN
-//    OSS.Mark("should be :=");
-//    OSS.Get(sym);
-//    expression(y)
-//  ELSIF sym = OSS.lparen THEN (*procedure call*)
-//    OSS.Get(sym);
-//    IF (obj.class = OSG.Proc) & (obj.type = NIL) THEN
-//      ParamList(obj);
-//      OSG.Call(obj);
-//    ELSE
-//      OSS.Mark("not a procedure")
-//    END
-//  ELSIF obj.class = OSG.Proc THEN (*procedure call without parameters*)
-//    IF obj.nofpar > 0 THEN
-//      OSS.Mark("missing parameters")
-//    END ;
-//    IF obj.type = NIL THEN
-//      OSG.Call(obj) ELSE
-//      OSS.Mark("not a procedure")
-//    END
-//  ELSIF (obj.class = OSG.SProc) & (obj.val = 3) THEN
-//    OSG.WriteLn
-//  ELSIF obj.class = OSG.Typ THEN
-//    OSS.Mark("illegal assignment")
-//  ELSE
-//    OSS.Mark("not a procedure")
-//  END
-END
-
-*/
-
 RULE(statement_seq)
 {
     while (1)
index 960e45cf28d2cad377180df5cc582106d2a66b45..c78051e66e010e1e0087c7b2c973a758c5e44894 100644 (file)
@@ -20,8 +20,8 @@ begin
     b = B;
 
     # Unary ops
-#    b = +b;
-#    b = -b;
+    b = +b;
+    b = -b;
 
     # Immediate ops
     c = b + 1;