From 0e5736e50c0efe428234bb97d634f356f0431706 Mon Sep 17 00:00:00 2001 From: Filleo Date: Mon, 13 May 2024 11:33:23 +0200 Subject: [PATCH] AST implementation WIP --- CMakeLists.txt | 2 +- src/ast/ast.c | 6 +++ src/ast/ast.h | 8 +++- src/yacc/parser.y | 109 ++++++++++++++++++++++++++++++------------ tests/ast/test_ast.py | 2 + 5 files changed, 95 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 12e8c66..0ffa084 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) add_custom_command(OUTPUT ${YACC_GENERATED_SOURCE_FILE} - COMMAND yacc + COMMAND bison ARGS -Wcounterexamples -d -o ${YACC_GENERATED_SOURCE_FILE} ${YACC_SOURCE_FILE} COMMENT "generate C source file for parser" VERBATIM) diff --git a/src/ast/ast.c b/src/ast/ast.c index b36d191..69d48fb 100644 --- a/src/ast/ast.c +++ b/src/ast/ast.c @@ -73,6 +73,8 @@ void AST_init() { lookup_table[AST_Typecast] = "cast"; lookup_table[AST_Transmute] = "as"; 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) { @@ -89,6 +91,10 @@ const char* AST_node_to_string(const struct AST_Node_t* node) { case AST_Macro: case AST_Import: case AST_Call: + case AST_Storage: + case AST_Typekind: + case AST_Sign: + case AST_Scale: string = node->value; break; default: diff --git a/src/ast/ast.h b/src/ast/ast.h index abd60bc..430f284 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -60,7 +60,13 @@ enum AST_SyntaxElement_t { AST_Import, // amount of variants // in this enum - AST_ELEMENT_COUNT + AST_ELEMENT_COUNT, + AST_List, + AST_Storage, + AST_Type, + AST_Typekind, + AST_Sign, + AST_Scale }; /** diff --git a/src/yacc/parser.y b/src/yacc/parser.y index 0fdffd4..ce25bc4 100644 --- a/src/yacc/parser.y +++ b/src/yacc/parser.y @@ -33,6 +33,12 @@ %type type %type identlist %type storagequalifier +%type typekind +%type scale +%type sign +%type oplogic +%type opbool +%type opbit %token KeyInt @@ -89,8 +95,8 @@ %% program: program programbody - | programbody {AST_NODE_PTR daineMudda = AST_new_node(AST_Module, NULL); - AST_fprint_graphviz(stdout, daineMudda); }; + | programbody {AST_NODE_PTR program = AST_new_node(AST_Module, NULL); + AST_fprint_graphviz(stdout, program); }; programbody: moduleimport | fundef @@ -190,7 +196,9 @@ while: KeyWhile expr '{' statementlist '}' { DEBUG("while"); }; identlist: Ident ',' identlist {AST_push_node($3, $1); $$ = $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); 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"); }; -storagequalifier: KeyGlobal - | KeyStatic - | KeyLocal; +storagequalifier: KeyGlobal {$$ = AST_new_node(AST_Storage, "global");} + | KeyStatic {$$ = AST_new_node(AST_Storage, "static");} + | KeyLocal {$$ = AST_new_node(AST_Storage, "local");}; assign: Ident '=' expr { AST_NODE_PTR assign = AST_new_node(AST_Assign, NULL); 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 | boxselfaccess '=' expr ; -sign: KeySigned - | KeyUnsigned; +sign: KeySigned {$$ = AST_new_node(AST_Sign, "signed");} + | KeyUnsigned{$$ = AST_new_node(AST_Sign, "unsigned");}; typedef: KeyType type':' Ident; -scale: scale KeyShort - | scale KeyHalf - | scale KeyLong - | scale KeyDouble - | KeyShort - | KeyHalf - | KeyLong - | KeyDouble; +scale: scale KeyShort {AST_NODE_PTR shortnode = AST_new_node(AST_Scale, "short"); + AST_push_node($1, shortnode); + $$ = $1;} + | scale KeyHalf {AST_NODE_PTR shortnode = AST_new_node(AST_Scale, "half"); + AST_push_node($1, shortnode); + $$ = $1;} + | scale KeyLong {AST_NODE_PTR shortnode = AST_new_node(AST_Scale, "long"); + 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 - | KeyInt - | KeyFloat; +typekind: Ident {$$ = AST_new_node(AST_Typekind, $1);} + | KeyInt {$$ = AST_new_node(AST_Typekind, "int");} + | KeyFloat {$$ = AST_new_node(AST_Typekind, "float");}; -type: typekind - | scale typekind - | sign typekind - | sign scale typekind; +type: typekind {AST_NODE_PTR type = AST_new_node(AST_type, NULL); + AST_push_node(type, $1); + $$ = type;} + | 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;} - | oplogic - | opbool - | opbit; + | oplogic {$$ = $1;} + | opbool {$$ = $1;} + | 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, $3); $$ = add;} - | expr '-' expr - | expr '*' expr - | expr '/' expr + | expr '-' expr {AST_NODE_PTR subtract = AST_new_node{AST_Sub, NULL}; + AST_push_node(subtract, $1); + 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 '*'; oplogic: expr OpEquals expr diff --git a/tests/ast/test_ast.py b/tests/ast/test_ast.py index 926671d..91d637a 100644 --- a/tests/ast/test_ast.py +++ b/tests/ast/test_ast.py @@ -66,6 +66,8 @@ def run_check_print_node(): 35 box 36 fun 37 value +38 list +39 storage """ == p.stdout