Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for return statements #45

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions include/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ struct ast_node_range_expression
sym_ptr symbol_entry;
ast_node_param *params;
ast_node_compound_statement *body;
ast_node_expression *return_statment;
};

struct ast_node_param
Expand Down Expand Up @@ -375,7 +374,7 @@ ast_node_conditional_else_if *add_else_if_node(ast_node_conditional_else_if *par
ast_node_loop_for *create_loop_for_node(ast_node_variable *init, ast_node_range_expression *range, ast_node_compound_statement *body);
ast_node_loop_while *create_loop_while_node(ast_node_expression *condition, ast_node_compound_statement *body);
ast_node_loop_control *create_loop_control_node(int node_type);
ast_node_function_def *create_function_def_node(sym_ptr symbol_entry, ast_node_param *params, ast_node_compound_statement *body, ast_node_expression *return_stmt);
ast_node_function_def *create_function_def_node(sym_ptr symbol_entry, ast_node_param *params, ast_node_compound_statement *body);
ast_node_param *create_parameter_node();
ast_node_param *add_parameter_node(ast_node_param *parent, ast_node_variable *var);
ast_node_function_call *create_function_call_node(sym_ptr symbol, ast_node_arguments *arguments);
Expand Down
1 change: 0 additions & 1 deletion include/semantic.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
#include "symbol_table.h"

int check_function_call(ast_node_function_call *function_call);
int check_function_definition(ast_node_function_def *function_defs);

#endif
3 changes: 1 addition & 2 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,14 @@ ast_node_loop_control *create_loop_control_node(int node_type)
return loop_control;
}

ast_node_function_def *create_function_def_node(sym_ptr symbol_entry, ast_node_param *params, ast_node_compound_statement *body, ast_node_expression *return_stmt)
ast_node_function_def *create_function_def_node(sym_ptr symbol_entry, ast_node_param *params, ast_node_compound_statement *body)
{
ast_node_function_def *function_def = (ast_node_function_def*)malloc(sizeof(ast_node_function_def));

function_def->node_type = AST_NODE_FUNCTION_DEFS;
function_def->symbol_entry = symbol_entry;
function_def->params = params;
function_def->body = body;
function_def->return_statment = return_stmt;

return function_def;
}
Expand Down
13 changes: 6 additions & 7 deletions src/code_printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ void ast_compound_statement_printer(ast_node_compound_statement *cmpd_stmt, FILE
fprintf(handle, "%s", ";\n");
break;

case AST_NODE_FUNC_RETURN:
fprintf(handle, "\t%s ", "return");
ast_expression_printer(((ast_node_statements*)temp)->child_nodes.return_statement, handle);
fprintf(handle, "%s", ";\n");
break;

case AST_NODE_LOOP_BREAK:
if (((ast_node_statements*)temp)->child_nodes.loop_control->node_type == AST_NODE_LOOP_BREAK)
{
Expand Down Expand Up @@ -626,13 +632,6 @@ void ast_function_definition(ast_node_function_def *def, FILE* handle)
}
fprintf(handle, "%s", ")\n");
ast_compound_statement_printer(def->body, handle, 1);

if (def->return_statment != NULL)
{
fprintf(handle, "\t%s ", "return");
ast_expression_printer(def->return_statment, handle);
fprintf(handle, "%s", ";\n");
}
fprintf(handle, "%s", "}\n");
}
}
Expand Down
29 changes: 28 additions & 1 deletion src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
int function_flag = 0;
int inside_loop = 0;
int brackets = 0;
int func = 0;
int inside_func = 0;
int brac = 0;
int ret = 0;

void yyerror(char* s);
int yylex();
Expand Down Expand Up @@ -54,6 +58,11 @@ comma ([,])
{braces} {
if (!strcmp(yytext, "{"))
{
brac += 1;
if (func == 1)
{
inside_func = 1;
}
if (for_loop_flag == -1)
{
for_loop_flag = 0;
Expand All @@ -76,8 +85,18 @@ comma ([,])
}
else if (!strcmp(yytext, "}"))
{
brac -= 1;
decrement_scope();

if (inside_func == 1 && ret == 0 && brac == 0)
{
yyerror("No return statement for a non void function ");
}
if (inside_func == 1 && brac == 0)
{
inside_func -= 1;
func = 0;
}
if (inside_loop)
{
brackets -= 1;
Expand Down Expand Up @@ -305,7 +324,15 @@ comma ([,])
{function} {
if (!strcmp(yytext, "return"))
{
ret += 1;
if (inside_func != 1)
{
yyerror("Cannot call return outside function definition");
}
if (inside_func == 1 && ret > 0)
{
return KW_RETURN;
}
}
else if (!strcmp(yytext, "void"))
{
Expand All @@ -314,7 +341,7 @@ comma ([,])
else if (!strcmp(yytext, "def"))
{
function_flag = 1;

func = 1;
return KW_DEF;
}
}
Expand Down
72 changes: 4 additions & 68 deletions src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -784,96 +784,32 @@ function_definition: KW_DEF IDENTIFIER COLON DT_INT {
if ($2 == NULL){yyerror("function name already defined");}
temp = $2; temp->data_type = DT_INTEGER;}
COLON parameters compound_statement {
if (vec_last(&$8->child_nodes)->node_type == AST_NODE_FUNC_RETURN)
{
$$ = create_function_def_node($2, $7, $8, vec_pop(&$8->child_nodes)->child_nodes.return_statement);
}
else if (vec_last(&$8->child_nodes)->node_type != AST_NODE_FUNC_RETURN && $2->data_type == DT_VOID_)
{
$$ = create_function_def_node($2, $7, $8, NULL);
}
else if (vec_last(&$8->child_nodes)->node_type != AST_NODE_FUNC_RETURN && $2->data_type != DT_VOID_)
{
yyerror("return statement missing in a non void function");
}
$$ = create_function_def_node($2, $7, $8);
temp = NULL;

if (check_function_definition($$) == -1)
{
yyerror("return statement different from return type");
}
printf("func\n");
}
| KW_DEF IDENTIFIER COLON DT_BOOL {
if ($2 == NULL){yyerror("function name already defined");}
temp = $2; temp->data_type = DT_BOOLEAN;}
COLON parameters compound_statement {
if (vec_last(&$8->child_nodes)->node_type == AST_NODE_FUNC_RETURN)
{
$$ = create_function_def_node($2, $7, $8, vec_pop(&$8->child_nodes)->child_nodes.return_statement);
}
else if (vec_last(&$8->child_nodes)->node_type != AST_NODE_FUNC_RETURN && $2->data_type == DT_VOID_)
{
$$ = create_function_def_node($2, $7, $8, NULL);
}
else if (vec_last(&$8->child_nodes)->node_type != AST_NODE_FUNC_RETURN && $2->data_type != DT_VOID_)
{
yyerror("return statement missing in a non void function");
}
$$ = create_function_def_node($2, $7, $8);
temp = NULL;

if (check_function_definition($$) == -1)
{
yyerror("return statement different from return type");
}
printf("func\n");
}
| KW_DEF IDENTIFIER COLON DT_CHAR {
if ($2 == NULL){yyerror("function name already defined");}
temp = $2; temp->data_type = DT_CHAR_;}
COLON parameters compound_statement {
if (vec_last(&$8->child_nodes)->node_type == AST_NODE_FUNC_RETURN)
{
$$ = create_function_def_node($2, $7, $8, vec_pop(&$8->child_nodes)->child_nodes.return_statement);
}
else if (vec_last(&$8->child_nodes)->node_type != AST_NODE_FUNC_RETURN && $2->data_type == DT_VOID_)
{
$$ = create_function_def_node($2, $7, $8, NULL);
}
else if (vec_last(&$8->child_nodes)->node_type != AST_NODE_FUNC_RETURN && $2->data_type != DT_VOID_)
{
yyerror("return statement missing in a non void function");
}
$$ = create_function_def_node($2, $7, $8);
temp = NULL;

if (check_function_definition($$) == -1)
{
yyerror("return statement different from return type");
}
printf("func\n");
}
| KW_DEF IDENTIFIER COLON DT_VOID {
if ($2 == NULL){yyerror("function name already defined");}
temp = $2; temp->data_type = DT_VOID_;}
COLON parameters compound_statement {
if (vec_last(&$8->child_nodes)->node_type == AST_NODE_FUNC_RETURN)
{
$$ = create_function_def_node($2, $7, $8, vec_pop(&$8->child_nodes)->child_nodes.return_statement);
}
else if (vec_last(&$8->child_nodes)->node_type != AST_NODE_FUNC_RETURN && $2->data_type == DT_VOID_)
{
$$ = create_function_def_node($2, $7, $8, NULL);
}
else if (vec_last(&$8->child_nodes)->node_type != AST_NODE_FUNC_RETURN && $2->data_type != DT_VOID_)
{
yyerror("return statement missing in a non void function");
}
$$ = create_function_def_node($2, $7, $8);
temp = NULL;

if (check_function_definition($$) == -1)
{
yyerror("return statement different from return type");
}
printf("func\n");
}
;
Expand Down
23 changes: 0 additions & 23 deletions src/semantic.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,3 @@ int check_function_call(ast_node_function_call *function_call)
return -1;
}

int check_function_definition(ast_node_function_def *function_defs)
{
if (function_defs->symbol_entry != NULL)
{
int expected_dt = function_defs->symbol_entry->data_type;

if (function_defs->return_statment == NULL && expected_dt == DT_VOID_)
{
return 1;
}
else if (function_defs->return_statment->node_type == AST_NODE_ARITHMETIC_EXP
&& (expected_dt == DT_INTEGER || expected_dt == DT_CHAR_))
{
return 1;
}
else if (function_defs->return_statment->node_type == AST_NODE_BOOLEAN_EXP && expected_dt == DT_BOOLEAN)
{
return 1;
}
}

return -1;
}
13 changes: 13 additions & 0 deletions tests/run_anywhere/return.sim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def testing: int: int a, int b, bool control {
int c;
if: control {
c := a + b;
return c;
}
else {
c := a * b;
return c;
}
}
int result := testing(3, 4, true);
print(result);
1 change: 1 addition & 0 deletions tests/run_anywhere/return.sim.output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7