diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-17 15:44:24 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-17 15:44:24 +0000 |
commit | b81f60d8b523260526af15361d4ce0ac4a7757c8 (patch) | |
tree | f2e779a6ed0f0879875e5a0482f2dfcafeaffef3 /c_parser | |
parent | 419384dbd0efced3af6c10ccfc2eee4ff6ca22c7 (diff) | |
download | Compiler-b81f60d8b523260526af15361d4ce0ac4a7757c8.tar.gz Compiler-b81f60d8b523260526af15361d4ce0ac4a7757c8.zip |
Finished functions for now
Diffstat (limited to 'c_parser')
-rw-r--r-- | c_parser/include/ast.hpp | 1 | ||||
-rw-r--r-- | c_parser/include/ast_function.hpp | 5 | ||||
-rw-r--r-- | c_parser/include/ast_statement.hpp | 37 | ||||
-rw-r--r-- | c_parser/src/c_parser.y | 4 | ||||
-rw-r--r-- | c_parser/test/output.xml | 18 | ||||
-rw-r--r-- | c_parser/test/test_parser.c | 10 |
6 files changed, 71 insertions, 4 deletions
diff --git a/c_parser/include/ast.hpp b/c_parser/include/ast.hpp index 7292af2..787e928 100644 --- a/c_parser/include/ast.hpp +++ b/c_parser/include/ast.hpp @@ -2,6 +2,7 @@ #define AST_HPP #include "ast_base.hpp" +#include "ast_statement.hpp" #include "ast_function.hpp" #include "ast_declaration.hpp" #include "ast_primitives.hpp" diff --git a/c_parser/include/ast_function.hpp b/c_parser/include/ast_function.hpp index 9383601..86230d1 100644 --- a/c_parser/include/ast_function.hpp +++ b/c_parser/include/ast_function.hpp @@ -10,12 +10,15 @@ class ast_Function : public ast_Base { private: std::string id; const ast_Base* param; + const ast_Base* comp_statement; public: - ast_Function(const std::string& _id, const ast_Base* _param) : id(_id), param(_param) {} + ast_Function(const std::string& _id, const ast_Base* _param, const ast_Base* _comp_statement) : + id(_id), param(_param), comp_statement(_comp_statement) {} virtual void print() const { std::cout << "<Function id=\"" << id << "\">" << std::endl; param->print(); + comp_statement->print(); std::cout << "</Function>" << std::endl; } diff --git a/c_parser/include/ast_statement.hpp b/c_parser/include/ast_statement.hpp new file mode 100644 index 0000000..725308b --- /dev/null +++ b/c_parser/include/ast_statement.hpp @@ -0,0 +1,37 @@ +#ifndef AST_STATEMENT_HPP +#define AST_STATEMENT_HPP + +class ast_Statement : public ast_Base { +protected: + mutable std::vector<const ast_Base*> ast_list; + +public: + ast_Statement(const ast_Base* _el) { + ast_list.push_back(_el); + } + + virtual void print() const { + for(size_t i = 0; i < ast_list.size(); ++i) { + ast_list[i]->print(); + } + } + + virtual void push(const ast_Base* _var) const { + ast_list.push_back(_var); + } +}; + +class ast_CompoundStatement : public ast_Statement { +public: + ast_CompoundStatement(const ast_Base* _el) : ast_Statement(_el) {} + + virtual void print() const override { + std::cout << "<Scope>" << std::endl; + for(size_t i = 0; i < ast_list.size(); ++i) { + ast_list[i]->print(); + } + std::cout << "</Scope>" << std::endl; + } +}; + +#endif diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y index 95db2c9..13f3d13 100644 --- a/c_parser/src/c_parser.y +++ b/c_parser/src/c_parser.y @@ -52,7 +52,7 @@ EXT_DECLARATION_2 : DECLARATION { $$ = $1; } // FUNCTION DEFINITION -FUNC_DEF : T_IDENTIFIER T_LRB PARAMETER_LIST T_RRB COMPOUND_STATEMENT { $$ = new ast_Function(*$1, $3); } +FUNC_DEF : T_IDENTIFIER T_LRB PARAMETER_LIST T_RRB COMPOUND_STATEMENT { $$ = new ast_Function(*$1, $3, $5); } ; PARAMETER_LIST: PARAMETER { $$ = new ast_ParamList($1); } @@ -94,7 +94,7 @@ INITIALIZER : T_INT_CONST { ; } // STATEMENT -COMPOUND_STATEMENT: T_SC { ; } +COMPOUND_STATEMENT: T_LCB EXT_DEF T_RCB { $$ = new ast_CompoundStatement($2); } ; %% diff --git a/c_parser/test/output.xml b/c_parser/test/output.xml index 8ea3e1c..d4594a2 100644 --- a/c_parser/test/output.xml +++ b/c_parser/test/output.xml @@ -10,10 +10,28 @@ <Variable id="d" /> <Variable id="hello" /> <Variable id="asd" /> +<Variable id="a" /> <Function id="f"> <Parameter id="i" /> <Parameter id="b" /> <Parameter id="c" /> <Parameter id="d" /> +<Scope> +<Variable id="a" /> +</Scope> +</Function> +<Variable id="a" /> +<Variable id="b" /> +<Variable id="c" /> +<Variable id="c" /> +<Variable id="d" /> +<Function id="func"> +<Parameter id="asd" /> +<Parameter id="b" /> +<Scope> +<Variable id="a" /> +<Variable id="b" /> +<Variable id="c" /> +</Scope> </Function> </Program> diff --git a/c_parser/test/test_parser.c b/c_parser/test/test_parser.c index d894c23..55a15b7 100644 --- a/c_parser/test/test_parser.c +++ b/c_parser/test/test_parser.c @@ -5,4 +5,12 @@ int yann, is, the, best; int d = 0; int hello = 122, asd = 123; -int f(int i, int b, int c, int d); +int f(int i, int b, int c, int d) { + int a; +} + +int func(int asd, int b) { + int a, b, c; + int c = 0; + int d; +} |