aboutsummaryrefslogtreecommitdiffstats
path: root/c_parser
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-02-17 14:44:53 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-02-17 14:44:53 +0000
commit6c4106967613cfebfe8582c84eacbf5b4110729f (patch)
tree5d42bc0d0eea785035f79f880b41c866b7924f19 /c_parser
parenta60337264c2551fffc2b5aeea12f40a06b7cb0e9 (diff)
downloadCompiler-6c4106967613cfebfe8582c84eacbf5b4110729f.tar.gz
Compiler-6c4106967613cfebfe8582c84eacbf5b4110729f.zip
Making functions work
Diffstat (limited to 'c_parser')
-rw-r--r--c_parser/include/ast.hpp1
-rw-r--r--c_parser/include/ast_function.hpp31
-rw-r--r--c_parser/include/ast_primitives.hpp13
-rw-r--r--c_parser/src/c_parser.y35
-rw-r--r--c_parser/test/output.xml2
-rw-r--r--c_parser/test/test_parser.c4
6 files changed, 79 insertions, 7 deletions
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 <string>
+#include <iostream>
+
+class ast_Function : public ast_Base {
+private:
+ std::string name;
+ mutable std::vector<const ast_Base*> 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 << "<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);
+ }
+};
+
+#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 << "<Parameter id=\"" << id << "\" />" << 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 <stmnt> EXT_DEF EXT_DECLARATION EXT_DECLARATION_2
-%type <stmnt> FUNC_DEF
+%type <stmnt> FUNC_DEF PARAMETER_LIST PARAMETER PARAM_DECLARATOR
%type <stmnt> DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER
+%type <stmnt> COMPOUND_STATEMENT
// %type <number> // T_CONSTANT
%type <string> 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
<?xml version="1.0"?>
<Program>
<Variable id="a" />
@@ -10,4 +11,3 @@
<Variable id="d" />
<Variable id="hello" />
<Variable id="asd" />
-</Program>
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);