diff --git a/src/ast/ast.c b/src/ast/ast.c index e503e1e..892af1f 100644 --- a/src/ast/ast.c +++ b/src/ast/ast.c @@ -70,8 +70,8 @@ void AST_init() { lookup_table[AST_Fun] = "fun"; lookup_table[AST_Call] = "funcall"; - lookup_table[AST_Typecast] = "cast"; - lookup_table[AST_Transmute] = "as"; + lookup_table[AST_Typecast] = "typecast"; + lookup_table[AST_Transmute] = "transmute"; lookup_table[AST_Condition] = "condition"; lookup_table[AST_List] = "list"; lookup_table[AST_ExprList] = "expr list"; diff --git a/src/yacc/parser.y b/src/yacc/parser.y index d8bf1a1..753c7bc 100644 --- a/src/yacc/parser.y +++ b/src/yacc/parser.y @@ -63,6 +63,8 @@ %type paramdecl %type boxbody %type boxcontent +%type typecast +%type reinterpretcast %type program @@ -126,6 +128,8 @@ %left '+' '-' %left '*' '/' %left OpNot OpBitnot +%left KeyAs +%left '(' ')' %% program: program programbody {AST_push_node(root, $2);} @@ -147,7 +151,9 @@ expr: ValFloat {$$ = AST_new_node(AST_Float, $1);} | Ident {$$ = AST_new_node(AST_Ident, $1);} | operation {$$ = $1;} | boxaccess {$$ = $1;} - | boxselfaccess{$$ = $1;}; + | boxselfaccess{$$ = $1;} + | typecast{$$ = $1;} + | reinterpretcast{$$ = $1;} exprlist: expr ',' exprlist {AST_push_node($3, $1); $$ = $3;} @@ -275,6 +281,20 @@ boxcall: boxaccess argumentlist {AST_NODE_PTR boxcall = AST_new_node(AST_Call, N AST_push_node(boxcall, $1); AST_push_node(boxcall, $2); $$ = boxcall;}; + + +typecast: expr KeyAs type %prec KeyAs {AST_NODE_PTR cast = AST_new_node(AST_Typecast, NULL); + AST_push_node(cast, $1); + AST_push_node(cast, $3); + $$ = cast; + DEBUG("Type-Cast"); }; + +reinterpretcast: '(' type ')' expr { AST_NODE_PTR cast = AST_new_node(AST_Transmute, NULL); + AST_push_node(cast, $4); + AST_push_node(cast, $2); + $$ = cast; + DEBUG("Reinterpret-Cast"); }; + funcall: Ident argumentlist {AST_NODE_PTR funcall = AST_new_node(AST_Call, NULL); AST_NODE_PTR ident = AST_new_node(AST_Ident, $1); @@ -364,6 +384,7 @@ assign: Ident '=' expr { AST_NODE_PTR assign = AST_new_node(AST_Assign, NULL); AST_push_node(assign, $3); $$ = assign; DEBUG("Assignment"); } + | boxaccess '=' expr | boxselfaccess '=' expr ; diff --git a/tests/ast/test_ast.py b/tests/ast/test_ast.py index 28407f9..e3e5d48 100644 --- a/tests/ast/test_ast.py +++ b/tests/ast/test_ast.py @@ -58,8 +58,8 @@ def run_check_print_node(): 27 == 28 > 29 < -30 cast -31 as +30 typecast +31 transmute 32 funcall 33 value 34 typedef