From 6c4106967613cfebfe8582c84eacbf5b4110729f Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 17 Feb 2017 14:44:53 +0000 Subject: Making functions work --- c_parser/include/ast.hpp | 1 + c_parser/include/ast_function.hpp | 31 +++++++++++++++++++++++++++++++ c_parser/include/ast_primitives.hpp | 13 +++++++++++++ c_parser/src/c_parser.y | 35 ++++++++++++++++++++++++++++++----- c_parser/test/output.xml | 2 +- c_parser/test/test_parser.c | 4 +++- 6 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 c_parser/include/ast_function.hpp diff --git a/c_parser/include/ast.hpp b/c_parser/include/ast.hpp index e17ab49..7292af2 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_function.hpp" #include "ast_declaration.hpp" #include "ast_primitives.hpp" #include "ast_top.hpp" diff --git a/c_parser/include/ast_function.hpp b/c_parser/include/ast_function.hpp new file mode 100644 index 0000000..60a1670 --- /dev/null +++ b/c_parser/include/ast_function.hpp @@ -0,0 +1,31 @@ +#ifndef AST_FUNCTION_HPP +#define AST_FUNCTION_HPP + +#include "ast.hpp" + +#include +#include + +class ast_Function : public ast_Base { +private: + std::string name; + mutable std::vector param_list; +public: + ast_Function(const std::string& _name) : name(_name) {} + ast_Function(const ast_Base* param) { + param_list.push_back(param); + } + + virtual void print() const { + std::cout << "" << std::endl; + for(size_t i = 0; i < param_list.size(); ++i) { + param_list[i]->print(); + } + } + + virtual void push(const ast_Base* var) const { + param_list.push_back(var); + } +}; + +#endif diff --git a/c_parser/include/ast_primitives.hpp b/c_parser/include/ast_primitives.hpp index 21629e7..d878780 100644 --- a/c_parser/include/ast_primitives.hpp +++ b/c_parser/include/ast_primitives.hpp @@ -18,4 +18,17 @@ public: virtual void push(const ast_Base* var) const {} }; +class ast_Parameter : public ast_Base { +private: + std::string id; +public: + ast_Parameter(const std::string& _id) : id(_id) {} + + virtual void print() const { + std::cout << "" << std::endl; + } + + virtual void push(const ast_Base* var) const {} +}; + #endif diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y index 9171a1b..de93939 100644 --- a/c_parser/src/c_parser.y +++ b/c_parser/src/c_parser.y @@ -24,8 +24,9 @@ void yyerror(const char *); %token T_INT_CONST %type EXT_DEF EXT_DECLARATION EXT_DECLARATION_2 -%type FUNC_DEF +%type FUNC_DEF PARAMETER_LIST PARAMETER PARAM_DECLARATOR %type DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER +%type COMPOUND_STATEMENT // %type // T_CONSTANT %type T_IDENTIFIER //T_OPERATOR @@ -34,43 +35,67 @@ void yyerror(const char *); %% ROOT : EXT_DEF { ; } +; // EXTERNAL DEFINITION EXT_DEF : EXT_DECLARATION { g_root->push($1); } - | EXT_DEF EXT_DECLARATION { g_root->push($2); } + | EXT_DEF EXT_DECLARATION { g_root->push($2); } +; EXT_DECLARATION : DECLARATION_SPEC EXT_DECLARATION_2 { $$ = $2; } +; EXT_DECLARATION_2 : DECLARATION { $$ = $1; } - | FUNC_DEF { ; } + | FUNC_DEF { ; } +; // FUNCTION DEFINITION -FUNC_DEF : T_IDENTIFIER T_LRB T_RRB T_LCB T_RCB { ; } +FUNC_DEF : T_IDENTIFIER T_LRB PARAMETER_LIST T_RRB COMPOUND_STATEMENT { printf("Function Def\n"); } +; + +PARAMETER_LIST: PARAMETER { $$ = new ast_Function($1); } + | PARAMETER_LIST T_CMA PARAMETER { $$->push($3); } +; + +PARAMETER: DECLARATION_SPEC PARAM_DECLARATOR { $$ = $2; } +; + +PARAM_DECLARATOR: T_IDENTIFIER { $$ = new ast_Parameter(*$1);} // DECLARATION DECLARATION : INIT_DECLARATOR_LIST T_SC { $$ = $1; } +; DECLARATION_SPEC : DECLARATION_SPEC_T { ; } | DECLARATION_SPEC_T DECLARATION_SPEC { ; } +; DECLARATION_SPEC_T : T_TYPE_SPEC { ; } | T_TYPE_QUAL { ; } | T_STRG_SPEC { ; } +; 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_Variable(*$1); } +; INITIALIZER : T_INT_CONST { ; } +; + +// STATEMENT -// STATEMENTS +COMPOUND_STATEMENT: T_SC { ; } +; %% diff --git a/c_parser/test/output.xml b/c_parser/test/output.xml index 852ed49..0e8833b 100644 --- a/c_parser/test/output.xml +++ b/c_parser/test/output.xml @@ -1,3 +1,4 @@ +Function Def @@ -10,4 +11,3 @@ - diff --git a/c_parser/test/test_parser.c b/c_parser/test/test_parser.c index 557fd56..d894c23 100644 --- a/c_parser/test/test_parser.c +++ b/c_parser/test/test_parser.c @@ -3,4 +3,6 @@ int b; int c; int yann, is, the, best; int d = 0; -int hello = 123, asd = 123; +int hello = 122, asd = 123; + +int f(int i, int b, int c, int d); -- cgit