From b81f60d8b523260526af15361d4ce0ac4a7757c8 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 17 Feb 2017 15:44:24 +0000 Subject: Finished functions for now --- c_parser/include/ast.hpp | 1 + c_parser/include/ast_function.hpp | 5 ++++- c_parser/include/ast_statement.hpp | 37 +++++++++++++++++++++++++++++++++++++ c_parser/src/c_parser.y | 4 ++-- c_parser/test/output.xml | 18 ++++++++++++++++++ c_parser/test/test_parser.c | 10 +++++++++- 6 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 c_parser/include/ast_statement.hpp 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 << "" << std::endl; param->print(); + comp_statement->print(); std::cout << "" << 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 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 << "" << std::endl; + for(size_t i = 0; i < ast_list.size(); ++i) { + ast_list[i]->print(); + } + std::cout << "" << 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 @@ + + + + + + + + + + + + + + + + + + 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; +} -- cgit