From bb474d75ad9c1c34a1aac5ae7549304b38f3cc4e Mon Sep 17 00:00:00 2001 From: Filleo Date: Sun, 12 May 2024 23:33:15 +0200 Subject: [PATCH] start of the syntax tree --- src/ast/ast.c | 1 + src/ast/ast.h | 1 + src/yacc/parser.y | 114 ++++++++++++++++++++++++++++++------------ tests/ast/test_ast.py | 67 +++++++++++++------------ 4 files changed, 117 insertions(+), 66 deletions(-) diff --git a/src/ast/ast.c b/src/ast/ast.c index 233e8da..b36d191 100644 --- a/src/ast/ast.c +++ b/src/ast/ast.c @@ -35,6 +35,7 @@ void AST_init() { INFO("filling lookup table..."); lookup_table[AST_Stmt] = "stmt"; + lookup_table[AST_Module] = "module"; lookup_table[AST_Expr] = "expr"; lookup_table[AST_Add] = "+"; diff --git a/src/ast/ast.h b/src/ast/ast.h index 5129765..abd60bc 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -12,6 +12,7 @@ */ enum AST_SyntaxElement_t { AST_Stmt = 0, + AST_Module, AST_Expr, // Literals AST_Int, diff --git a/src/yacc/parser.y b/src/yacc/parser.y index 36c5931..0fdffd4 100644 --- a/src/yacc/parser.y +++ b/src/yacc/parser.y @@ -1,5 +1,6 @@ %{ #include + #include extern int yylineno; int yyerror(char*); @@ -11,6 +12,29 @@ char *string; } +%type expr +%type operation +%type boxaccess +%type boxselfaccess +%type statement +%type statementlist +%type assign +%type oparith +%type decl +%type definition +%type while +%type branch +%type funcall +%type boxcall +%type branchelseifs +%type branchelse +%type branchelseif +%type branchif +%type type +%type identlist +%type storagequalifier + + %token KeyInt %token KeyFloat %token KeySelf @@ -65,7 +89,8 @@ %% program: program programbody - | programbody; + | programbody {AST_NODE_PTR daineMudda = AST_new_node(AST_Module, NULL); + AST_fprint_graphviz(stdout, daineMudda); }; programbody: moduleimport | fundef @@ -76,17 +101,17 @@ programbody: moduleimport -expr: ValFloat - | ValInt - | ValMultistr - | ValStr - | Ident - | operation - | boxaccess - | boxselfaccess; +expr: ValFloat {$$ = AST_new_node{AST_Float, $1};} + | ValInt {$$ = AST_new_node{AST_Int, $1};} + | ValMultistr {$$ = AST_new_node{AST_String, $1};} + | ValStr {$$ = AST_new_node{AST_String, $1};} + | Ident {$$ = AST_new_node{AST_Ident, $1};} + | operation {$$ = $1;} + | boxaccess {$$ = $1;} + | boxselfaccess{$$ = $1;}; exprlist: expr ',' exprlist - | expr; + | expr ; argumentlist: argumentlist '(' exprlist ')' | ; @@ -120,11 +145,11 @@ boxcontent: decl { DEBUG("Box decl Content"); } | definition { DEBUG("Box def Content"); } | fundef { DEBUG("Box fun Content"); }; -boxselfaccess: KeySelf '.' Ident - | KeySelf '.' boxaccess; +boxselfaccess: KeySelf '.' Ident {$$ = AST_new_node(AST_Call, NULL);} + | KeySelf '.' boxaccess {$$ = AST_new_node(AST_Call, NULL);}; -boxaccess: Ident '.' Ident - | Ident '.' boxaccess; +boxaccess: Ident '.' Ident {$$ = AST_new_node(AST_Call, NULL);} + | Ident '.' boxaccess {$$ = AST_new_node(AST_Ident, $1);}; boxcall: boxaccess argumentlist | boxselfaccess argumentlist; @@ -134,33 +159,48 @@ funcall: Ident argumentlist { DEBUG("Function call"); }; moduleimport: KeyImport ValStr { DEBUG("Module-Import"); }; statementlist: statement statementlist - | statement; + | statement {$$ = $1;}; -statement: assign - | decl - | definition - | while - | branch - | funcall - | boxcall; +statement: assign {$$ = $1;} + | decl {$$ = $1;} + | definition {$$ = $1;} + | while {$$ = $1;} + | branch {$$ = $1;} + | funcall {$$ = $1;} + | boxcall{$$ = $1;}; branchif: KeyIf expr '{' statementlist '}' { DEBUG("if"); }; branchelse: KeyElse '{' statementlist '}' { DEBUG("if-else"); }; branchelseif: KeyElse KeyIf expr '{' statementlist '}' { DEBUG("else-if"); }; -branchelseifs: branchelseifs branchelseif - | branchelseif; +branchelseifs: branchelseifs branchelseif {AST_NODE_PTR ifelse = AST_new_node(AST_IfElse, NULL); + AST_push_node(ifelse, $1); + AST_push_node(ifelse, $2); + $$ = ifelse;} + | branchelseif {$$ = $1;}; -branch: branchif branchelseifs - | branchif branchelseifs branchelse; +branch: branchif branchelseifs {AST_NODE_PTR branch = AST_new_node(AST_Stmt, NULL); + AST_push_node(branch, $1); + AST_push_node(branch, $2); + $$ = branch;} + + | branchif branchelseifs branchelse { AST_NODE_PTR branch = AST_new_node(AST_IF, NULL);}; while: KeyWhile expr '{' statementlist '}' { DEBUG("while"); }; -identlist: Ident ',' identlist - | Ident; +identlist: Ident ',' identlist {AST_push_node($3, $1); + $$ = $3;} + | Ident; -decl: type ':' identlist - | storagequalifier type ':' identlist +decl: type ':' identlist {AST_NODE_PTR decl = AST_new_node(AST_Decl, NULL); + AST_push_node(decl, $1); + AST_push_node(decl, $3); + $$ = decl;} + | storagequalifier type ':' identlist {AST_NODE_PTR decl = AST_new_node(AST_Decl, NULL); + AST_push_node(decl, $1); + AST_push_node(decl, $2); + AST_push_node(decl, $4); + $$ = decl;} definition: decl '=' expr { DEBUG("Definition"); }; @@ -169,7 +209,12 @@ storagequalifier: KeyGlobal | KeyStatic | KeyLocal; -assign: Ident '=' expr { DEBUG("Assignment"); } +assign: Ident '=' expr { AST_NODE_PTR assign = AST_new_node(AST_Assign, NULL); + AST_NODE_PTR ident = AST_new_node(AST_Ident, $1); + AST_push_node(assign, ident); + AST_push_node(assign, $3); + $$ = assign; + DEBUG("Assignment"); } | boxaccess '=' expr | boxselfaccess '=' expr ; @@ -196,12 +241,15 @@ type: typekind | sign typekind | sign scale typekind; -operation: oparith +operation: oparith {$$ = $1;} | oplogic | opbool | opbit; -oparith: expr '+' expr +oparith: expr '+' expr {AST_NODE_PTR add = AST_new_node{AST_add, NULL}; + AST_push_node(add, $1); + AST_push_node(add, $3); + $$ = add;} | expr '-' expr | expr '*' expr | expr '/' expr diff --git a/tests/ast/test_ast.py b/tests/ast/test_ast.py index 0f9b757..926671d 100644 --- a/tests/ast/test_ast.py +++ b/tests/ast/test_ast.py @@ -29,42 +29,43 @@ def run_check_print_node(): assert p.returncode == 0 assert """0 stmt -1 expr -2 value +1 module +2 expr 3 value 4 value -5 while -6 if -7 else if -8 else -9 condition -10 decl -11 assign -12 def -13 value -14 + -15 - -16 * -17 / -18 & -19 | -20 ^ -21 ! -22 && -23 || -24 ^^ -25 !! -26 == -27 > -28 < -29 cast -30 as -31 value +5 value +6 while +7 if +8 else if +9 else +10 condition +11 decl +12 assign +13 def +14 value +15 + +16 - +17 * +18 / +19 & +20 | +21 ^ +22 ! +23 && +24 || +25 ^^ +26 !! +27 == +28 > +29 < +30 cast +31 as 32 value -33 typedef -34 box -35 fun -36 value +33 value +34 typedef +35 box +36 fun +37 value """ == p.stdout