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...");
lookup_table[AST_Stmt] = "stmt";
lookup_table[AST_Module] = "module";
lookup_table[AST_Expr] = "expr";
lookup_table[AST_Add] = "+";

View File

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

View File

@ -1,5 +1,6 @@
%{
#include <sys/log.h>
#include <ast/ast.h>
extern int yylineno;
int yyerror(char*);
@ -11,6 +12,29 @@
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 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

View File

@ -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