start of the syntax tree

This commit is contained in:
Felix Müller 2024-05-12 23:33:15 +02:00
parent 730248595e
commit bb474d75ad
4 changed files with 117 additions and 66 deletions

View File

@ -35,6 +35,7 @@ void AST_init() {
INFO("filling lookup table..."); INFO("filling lookup table...");
lookup_table[AST_Stmt] = "stmt"; lookup_table[AST_Stmt] = "stmt";
lookup_table[AST_Module] = "module";
lookup_table[AST_Expr] = "expr"; lookup_table[AST_Expr] = "expr";
lookup_table[AST_Add] = "+"; lookup_table[AST_Add] = "+";

View File

@ -12,6 +12,7 @@
*/ */
enum AST_SyntaxElement_t { enum AST_SyntaxElement_t {
AST_Stmt = 0, AST_Stmt = 0,
AST_Module,
AST_Expr, AST_Expr,
// Literals // Literals
AST_Int, AST_Int,

View File

@ -1,5 +1,6 @@
%{ %{
#include <sys/log.h> #include <sys/log.h>
#include <ast/ast.h>
extern int yylineno; extern int yylineno;
int yyerror(char*); int yyerror(char*);
@ -11,6 +12,29 @@
char *string; char *string;
} }
%type <AST_NODE_PTR> expr
%type <AST_NODE_PTR> operation
%type <AST_NODE_PTR> boxaccess
%type <AST_NODE_PTR> boxselfaccess
%type <AST_NODE_PTR> statement
%type <AST_NODE_PTR> statementlist
%type <AST_NODE_PTR> assign
%type <AST_NODE_PTR> oparith
%type <AST_NODE_PTR> decl
%type <AST_NODE_PTR> definition
%type <AST_NODE_PTR> while
%type <AST_NODE_PTR> branch
%type <AST_NODE_PTR> funcall
%type <AST_NODE_PTR> boxcall
%type <AST_NODE_PTR> branchelseifs
%type <AST_NODE_PTR> branchelse
%type <AST_NODE_PTR> branchelseif
%type <AST_NODE_PTR> branchif
%type <AST_NODE_PTR> type
%type <AST_NODE_PTR> identlist
%type <AST_NODE_PTR> storagequalifier
%token KeyInt %token KeyInt
%token KeyFloat %token KeyFloat
%token KeySelf %token KeySelf
@ -65,7 +89,8 @@
%% %%
program: program programbody program: program programbody
| programbody; | programbody {AST_NODE_PTR daineMudda = AST_new_node(AST_Module, NULL);
AST_fprint_graphviz(stdout, daineMudda); };
programbody: moduleimport programbody: moduleimport
| fundef | fundef
@ -76,17 +101,17 @@ programbody: moduleimport
expr: ValFloat expr: ValFloat {$$ = AST_new_node{AST_Float, $1};}
| ValInt | ValInt {$$ = AST_new_node{AST_Int, $1};}
| ValMultistr | ValMultistr {$$ = AST_new_node{AST_String, $1};}
| ValStr | ValStr {$$ = AST_new_node{AST_String, $1};}
| Ident | Ident {$$ = AST_new_node{AST_Ident, $1};}
| operation | operation {$$ = $1;}
| boxaccess | boxaccess {$$ = $1;}
| boxselfaccess; | boxselfaccess{$$ = $1;};
exprlist: expr ',' exprlist exprlist: expr ',' exprlist
| expr; | expr ;
argumentlist: argumentlist '(' exprlist ')' argumentlist: argumentlist '(' exprlist ')'
| ; | ;
@ -120,11 +145,11 @@ boxcontent: decl { DEBUG("Box decl Content"); }
| definition { DEBUG("Box def Content"); } | definition { DEBUG("Box def Content"); }
| fundef { DEBUG("Box fun Content"); }; | fundef { DEBUG("Box fun Content"); };
boxselfaccess: KeySelf '.' Ident boxselfaccess: KeySelf '.' Ident {$$ = AST_new_node(AST_Call, NULL);}
| KeySelf '.' boxaccess; | KeySelf '.' boxaccess {$$ = AST_new_node(AST_Call, NULL);};
boxaccess: Ident '.' Ident boxaccess: Ident '.' Ident {$$ = AST_new_node(AST_Call, NULL);}
| Ident '.' boxaccess; | Ident '.' boxaccess {$$ = AST_new_node(AST_Ident, $1);};
boxcall: boxaccess argumentlist boxcall: boxaccess argumentlist
| boxselfaccess argumentlist; | boxselfaccess argumentlist;
@ -134,33 +159,48 @@ funcall: Ident argumentlist { DEBUG("Function call"); };
moduleimport: KeyImport ValStr { DEBUG("Module-Import"); }; moduleimport: KeyImport ValStr { DEBUG("Module-Import"); };
statementlist: statement statementlist statementlist: statement statementlist
| statement; | statement {$$ = $1;};
statement: assign statement: assign {$$ = $1;}
| decl | decl {$$ = $1;}
| definition | definition {$$ = $1;}
| while | while {$$ = $1;}
| branch | branch {$$ = $1;}
| funcall | funcall {$$ = $1;}
| boxcall; | boxcall{$$ = $1;};
branchif: KeyIf expr '{' statementlist '}' { DEBUG("if"); }; branchif: KeyIf expr '{' statementlist '}' { DEBUG("if"); };
branchelse: KeyElse '{' statementlist '}' { DEBUG("if-else"); }; branchelse: KeyElse '{' statementlist '}' { DEBUG("if-else"); };
branchelseif: KeyElse KeyIf expr '{' statementlist '}' { DEBUG("else-if"); }; branchelseif: KeyElse KeyIf expr '{' statementlist '}' { DEBUG("else-if"); };
branchelseifs: branchelseifs branchelseif branchelseifs: branchelseifs branchelseif {AST_NODE_PTR ifelse = AST_new_node(AST_IfElse, NULL);
| branchelseif; AST_push_node(ifelse, $1);
AST_push_node(ifelse, $2);
$$ = ifelse;}
| branchelseif {$$ = $1;};
branch: branchif branchelseifs branch: branchif branchelseifs {AST_NODE_PTR branch = AST_new_node(AST_Stmt, NULL);
| branchif branchelseifs branchelse; 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"); }; while: KeyWhile expr '{' statementlist '}' { DEBUG("while"); };
identlist: Ident ',' identlist identlist: Ident ',' identlist {AST_push_node($3, $1);
| Ident; $$ = $3;}
| Ident;
decl: type ':' identlist decl: type ':' identlist {AST_NODE_PTR decl = AST_new_node(AST_Decl, NULL);
| storagequalifier type ':' identlist 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"); }; definition: decl '=' expr { DEBUG("Definition"); };
@ -169,7 +209,12 @@ storagequalifier: KeyGlobal
| KeyStatic | KeyStatic
| KeyLocal; | 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 | boxaccess '=' expr
| boxselfaccess '=' expr ; | boxselfaccess '=' expr ;
@ -196,12 +241,15 @@ type: typekind
| sign typekind | sign typekind
| sign scale typekind; | sign scale typekind;
operation: oparith operation: oparith {$$ = $1;}
| oplogic | oplogic
| opbool | opbool
| opbit; | 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 | expr '*' expr
| expr '/' expr | expr '/' expr

View File

@ -29,42 +29,43 @@ def run_check_print_node():
assert p.returncode == 0 assert p.returncode == 0
assert """0 stmt assert """0 stmt
1 expr 1 module
2 value 2 expr
3 value 3 value
4 value 4 value
5 while 5 value
6 if 6 while
7 else if 7 if
8 else 8 else if
9 condition 9 else
10 decl 10 condition
11 assign 11 decl
12 def 12 assign
13 value 13 def
14 + 14 value
15 - 15 +
16 * 16 -
17 / 17 *
18 & 18 /
19 | 19 &
20 ^ 20 |
21 ! 21 ^
22 && 22 !
23 || 23 &&
24 ^^ 24 ||
25 !! 25 ^^
26 == 26 !!
27 > 27 ==
28 < 28 >
29 cast 29 <
30 as 30 cast
31 value 31 as
32 value 32 value
33 typedef 33 value
34 box 34 typedef
35 fun 35 box
36 value 36 fun
37 value
""" == p.stdout """ == p.stdout