From a60337264c2551fffc2b5aeea12f40a06b7cb0e9 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 17 Feb 2017 10:55:21 +0000 Subject: Working variables finally by adding mutable --- c_lexer/test/output.txt | 9 --------- c_parser/include/ast.hpp | 1 + c_parser/include/ast_base.hpp | 1 + c_parser/include/ast_declaration.hpp | 26 +++++++++++++++++++------- c_parser/include/ast_primitives.hpp | 4 +++- c_parser/include/ast_top.hpp | 3 ++- c_parser/src/c_parser.y | 14 +++++++------- c_parser/src/parser_main.cpp | 4 +++- c_parser/test/output.xml | 13 +++++++++++++ c_parser/test/test_parser.c | 3 ++- test_parser.sh | 2 +- 11 files changed, 52 insertions(+), 28 deletions(-) delete mode 100644 c_lexer/test/output.txt create mode 100644 c_parser/test/output.xml diff --git a/c_lexer/test/output.txt b/c_lexer/test/output.txt deleted file mode 100644 index 91936e9..0000000 --- a/c_lexer/test/output.txt +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/c_parser/include/ast.hpp b/c_parser/include/ast.hpp index a0889ae..e17ab49 100644 --- a/c_parser/include/ast.hpp +++ b/c_parser/include/ast.hpp @@ -3,6 +3,7 @@ #include "ast_base.hpp" #include "ast_declaration.hpp" +#include "ast_primitives.hpp" #include "ast_top.hpp" ast_Top *parseAST(); diff --git a/c_parser/include/ast_base.hpp b/c_parser/include/ast_base.hpp index 432a54b..c4293a8 100644 --- a/c_parser/include/ast_base.hpp +++ b/c_parser/include/ast_base.hpp @@ -10,6 +10,7 @@ public: virtual ~ast_Base() {} virtual void print() const = 0; + virtual void push(const ast_Base* var) const = 0; }; #endif diff --git a/c_parser/include/ast_declaration.hpp b/c_parser/include/ast_declaration.hpp index 98fe255..50cff2a 100644 --- a/c_parser/include/ast_declaration.hpp +++ b/c_parser/include/ast_declaration.hpp @@ -3,23 +3,35 @@ #include "ast.hpp" +#include + +// Declaration that holds a list of declarations + class ast_Declaration : public ast_Base { private: public: - virtual void print() const override = 0; + virtual void print() const = 0; }; -class ast_VariableDeclaration : public ast_Declartaion { +class ast_VariableDeclaration : public ast_Declaration { private: - const std::string id; - const std::string type; + mutable std::vector var_list; public: - ast_VariableDeclaration(const std::string& _id, const std::string& _type) : - id(_id), type(_type) {} + ast_VariableDeclaration(const ast_Base* _var) { + var_list.push_back(_var); + } + + virtual void print() const { + for(int i = 0; i < var_list.size(); ++i) { + var_list[i]->print(); + } + } - virtual void print() const override {} + virtual void push(const ast_Base* var) const { + var_list.push_back(var); + } }; #endif diff --git a/c_parser/include/ast_primitives.hpp b/c_parser/include/ast_primitives.hpp index fbcb1b4..21629e7 100644 --- a/c_parser/include/ast_primitives.hpp +++ b/c_parser/include/ast_primitives.hpp @@ -11,9 +11,11 @@ private: public: ast_Variable(const std::string& _id) : id(_id) {} - virtual void print() const override { + virtual void print() const { std::cout << "" << std::endl; } + + virtual void push(const ast_Base* var) const {} }; #endif diff --git a/c_parser/include/ast_top.hpp b/c_parser/include/ast_top.hpp index ff1cffa..142dfb8 100644 --- a/c_parser/include/ast_top.hpp +++ b/c_parser/include/ast_top.hpp @@ -12,10 +12,11 @@ public: ast_vec[i]->print(); } } - + void push(const ast_Base *stmnt) { ast_vec.push_back(stmnt); } + private: std::vector ast_vec; }; diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y index 5429f76..9171a1b 100644 --- a/c_parser/src/c_parser.y +++ b/c_parser/src/c_parser.y @@ -37,12 +37,12 @@ ROOT : EXT_DEF { ; } // EXTERNAL DEFINITION -EXT_DEF : EXT_DECLARATION { ; } - | EXT_DEF EXT_DECLARATION { $$ = $2; } +EXT_DEF : EXT_DECLARATION { g_root->push($1); } + | EXT_DEF EXT_DECLARATION { g_root->push($2); } EXT_DECLARATION : DECLARATION_SPEC EXT_DECLARATION_2 { $$ = $2; } -EXT_DECLARATION_2 : DECLARATION { ; } +EXT_DECLARATION_2 : DECLARATION { $$ = $1; } | FUNC_DEF { ; } // FUNCTION DEFINITION @@ -51,7 +51,7 @@ FUNC_DEF : T_IDENTIFIER T_LRB T_RRB T_LCB T_RCB { ; } // DECLARATION -DECLARATION : INIT_DECLARATOR_LIST T_SC { ; } +DECLARATION : INIT_DECLARATOR_LIST T_SC { $$ = $1; } DECLARATION_SPEC : DECLARATION_SPEC_T { ; } | DECLARATION_SPEC_T DECLARATION_SPEC { ; } @@ -60,13 +60,13 @@ DECLARATION_SPEC_T : T_TYPE_SPEC { ; } | T_TYPE_QUAL { ; } | T_STRG_SPEC { ; } -INIT_DECLARATOR_LIST : INIT_DECLARATOR { g_root->push($1); } - | INIT_DECLARATOR_LIST T_CMA INIT_DECLARATOR { g_root->push($3); } +INIT_DECLARATOR_LIST : INIT_DECLARATOR { $$ = new ast_VariableDeclaration($1); } + | INIT_DECLARATOR_LIST T_CMA INIT_DECLARATOR { $$->push($3); } INIT_DECLARATOR : DECLARATOR { ; } | DECLARATOR T_EQ INITIALIZER { ; } -DECLARATOR : T_IDENTIFIER {$$ = new ast_Declaration(*$1); } +DECLARATOR : T_IDENTIFIER {$$ = new ast_Variable(*$1); } INITIALIZER : T_INT_CONST { ; } diff --git a/c_parser/src/parser_main.cpp b/c_parser/src/parser_main.cpp index 02f3b3f..9626334 100644 --- a/c_parser/src/parser_main.cpp +++ b/c_parser/src/parser_main.cpp @@ -5,9 +5,11 @@ int main(int argc, char *argv[]) { ast_Top *ast = parseAST(); + std::cout << "" << std::endl << "" << std::endl; + ast->print(); - std::cout << std::endl; + std::cout << "" << std::endl; return 0; } diff --git a/c_parser/test/output.xml b/c_parser/test/output.xml new file mode 100644 index 0000000..852ed49 --- /dev/null +++ b/c_parser/test/output.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/c_parser/test/test_parser.c b/c_parser/test/test_parser.c index a685aff..557fd56 100644 --- a/c_parser/test/test_parser.c +++ b/c_parser/test/test_parser.c @@ -2,4 +2,5 @@ int a; int b; int c; int yann, is, the, best; -int hello = 0; +int d = 0; +int hello = 123, asd = 123; diff --git a/test_parser.sh b/test_parser.sh index d313401..5f8e606 100755 --- a/test_parser.sh +++ b/test_parser.sh @@ -15,4 +15,4 @@ fi echo "=========================================" echo " Testing lexer" -cat c_parser/test/test_parser.c | ./bin/c_parser | tee c_lexer/test/output.txt +cat c_parser/test/test_parser.c | ./bin/c_parser | tee c_parser/test/output.xml -- cgit