Skip to content

Commit

Permalink
fix: add check for procedure return use not allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
zestones committed Dec 30, 2024
1 parent d411a83 commit 895595f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion example/compilation/to-remove.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var fib_1 : int;
var fib_2 : int;


func main(n: int) -> int {
proc main(n: int) {
if (n < 1) {
return -1;
}
Expand Down
8 changes: 7 additions & 1 deletion src/parser/grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,15 @@ return_statement: RETURN_VALUE expression SEMICOLON {
set_error_type(&error, SYNTAX_ERROR);
yyerror("Return statement is not allowed outside of a function.");
}
int index_declaration = find_function_index_by_region(peek_region());
if (index_declaration == NULL_VALUE) {
set_error_type(&error, SYNTAX_ERROR);
yyerror("Return statement is not allowed outside of a function.");
}

$$ = construct_node_default(A_RETURN_STATEMENT);
add_child($$, $2);
check_func_prototype(get_func_return_type(find_function_index_by_region(peek_region())), $$);
check_func_prototype(get_func_return_type(index_declaration), $$);
}
;

Expand Down

0 comments on commit 895595f

Please sign in to comment.