]> git.mdlowis.com Git - proto/obnc.git/commitdiff
fixed code for handling returns from functions
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 3 Dec 2021 20:54:06 +0000 (15:54 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 3 Dec 2021 20:54:06 +0000 (15:54 -0500)
cerise/src/grammar.c
cerise/src/ssa.c
cerise/tests/Module.m

index b4766297c154b4e3de336a65174b4c381eda8f73..b989b57f7e3d9c94b6db873d59020009841b1e70 100644 (file)
@@ -438,6 +438,10 @@ static SsaBlock* statement_seq(Parser* p)
             p->curr_join = p->curr_join->next;
             expect(p, END);
         }
+        else if (matches(p, END) || matches(p,RETURN))
+        {
+            /* empty sequence */
+        }
         else /* assignments/expressions */
         {
             SsaNode* expr = expression(p);
@@ -573,17 +577,7 @@ void proc_decl(Parser* p)
     SsaBlock* block = ssa_block(p);
     p->curr_join = ssa_block(p);
     block->links[1] = p->curr_join;
-
-    if (!matches(p, RETURN) && !matches(p, END))
-    {
-        block->links[0] = statement_seq(p);
-    }
-    else
-    {
-        block->links[0] = ssa_block(p);
-        p->curr_block = block->links[0];
-    }
-
+    block->links[0] = statement_seq(p);
     if (proctype->base != &VoidType)
     {
         expect(p, RETURN);
@@ -595,27 +589,6 @@ void proc_decl(Parser* p)
     expect(p, END);
     ssa_print_graph(p, block);
 
-//    /* parse the body of the procedure */
-//    expect(p, BEGIN);
-//    proc->value = ast_block();
-//    if (!matches(p, RETURN) && !matches(p, END))
-//    {
-//        ast_block_add(proc->value, statement_seq(p));
-//    }
-//    if (proctype->base != &VoidType)
-//    {
-//        expect(p, RETURN);
-//        AstNode* retval = expression(p);
-//        check_type(p, proctype->base, retval);
-//        ast_block_add(proc->value, ast_return(retval));
-//        expect(p, ';');
-//    }
-//    expect(p, END);
-//    symbol_closescope(p, scope);
-//
-//    ast_print(p, proc->value);
-//    (void)proc->value;
-
     EXIT_RULE();
 }
 
index 45b74f3e73880ccfc4a00f8e5efd59573cec89d9..bddfa48086e3a76e1da9a0296710c15b34c80f9b 100644 (file)
@@ -490,6 +490,11 @@ void ssa_print(Parser* p, SsaNode* expr)
             printf(" then block%lu else block%lu", expr->left.block->id, expr->right.block->id);
             break;
 
+        case RETURN:
+            printf("return ");
+            print_ident(p, &(expr->dest));
+            break;
+
         default:
             if (expr->mode == MODE_VAR)
             {
index 92d535f5b128a4c6deb82aa6eee096ef1760aaa3..ed147c01856b9dfee950f284a3944bcaa6f04ff4 100644 (file)
@@ -56,17 +56,18 @@ var
 
 procedure Bar*(a : Int) : Int
 begin
+    a = 42;
     return a;
 end
 
-#procedure Baz*(a : Int)
-#begin
-#    if (1 > 2) then
-#        42;
-#    else
-#        24;
-#    end
-#end
+procedure Baz*(a : Int)
+begin
+    if (1 > 2) then
+        42;
+    else
+        24;
+    end
+end
 
 begin
   b = 42;