From 53840881d737118b815499ebdf3ddbaa82541c72 Mon Sep 17 00:00:00 2001 From: servostar Date: Fri, 2 Aug 2024 18:48:24 +0200 Subject: [PATCH] added token and parser rules for function return value --- CMakeLists.txt | 4 ++++ src/ast/ast.c | 1 + src/ast/ast.h | 1 + src/lex/lexer.l | 1 + src/yacc/parser.y | 23 ++++++++++++++++++++++- tests/stdlib/src/matrix.gsc | 5 ++++- 6 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ee5a34a..d5de47b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,8 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(LEX_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/lex/lexer.l) set(LEX_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/lex/lexer.ll.c) +file(REMOVE ${LEX_GENERATED_SOURCE_FILE}) + add_custom_command(OUTPUT ${LEX_GENERATED_SOURCE_FILE} COMMAND lex ARGS -o ${LEX_GENERATED_SOURCE_FILE} ${LEX_SOURCE_FILE} @@ -62,6 +64,8 @@ endif() set(YACC_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/yacc/parser.y) set(YACC_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/yacc/parser.tab.c) +file(REMOVE ${YACC_GENERATED_SOURCE_FILE}) + add_custom_command(OUTPUT ${YACC_GENERATED_SOURCE_FILE} COMMAND bison ARGS -Wno-yacc -Wcounterexamples -d -o ${YACC_GENERATED_SOURCE_FILE} ${YACC_SOURCE_FILE} diff --git a/src/ast/ast.c b/src/ast/ast.c index a0321bb..582c3d4 100644 --- a/src/ast/ast.c +++ b/src/ast/ast.c @@ -86,6 +86,7 @@ void AST_init() { lookup_table[AST_AddressOf] = "address of"; lookup_table[AST_Dereference] = "deref"; lookup_table[AST_Reference] = "ref"; + lookup_table[AST_Reference] = "ret"; } const char* AST_node_to_string(const struct AST_Node_t* node) { diff --git a/src/ast/ast.h b/src/ast/ast.h index ea2b332..4ead853 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -81,6 +81,7 @@ enum AST_SyntaxElement_t { AST_Reference, AST_Include, AST_Char, + AST_Return, AST_ELEMENT_COUNT }; diff --git a/src/lex/lexer.l b/src/lex/lexer.l index d98c1da..9902ea4 100644 --- a/src/lex/lexer.l +++ b/src/lex/lexer.l @@ -90,6 +90,7 @@ "funname" {DEBUG("\"%s\" tokenized with \'FunFunname\'", yytext); return(FunFunname);}; "lineno" {DEBUG("\"%s\" tokenized with \'FunLineno\'", yytext); return(FunLineno);}; "extsupport" {DEBUG("\"%s\" tokenized with \'FunExtsupport\'", yytext); return(FunExtsupport);}; +"ret" {DEBUG("\"%s\" tokenized with \'Return\'", yytext); return(KeyReturn);}; [0-9]+ {DEBUG("\"%s\" tokenized with \'ValInt\'", yytext); yylval.string = mem_strdup(MemoryNamespaceLex, yytext); return(ValInt); }; [0-9]*\.[0-9]+ {DEBUG("\"%s\" tokenized with \'ValFloat\'", yytext); yylval.string = mem_strdup(MemoryNamespaceLex, yytext); return(ValFloat);}; diff --git a/src/yacc/parser.y b/src/yacc/parser.y index 3408b94..d1468fd 100644 --- a/src/yacc/parser.y +++ b/src/yacc/parser.y @@ -72,6 +72,7 @@ %type reinterpretcast %type program %type storage_expr +%type return %token KeyInt @@ -122,6 +123,7 @@ %token FunLineno %token FunExtsupport %token Invalid +%token KeyReturn /* Operator associativity */ /* Operators at lower line number have lower precedence */ @@ -192,11 +194,20 @@ argumentlist: argumentlist '(' exprlist ')' {AST_push_node($1, $3); $$ = list;}; +// TODO: add ast node for definition and declaration fundef: KeyFun Ident paramlist '{' statementlist'}' {AST_NODE_PTR fun = AST_new_node(new_loc(), AST_Fun, NULL); AST_NODE_PTR ident = AST_new_node(new_loc(), AST_Ident, $2); AST_push_node(fun, ident); AST_push_node(fun, $3); AST_push_node(fun, $5); + $$ = fun; + DEBUG("Function");} + | KeyFun Ident paramlist ':' type '{' statementlist'}' {AST_NODE_PTR fun = AST_new_node(new_loc(), AST_Fun, NULL); + AST_NODE_PTR ident = AST_new_node(new_loc(), AST_Ident, $2); + AST_push_node(fun, ident); + AST_push_node(fun, $3); + AST_push_node(fun, $5); + AST_push_node(fun, $7); $$ = fun; DEBUG("Function");}; @@ -205,7 +216,13 @@ fundecl: KeyFun Ident paramlist {AST_NODE_PTR fun = AST_new_node(new_loc(), AST_ AST_push_node(fun, ident); AST_push_node(fun, $3); $$ = fun; - DEBUG("Function");}; + DEBUG("Function");} + | KeyFun Ident paramlist ':' type {AST_NODE_PTR fun = AST_new_node(new_loc(), AST_Fun, NULL); + AST_NODE_PTR ident = AST_new_node(new_loc(), AST_Ident, $2); + AST_push_node(fun, ident); + AST_push_node(fun, $3); + $$ = fun; + DEBUG("Function");}; paramlist: paramlist '(' params ')' {AST_push_node($1, $3); $$ = $1;} @@ -348,9 +365,13 @@ statement: assign {$$ = $1;} | definition {$$ = $1;} | while {$$ = $1;} | branchfull {$$ = $1;} + | return {$$ = $1;} | funcall {$$ = $1;} | boxcall{$$ = $1;}; +return: KeyReturn expr { AST_NODE_PTR return_stmt = AST_new_node(new_loc(), AST_Return, NULL); + AST_push_node(return_stmt, $2); }; + branchif: KeyIf expr '{' statementlist '}' { AST_NODE_PTR branch = AST_new_node(new_loc(), AST_If, NULL); AST_push_node(branch, $2); AST_push_node(branch, $4); diff --git a/tests/stdlib/src/matrix.gsc b/tests/stdlib/src/matrix.gsc index e14b1a3..234629f 100644 --- a/tests/stdlib/src/matrix.gsc +++ b/tests/stdlib/src/matrix.gsc @@ -92,7 +92,10 @@ fun test_matrix() heapFree(matrix as ref u8) } -fun main() +fun main(): u32 { test_matrix() + exit_code = 0 + + ret 0 }