diff --git a/src/parser.y b/src/parser.y index c28d352..d7e6fbd 100644 --- a/src/parser.y +++ b/src/parser.y @@ -83,7 +83,7 @@ declarator direct_declarator : IDENTIFIER { - $$ = new Identifier(*$1); + $$ = new Identifier(std::move(*$1)); delete $1; } | direct_declarator '(' ')' { diff --git a/src/parser_full.y.example b/src/parser_full.y.example index 1861858..f52f0b5 100644 --- a/src/parser_full.y.example +++ b/src/parser_full.y.example @@ -3,20 +3,24 @@ %code requires{ #include "ast.hpp" + using namespace ast; + extern Node* g_root; extern FILE* yyin; int yylex(void); - void yyerror(const char *); + void yyerror(const char*); + int yylex_destroy(void); } // Represents the value associated with any kind of AST node. %union{ - Node* node; - NodeList* nodes; - int number_int; - double number_float; - std::string* string; - yytokentype token; + Node* node; + NodeList* node_list; + int number_int; + double number_float; + std::string* string; + TypeSpecifier type_specifier; + yytokentype token; } %token IDENTIFIER INT_CONSTANT FLOAT_CONSTANT STRING_LITERAL @@ -36,13 +40,16 @@ %type identifier_list type_name abstract_declarator direct_abstract_declarator initializer initializer_list statement labeled_statement %type compound_statement declaration_list expression_statement selection_statement iteration_statement jump_statement -%type statement_list +%type statement_list %type unary_operator assignment_operator storage_class_specifier %type INT_CONSTANT STRING_LITERAL %type FLOAT_CONSTANT %type IDENTIFIER +%type type_specifier +// TODO: Make a better type for this (only needed for advanced features) +%type declaration_specifiers %start ROOT @@ -64,7 +71,7 @@ external_declaration function_definition : declaration_specifiers declarator declaration_list compound_statement | declaration_specifiers declarator compound_statement { - $$ = new FunctionDefinition($1, $2, $3); + $$ = new FunctionDefinition($1, NodePtr($2), NodePtr($3)); } | declarator declaration_list compound_statement | declarator compound_statement @@ -245,9 +252,7 @@ type_specifier : VOID | CHAR | SHORT - | INT { - $$ = new TypeSpecifier("int"); - } + | INT { $$ = TypeSpecifier::INT; } | LONG | FLOAT | DOUBLE @@ -312,7 +317,7 @@ declarator direct_declarator : IDENTIFIER { - $$ = new Identifier(*$1); + $$ = new Identifier(std::move(*$1)); delete $1; } | '(' declarator ')' @@ -321,7 +326,7 @@ direct_declarator | direct_declarator '(' parameter_list ')' | direct_declarator '(' identifier_list ')' | direct_declarator '(' ')' { - $$ = new DirectDeclarator($1); + $$ = new DirectDeclarator(NodePtr($1)); } ; @@ -419,8 +424,8 @@ declaration_list ; statement_list - : statement { $$ = new NodeList($1); } - | statement_list statement { $1->PushBack($2); $$=$1; } + : statement { $$ = new NodeList(NodePtr($1)); } + | statement_list statement { $1->PushBack(NodePtr($2)); $$=$1; } ; expression_statement @@ -449,7 +454,7 @@ jump_statement $$ = new ReturnStatement(nullptr); } | RETURN expression ';' { - $$ = new ReturnStatement($2); + $$ = new ReturnStatement(NodePtr($2)); } ; @@ -457,9 +462,9 @@ jump_statement %% -Node *g_root; +Node* g_root; -Node *ParseAST(std::string file_name) +NodePtr ParseAST(std::string file_name) { yyin = fopen(file_name.c_str(), "r"); if(yyin == NULL){ @@ -468,5 +473,7 @@ Node *ParseAST(std::string file_name) } g_root = nullptr; yyparse(); - return g_root; + fclose(yyin); + yylex_destroy(); + return NodePtr(g_root); }