AST implementation WIP

This commit is contained in:
Felix Müller 2024-05-13 11:33:23 +02:00
parent bb474d75ad
commit 0e5736e50c
5 changed files with 95 additions and 32 deletions

View File

@ -61,7 +61,7 @@ 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)
add_custom_command(OUTPUT ${YACC_GENERATED_SOURCE_FILE} add_custom_command(OUTPUT ${YACC_GENERATED_SOURCE_FILE}
COMMAND yacc COMMAND bison
ARGS -Wcounterexamples -d -o ${YACC_GENERATED_SOURCE_FILE} ${YACC_SOURCE_FILE} ARGS -Wcounterexamples -d -o ${YACC_GENERATED_SOURCE_FILE} ${YACC_SOURCE_FILE}
COMMENT "generate C source file for parser" COMMENT "generate C source file for parser"
VERBATIM) VERBATIM)

View File

@ -73,6 +73,8 @@ void AST_init() {
lookup_table[AST_Typecast] = "cast"; lookup_table[AST_Typecast] = "cast";
lookup_table[AST_Transmute] = "as"; lookup_table[AST_Transmute] = "as";
lookup_table[AST_Condition] = "condition"; lookup_table[AST_Condition] = "condition";
lookup_table[AST_List] = "list";
lookup_table[AST_Type] = "type";
} }
const char* AST_node_to_string(const struct AST_Node_t* node) { const char* AST_node_to_string(const struct AST_Node_t* node) {
@ -89,6 +91,10 @@ const char* AST_node_to_string(const struct AST_Node_t* node) {
case AST_Macro: case AST_Macro:
case AST_Import: case AST_Import:
case AST_Call: case AST_Call:
case AST_Storage:
case AST_Typekind:
case AST_Sign:
case AST_Scale:
string = node->value; string = node->value;
break; break;
default: default:

View File

@ -60,7 +60,13 @@ enum AST_SyntaxElement_t {
AST_Import, AST_Import,
// amount of variants // amount of variants
// in this enum // in this enum
AST_ELEMENT_COUNT AST_ELEMENT_COUNT,
AST_List,
AST_Storage,
AST_Type,
AST_Typekind,
AST_Sign,
AST_Scale
}; };
/** /**

View File

@ -33,6 +33,12 @@
%type <AST_NODE_PTR> type %type <AST_NODE_PTR> type
%type <AST_NODE_PTR> identlist %type <AST_NODE_PTR> identlist
%type <AST_NODE_PTR> storagequalifier %type <AST_NODE_PTR> storagequalifier
%type <AST_NODE_PTR> typekind
%type <AST_NODE_PTR> scale
%type <AST_NODE_PTR> sign
%type <AST_NODE_PTR> oplogic
%type <AST_NODE_PTR> opbool
%type <AST_NODE_PTR> opbit
%token KeyInt %token KeyInt
@ -89,8 +95,8 @@
%% %%
program: program programbody program: program programbody
| programbody {AST_NODE_PTR daineMudda = AST_new_node(AST_Module, NULL); | programbody {AST_NODE_PTR program = AST_new_node(AST_Module, NULL);
AST_fprint_graphviz(stdout, daineMudda); }; AST_fprint_graphviz(stdout, program); };
programbody: moduleimport programbody: moduleimport
| fundef | fundef
@ -190,7 +196,9 @@ while: KeyWhile expr '{' statementlist '}' { DEBUG("while"); };
identlist: Ident ',' identlist {AST_push_node($3, $1); identlist: Ident ',' identlist {AST_push_node($3, $1);
$$ = $3;} $$ = $3;}
| Ident; | Ident {AST_NODE_PTR list = AST_new_node(AST_List, NULL);
AST_push_node(list, $1);
$$ = list;};
decl: type ':' identlist {AST_NODE_PTR decl = AST_new_node(AST_Decl, NULL); decl: type ':' identlist {AST_NODE_PTR decl = AST_new_node(AST_Decl, NULL);
AST_push_node(decl, $1); AST_push_node(decl, $1);
@ -205,9 +213,9 @@ decl: type ':' identlist {AST_NODE_PTR decl = AST_new_node(AST_Decl, NULL);
definition: decl '=' expr { DEBUG("Definition"); }; definition: decl '=' expr { DEBUG("Definition"); };
storagequalifier: KeyGlobal storagequalifier: KeyGlobal {$$ = AST_new_node(AST_Storage, "global");}
| KeyStatic | KeyStatic {$$ = AST_new_node(AST_Storage, "static");}
| KeyLocal; | KeyLocal {$$ = AST_new_node(AST_Storage, "local");};
assign: Ident '=' expr { AST_NODE_PTR assign = AST_new_node(AST_Assign, NULL); assign: Ident '=' expr { AST_NODE_PTR assign = AST_new_node(AST_Assign, NULL);
AST_NODE_PTR ident = AST_new_node(AST_Ident, $1); AST_NODE_PTR ident = AST_new_node(AST_Ident, $1);
@ -218,41 +226,82 @@ assign: Ident '=' expr { AST_NODE_PTR assign = AST_new_node(AST_Assign, NULL);
| boxaccess '=' expr | boxaccess '=' expr
| boxselfaccess '=' expr ; | boxselfaccess '=' expr ;
sign: KeySigned sign: KeySigned {$$ = AST_new_node(AST_Sign, "signed");}
| KeyUnsigned; | KeyUnsigned{$$ = AST_new_node(AST_Sign, "unsigned");};
typedef: KeyType type':' Ident; typedef: KeyType type':' Ident;
scale: scale KeyShort scale: scale KeyShort {AST_NODE_PTR shortnode = AST_new_node(AST_Scale, "short");
| scale KeyHalf AST_push_node($1, shortnode);
| scale KeyLong $$ = $1;}
| scale KeyDouble | scale KeyHalf {AST_NODE_PTR shortnode = AST_new_node(AST_Scale, "half");
| KeyShort AST_push_node($1, shortnode);
| KeyHalf $$ = $1;}
| KeyLong | scale KeyLong {AST_NODE_PTR shortnode = AST_new_node(AST_Scale, "long");
| KeyDouble; AST_push_node($1, shortnode);
$$ = $1;}
| scale KeyDouble {AST_NODE_PTR shortnode = AST_new_node(AST_Scale, "double");
AST_push_node($1, shortnode);
$$ = $1;}
| KeyShort {AST_NODE_PTR scale = AST_new_node(AST_List, NULL);
AST_NODE_PTR shortnode = AST_new_node(AST_Scale, "short");
AST_push_node(scale, shortnode);
$$ = scale;}
| KeyHalf {AST_NODE_PTR scale = AST_new_node(AST_List, NULL);
AST_NODE_PTR shortnode = AST_new_node(AST_Scale, "half");
AST_push_node(scale, shortnode);
$$ = scale;}
| KeyLong {AST_NODE_PTR scale = AST_new_node(AST_List, NULL);
AST_NODE_PTR shortnode = AST_new_node(AST_Scale, "long");
AST_push_node(scale, shortnode);
$$ = scale;}
| KeyDouble {AST_NODE_PTR scale = AST_new_node(AST_List, NULL);
AST_NODE_PTR shortnode = AST_new_node(AST_Scale, "double");
AST_push_node(scale, shortnode);
$$ = scale;};
typekind: Ident typekind: Ident {$$ = AST_new_node(AST_Typekind, $1);}
| KeyInt | KeyInt {$$ = AST_new_node(AST_Typekind, "int");}
| KeyFloat; | KeyFloat {$$ = AST_new_node(AST_Typekind, "float");};
type: typekind type: typekind {AST_NODE_PTR type = AST_new_node(AST_type, NULL);
| scale typekind AST_push_node(type, $1);
| sign typekind $$ = type;}
| sign scale typekind; | scale typekind {AST_NODE_PTR type = AST_new_node(AST_type, NULL);
AST_push_node(type, $1);
AST_push_node(type, $2);
$$ = type;}
| sign typekind {AST_NODE_PTR type = AST_new_node(AST_type, NULL);
AST_push_node(type, $1);
AST_push_node(type, $2);
$$ = type;}
| sign scale typekind {AST_NODE_PTR type = AST_new_node(AST_type, NULL);
AST_push_node(type, $1);
AST_push_node(type, $2);
AST_push_node(type, $3);
$$ = type;};
operation: oparith {$$ = $1;} operation: oparith {$$ = $1;}
| oplogic | oplogic {$$ = $1;}
| opbool | opbool {$$ = $1;}
| opbit; | opbit {$$ = $1;};
oparith: expr '+' expr {AST_NODE_PTR add = AST_new_node{AST_add, NULL}; oparith: expr '+' expr {AST_NODE_PTR add = AST_new_node{AST_Add, NULL};
AST_push_node(add, $1); AST_push_node(add, $1);
AST_push_node(add, $3); AST_push_node(add, $3);
$$ = add;} $$ = add;}
| expr '-' expr | expr '-' expr {AST_NODE_PTR subtract = AST_new_node{AST_Sub, NULL};
| expr '*' expr AST_push_node(subtract, $1);
| expr '/' expr AST_push_node(subtract, $3);
$$ = subtract;}
| expr '*' expr {AST_NODE_PTR mul = AST_new_node{AST_Mul, NULL};
AST_push_node(mul, $1);
AST_push_node(mul, $3);
$$ = mul;}
| expr '/' expr {AST_NODE_PTR div = AST_new_node{AST_Div, NULL};
AST_push_node(div, $1);
AST_push_node(div, $3);
$$ = div;}
| '-' expr %prec '*'; | '-' expr %prec '*';
oplogic: expr OpEquals expr oplogic: expr OpEquals expr

View File

@ -66,6 +66,8 @@ def run_check_print_node():
35 box 35 box
36 fun 36 fun
37 value 37 value
38 list
39 storage
""" == p.stdout """ == p.stdout