From cde73d9f5a97aa2b3fbe16d6677f1c0ba2dbd95f Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 3 Dec 2021 15:54:06 -0500 Subject: [PATCH] fixed code for handling returns from functions --- cerise/src/grammar.c | 37 +++++-------------------------------- cerise/src/ssa.c | 5 +++++ cerise/tests/Module.m | 17 +++++++++-------- 3 files changed, 19 insertions(+), 40 deletions(-) diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index b476629..b989b57 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -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(); } diff --git a/cerise/src/ssa.c b/cerise/src/ssa.c index 45b74f3..bddfa48 100644 --- a/cerise/src/ssa.c +++ b/cerise/src/ssa.c @@ -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) { diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 92d535f..ed147c0 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -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; -- 2.49.0