aboutsummaryrefslogtreecommitdiffstats
path: root/c_parser
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-02-17 15:10:22 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-02-17 15:10:22 +0000
commit419384dbd0efced3af6c10ccfc2eee4ff6ca22c7 (patch)
treedb81f68a4afd5e150ffeedf17f91abbff5223518 /c_parser
parent6c4106967613cfebfe8582c84eacbf5b4110729f (diff)
downloadCompiler-419384dbd0efced3af6c10ccfc2eee4ff6ca22c7.tar.gz
Compiler-419384dbd0efced3af6c10ccfc2eee4ff6ca22c7.zip
Functions now work as well
Diffstat (limited to 'c_parser')
-rw-r--r--c_parser/include/ast_base.hpp2
-rw-r--r--c_parser/include/ast_declaration.hpp4
-rw-r--r--c_parser/include/ast_function.hpp26
-rw-r--r--c_parser/src/c_parser.y6
-rw-r--r--c_parser/test/output.xml8
5 files changed, 33 insertions, 13 deletions
diff --git a/c_parser/include/ast_base.hpp b/c_parser/include/ast_base.hpp
index c4293a8..b793a9b 100644
--- a/c_parser/include/ast_base.hpp
+++ b/c_parser/include/ast_base.hpp
@@ -10,7 +10,7 @@ public:
virtual ~ast_Base() {}
virtual void print() const = 0;
- virtual void push(const ast_Base* var) 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 50cff2a..01b1498 100644
--- a/c_parser/include/ast_declaration.hpp
+++ b/c_parser/include/ast_declaration.hpp
@@ -29,8 +29,8 @@ public:
}
}
- virtual void push(const ast_Base* var) const {
- var_list.push_back(var);
+ virtual void push(const ast_Base* _var) const {
+ var_list.push_back(_var);
}
};
diff --git a/c_parser/include/ast_function.hpp b/c_parser/include/ast_function.hpp
index 60a1670..9383601 100644
--- a/c_parser/include/ast_function.hpp
+++ b/c_parser/include/ast_function.hpp
@@ -8,23 +8,37 @@
class ast_Function : public ast_Base {
private:
- std::string name;
+ std::string id;
+ const ast_Base* param;
+public:
+ ast_Function(const std::string& _id, const ast_Base* _param) : id(_id), param(_param) {}
+
+ virtual void print() const {
+ std::cout << "<Function id=\"" << id << "\">" << std::endl;
+ param->print();
+ std::cout << "</Function>" << std::endl;
+ }
+
+ virtual void push(const ast_Base* var) const {}
+};
+
+class ast_ParamList : public ast_Base {
+private:
mutable std::vector<const ast_Base*> param_list;
+
public:
- ast_Function(const std::string& _name) : name(_name) {}
- ast_Function(const ast_Base* param) {
+ ast_ParamList(const ast_Base* param) {
param_list.push_back(param);
}
virtual void print() const {
- std::cout << "<Function id=\"" << name << "\">" << 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);
+ virtual void push(const ast_Base* _var) const {
+ param_list.push_back(_var);
}
};
diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y
index de93939..95db2c9 100644
--- a/c_parser/src/c_parser.y
+++ b/c_parser/src/c_parser.y
@@ -47,15 +47,15 @@ EXT_DECLARATION : DECLARATION_SPEC EXT_DECLARATION_2 { $$ = $2; }
;
EXT_DECLARATION_2 : DECLARATION { $$ = $1; }
- | FUNC_DEF { ; }
+ | FUNC_DEF { $$ = $1; }
;
// FUNCTION DEFINITION
-FUNC_DEF : T_IDENTIFIER T_LRB PARAMETER_LIST T_RRB COMPOUND_STATEMENT { printf("Function Def\n"); }
+FUNC_DEF : T_IDENTIFIER T_LRB PARAMETER_LIST T_RRB COMPOUND_STATEMENT { $$ = new ast_Function(*$1, $3); }
;
-PARAMETER_LIST: PARAMETER { $$ = new ast_Function($1); }
+PARAMETER_LIST: PARAMETER { $$ = new ast_ParamList($1); }
| PARAMETER_LIST T_CMA PARAMETER { $$->push($3); }
;
diff --git a/c_parser/test/output.xml b/c_parser/test/output.xml
index 0e8833b..8ea3e1c 100644
--- a/c_parser/test/output.xml
+++ b/c_parser/test/output.xml
@@ -1,4 +1,3 @@
-Function Def
<?xml version="1.0"?>
<Program>
<Variable id="a" />
@@ -11,3 +10,10 @@ Function Def
<Variable id="d" />
<Variable id="hello" />
<Variable id="asd" />
+<Function id="f">
+<Parameter id="i" />
+<Parameter id="b" />
+<Parameter id="c" />
+<Parameter id="d" />
+</Function>
+</Program>