added token and parser rules for function return value
This commit is contained in:
parent
34361bd834
commit
53840881d7
|
@ -44,6 +44,8 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
set(LEX_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/lex/lexer.l)
|
set(LEX_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/lex/lexer.l)
|
||||||
set(LEX_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/lex/lexer.ll.c)
|
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}
|
add_custom_command(OUTPUT ${LEX_GENERATED_SOURCE_FILE}
|
||||||
COMMAND lex
|
COMMAND lex
|
||||||
ARGS -o ${LEX_GENERATED_SOURCE_FILE} ${LEX_SOURCE_FILE}
|
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_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/yacc/parser.y)
|
||||||
set(YACC_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/yacc/parser.tab.c)
|
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}
|
add_custom_command(OUTPUT ${YACC_GENERATED_SOURCE_FILE}
|
||||||
COMMAND bison
|
COMMAND bison
|
||||||
ARGS -Wno-yacc -Wcounterexamples -d -o ${YACC_GENERATED_SOURCE_FILE} ${YACC_SOURCE_FILE}
|
ARGS -Wno-yacc -Wcounterexamples -d -o ${YACC_GENERATED_SOURCE_FILE} ${YACC_SOURCE_FILE}
|
||||||
|
|
|
@ -86,6 +86,7 @@ void AST_init() {
|
||||||
lookup_table[AST_AddressOf] = "address of";
|
lookup_table[AST_AddressOf] = "address of";
|
||||||
lookup_table[AST_Dereference] = "deref";
|
lookup_table[AST_Dereference] = "deref";
|
||||||
lookup_table[AST_Reference] = "ref";
|
lookup_table[AST_Reference] = "ref";
|
||||||
|
lookup_table[AST_Reference] = "ret";
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* AST_node_to_string(const struct AST_Node_t* node) {
|
const char* AST_node_to_string(const struct AST_Node_t* node) {
|
||||||
|
|
|
@ -81,6 +81,7 @@ enum AST_SyntaxElement_t {
|
||||||
AST_Reference,
|
AST_Reference,
|
||||||
AST_Include,
|
AST_Include,
|
||||||
AST_Char,
|
AST_Char,
|
||||||
|
AST_Return,
|
||||||
AST_ELEMENT_COUNT
|
AST_ELEMENT_COUNT
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -90,6 +90,7 @@
|
||||||
"funname" {DEBUG("\"%s\" tokenized with \'FunFunname\'", yytext); return(FunFunname);};
|
"funname" {DEBUG("\"%s\" tokenized with \'FunFunname\'", yytext); return(FunFunname);};
|
||||||
"lineno" {DEBUG("\"%s\" tokenized with \'FunLineno\'", yytext); return(FunLineno);};
|
"lineno" {DEBUG("\"%s\" tokenized with \'FunLineno\'", yytext); return(FunLineno);};
|
||||||
"extsupport" {DEBUG("\"%s\" tokenized with \'FunExtsupport\'", yytext); return(FunExtsupport);};
|
"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]+ {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);};
|
[0-9]*\.[0-9]+ {DEBUG("\"%s\" tokenized with \'ValFloat\'", yytext); yylval.string = mem_strdup(MemoryNamespaceLex, yytext); return(ValFloat);};
|
||||||
|
|
|
@ -72,6 +72,7 @@
|
||||||
%type <node_ptr> reinterpretcast
|
%type <node_ptr> reinterpretcast
|
||||||
%type <node_ptr> program
|
%type <node_ptr> program
|
||||||
%type <node_ptr> storage_expr
|
%type <node_ptr> storage_expr
|
||||||
|
%type <node_ptr> return
|
||||||
|
|
||||||
|
|
||||||
%token KeyInt
|
%token KeyInt
|
||||||
|
@ -122,6 +123,7 @@
|
||||||
%token FunLineno
|
%token FunLineno
|
||||||
%token FunExtsupport
|
%token FunExtsupport
|
||||||
%token Invalid
|
%token Invalid
|
||||||
|
%token KeyReturn
|
||||||
|
|
||||||
/* Operator associativity */
|
/* Operator associativity */
|
||||||
/* Operators at lower line number have lower precedence */
|
/* Operators at lower line number have lower precedence */
|
||||||
|
@ -192,15 +194,30 @@ argumentlist: argumentlist '(' exprlist ')' {AST_push_node($1, $3);
|
||||||
$$ = list;};
|
$$ = 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);
|
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_NODE_PTR ident = AST_new_node(new_loc(), AST_Ident, $2);
|
||||||
AST_push_node(fun, ident);
|
AST_push_node(fun, ident);
|
||||||
AST_push_node(fun, $3);
|
AST_push_node(fun, $3);
|
||||||
AST_push_node(fun, $5);
|
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;
|
$$ = fun;
|
||||||
DEBUG("Function");};
|
DEBUG("Function");};
|
||||||
|
|
||||||
fundecl: KeyFun Ident paramlist {AST_NODE_PTR fun = AST_new_node(new_loc(), AST_Fun, NULL);
|
fundecl: KeyFun Ident paramlist {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");}
|
||||||
|
| 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_NODE_PTR ident = AST_new_node(new_loc(), AST_Ident, $2);
|
||||||
AST_push_node(fun, ident);
|
AST_push_node(fun, ident);
|
||||||
AST_push_node(fun, $3);
|
AST_push_node(fun, $3);
|
||||||
|
@ -348,9 +365,13 @@ statement: assign {$$ = $1;}
|
||||||
| definition {$$ = $1;}
|
| definition {$$ = $1;}
|
||||||
| while {$$ = $1;}
|
| while {$$ = $1;}
|
||||||
| branchfull {$$ = $1;}
|
| branchfull {$$ = $1;}
|
||||||
|
| return {$$ = $1;}
|
||||||
| funcall {$$ = $1;}
|
| funcall {$$ = $1;}
|
||||||
| boxcall{$$ = $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);
|
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, $2);
|
||||||
AST_push_node(branch, $4);
|
AST_push_node(branch, $4);
|
||||||
|
|
|
@ -92,7 +92,10 @@ fun test_matrix()
|
||||||
heapFree(matrix as ref u8)
|
heapFree(matrix as ref u8)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun main()
|
fun main(): u32
|
||||||
{
|
{
|
||||||
test_matrix()
|
test_matrix()
|
||||||
|
exit_code = 0
|
||||||
|
|
||||||
|
ret 0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue