]> git.mdlowis.com Git - proto/obnc.git/commitdiff
removed distinction between unop and binop in API
authorMichael D. Lowis <mike@mdlowis.com>
Mon, 19 Jul 2021 03:18:17 +0000 (23:18 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Mon, 19 Jul 2021 03:18:17 +0000 (23:18 -0400)
cerise/inc/cerise.h
cerise/src/ast.c
cerise/src/grammar.c
cerise/tests/Module.m

index 08f147471916a9529152b66196aee8e3c0069539..40b55a6ccb50c7624ed3b21bceb2711a04a3b08a 100644 (file)
@@ -205,8 +205,7 @@ bool ast_asbool(AstNode* node);
 long long ast_asint(AstNode* node);
 double ast_asreal(AstNode* node);
 
-AstNode* ast_binop(int op, AstNode* left, AstNode* right);
-AstNode* ast_unop(int op, AstNode* operand);
+AstNode* ast_op(int op, AstNode* left, AstNode* right);
 AstNode* ast_store(AstNode* dest, AstNode* value);
 AstNode* ast_fieldref(Parser* p, AstNode* record, char* fname);
 AstNode* ast_index(Parser* p, AstNode* array, AstNode* index);
index 14defd44107c7b5857f4d0fef03f556ba2f3c9a8..ac1c78ab28f644493a457273cfa44a62f9fcfd08 100644 (file)
@@ -132,7 +132,7 @@ double ast_asreal(AstNode* node)
     return ((AstValue*)node)->val.f;
 }
 
-AstNode* ast_binop(int op, AstNode* left, AstNode* right)
+static AstNode* ast_binop(int op, AstNode* left, AstNode* right)
 {
     assert(left);
     assert(right);
@@ -259,7 +259,7 @@ AstNode* ast_binop(int op, AstNode* left, AstNode* right)
     return ret;
 }
 
-AstNode* ast_unop(int op, AstNode* operand)
+static AstNode* ast_unop(int op, AstNode* operand)
 {
     assert(operand);
     AstNode* ret = NULL;
@@ -305,6 +305,13 @@ AstNode* ast_unop(int op, AstNode* operand)
     return ret;
 }
 
+AstNode* ast_op(int op, AstNode* left, AstNode* right)
+{
+    return (right
+        ? ast_binop(op, left, right)
+        : ast_unop(op, left));
+}
+
 AstNode* ast_store(AstNode* dest, AstNode* value)
 {
     /* TODO: validate left-hand side is assignable */
@@ -477,16 +484,22 @@ static void print(Parser* p, AstNode* node, int indent)
         case IDENT:
             {
                 Symbol* s = symbol_getbyid(p, ((AstValue*)node)->val.i);
-                printf("S:%s", s->name);
+                printf("%s.%lld", s->name, ((AstValue*)node)->tag);
             }
             break;
 
         case '.':
-            printf("(field-ref)");
+            printf("(field-ref\n");
+            print(p, node->links[0], indent+1);
+            print(p, node->links[1], indent+1);
+            print_indent(indent, ")");
             break;
 
         case '[':
-            printf("(array-index)");
+            printf("(array-index\n");
+            print(p, node->links[0], indent+1);
+            print(p, node->links[1], indent+1);
+            print_indent(indent, ")");
             break;
 
         case BEGIN:
index b5dbffab5a3cbbd677def33bcc978588b5d5766e..e7295b6f903bf57cb5e2793fdeb7f72a6a46f9c6 100644 (file)
@@ -166,7 +166,7 @@ static AstNode* factor(Parser* p)
 
         case NOT:
             consume(p);
-            expr = ast_unop(NOT, factor(p));
+            expr = ast_op(NOT, factor(p), NULL);
             break;
 
         case IDENT:
@@ -238,12 +238,12 @@ static AstNode* term(Parser* p)
             case '/':
             case '%':
                 check_nums(p, expr, right);
-                expr = ast_binop(op, expr, right);
+                expr = ast_op(op, expr, right);
                 break;
 
             case AND:
                 check_bools(p, expr, right);
-                expr = ast_binop(op, expr, right);
+                expr = ast_op(op, expr, right);
                 break;
 
             default:
@@ -269,7 +269,7 @@ static AstNode* simple_expr(Parser* p)
         int op = consume(p);
         AstNode* operand = term(p);
         check_num(p, operand);
-        expr = ast_unop(op, operand);
+        expr = ast_op(op, operand, NULL);
     }
     else
     {
@@ -289,7 +289,7 @@ static AstNode* simple_expr(Parser* p)
         {
             check_nums(p, expr, right);
         }
-        expr = ast_binop(op, expr, right);
+        expr = ast_op(op, expr, right);
     }
 
     EXIT_RULE();
@@ -309,7 +309,7 @@ static AstNode* expression(Parser* p)
         int op = consume(p);
         AstNode* right = simple_expr(p);
         check_nums(p, expr, right);
-        expr = ast_binop(op, expr, right);
+        expr = ast_op(op, expr, right);
     }
 
     EXIT_RULE();
@@ -338,7 +338,6 @@ static Type* type(Parser* p)
         long long length = ast_asint(lnode);
         expect(p, OF);
         Type* base = type(p);
-//        ret = calloc(1, sizeof(Type));
         ret = symbol_newtype(p);
         ret->form = FORM_ARRAY;
         ret->size = length;
@@ -347,7 +346,6 @@ static Type* type(Parser* p)
     else if (accept(p, RECORD))
     {
         long offset = 0;
-//        ret = calloc(1, sizeof(Type));
         ret = symbol_newtype(p);
         ret->form = FORM_RECORD;
 
@@ -558,7 +556,7 @@ void proc_decl(Parser* p)
     expect(p, END);
     symbol_closescope(p, scope);
 
-//    ast_print(p, proc->value);
+    ast_print(p, proc->value);
     (void)proc->value;
 
     EXIT_RULE();
@@ -622,7 +620,7 @@ static void module(Parser* p)
     if (accept(p, BEGIN))
     {
         AstNode* block = statement_seq(p);
-//        ast_print(p, block);
+        ast_print(p, block);
         (void)block;
         expect(p, END);
     }
@@ -632,7 +630,7 @@ static void module(Parser* p)
         error(p, "expected end of file");
     }
 
-    symbol_export(p, NULL);
+//    symbol_export(p, NULL);
     symbol_closescope(p, scope);
     EXIT_RULE();
 }
index dd92fb44f1273ba20fb6853cc7415f5ffe06d8c6..58698a518cfd85c572a2f7f817d11c71e9cfa33a 100644 (file)
@@ -69,7 +69,7 @@ begin
 end
 
 begin
-#    h[1].i = 42;
+    h[1].i = 42;
 #  a = true;
 #  a = A;
 #  b = 24;