diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-17 21:55:16 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-17 21:55:16 +0000 |
commit | 850f46eeb6de286d4b079373104af87e3aca781c (patch) | |
tree | 9f5f3c7b73d1447487030112c03c112ca85c7e55 /c_parser | |
parent | ce93fb7754f4565f70fe5572bd7d4a2c368ea9cd (diff) | |
download | Compiler-850f46eeb6de286d4b079373104af87e3aca781c.tar.gz Compiler-850f46eeb6de286d4b079373104af87e3aca781c.zip |
Finished functions completely, now moving on to statements
Diffstat (limited to 'c_parser')
-rw-r--r-- | c_parser/include/ast_statement.hpp | 27 | ||||
-rw-r--r-- | c_parser/src/c_parser.y | 15 | ||||
-rw-r--r-- | c_parser/test/in/05.c | 3 | ||||
-rw-r--r-- | c_parser/test/in/06.c | 6 | ||||
-rw-r--r-- | c_parser/test/out/01.stdout.txt | 9 | ||||
-rw-r--r-- | c_parser/test/out/05.diff.txt | 0 | ||||
-rw-r--r-- | c_parser/test/out/05.stderr.txt | 0 | ||||
-rw-r--r-- | c_parser/test/out/05.stdout.xml | 11 | ||||
-rw-r--r-- | c_parser/test/out/06.diff.txt | 0 | ||||
-rw-r--r-- | c_parser/test/out/06.stderr.txt | 0 | ||||
-rw-r--r-- | c_parser/test/out/06.stdout.xml | 14 | ||||
-rw-r--r-- | c_parser/test/ref/05.stdout.xml | 11 | ||||
-rw-r--r-- | c_parser/test/ref/06.stdout.xml | 14 |
13 files changed, 108 insertions, 2 deletions
diff --git a/c_parser/include/ast_statement.hpp b/c_parser/include/ast_statement.hpp index f2c7175..d3f6e96 100644 --- a/c_parser/include/ast_statement.hpp +++ b/c_parser/include/ast_statement.hpp @@ -1,16 +1,41 @@ #ifndef AST_STATEMENT_HPP #define AST_STATEMENT_HPP +class ast_StatementList : public ast_Base { +protected: + mutable std::vector<const ast_Base*> statement_list; + +public: + ast_StatementList(const ast_Base* _statement) { + statement_list.push_back(_statement); + } + + virtual void print() const { + for(size_t i = 0; i < statement_list.size(); ++i) { + statement_list[i]->print(); + } + } + + virtual void push(const ast_Base* _statement) const { + statement_list.push_back(_statement); + } +}; + class ast_Statement : public ast_Base { protected: mutable std::vector<const ast_Base*> ast_list; public: ast_Statement() {} + ast_Statement(const ast_Base* _el) { ast_list.push_back(_el); } + ast_Statement(const ast_Base* _dec, const ast_Base* _statement) { + ast_list.push_back(_dec); + ast_list.push_back(_statement); + } virtual void print() const { for(size_t i = 0; i < ast_list.size(); ++i) { ast_list[i]->print(); @@ -26,6 +51,8 @@ class ast_CompoundStatement : public ast_Statement { public: ast_CompoundStatement() : ast_Statement() {} ast_CompoundStatement(const ast_Base* _el) : ast_Statement(_el) {} + ast_CompoundStatement(const ast_Base* _dec, const ast_Base* _statement) : + ast_Statement(_dec, _statement) {} virtual void print() const override { std::cout << "<Scope>" << std::endl; diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y index bdafa5b..d8396a8 100644 --- a/c_parser/src/c_parser.y +++ b/c_parser/src/c_parser.y @@ -26,7 +26,7 @@ void yyerror(const char *); %type <stmnt> EXT_DEF EXT_DECLARATION %type <stmnt> FUNC_DEF PARAMETER_LIST PARAMETER PARAM_DECLARATOR %type <stmnt> DECLARATION_LIST DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER -%type <stmnt> COMPOUND_STATEMENT COMPOUND_STATEMENT_2 +%type <stmnt> STATEMENT_LIST STATEMENT COMPOUND_STATEMENT COMPOUND_STATEMENT_2 // %type <number> // T_CONSTANT %type <string> T_IDENTIFIER //T_OPERATOR @@ -96,13 +96,24 @@ INITIALIZER : T_INT_CONST { ; } // STATEMENT +STATEMENT_LIST : STATEMENT { $$ = new ast_StatementList($1); } + | STATEMENT_LIST STATEMENT { $$->push($2); } +; + +STATEMENT : COMPOUND_STATEMENT { $$ = $1; } +; + COMPOUND_STATEMENT : T_LCB COMPOUND_STATEMENT_2 { $$ = $2; } ; -COMPOUND_STATEMENT_2 : T_RCB { $$ = new ast_CompoundStatement(); } +COMPOUND_STATEMENT_2 : T_RCB { $$ = new ast_CompoundStatement; } | DECLARATION_LIST T_RCB { $$ = new ast_CompoundStatement($1); } +| DECLARATION_LIST STATEMENT_LIST T_RCB { $$ = new ast_CompoundStatement($1, $2); } + | STATEMENT_LIST T_RCB { $$ = new ast_CompoundStatement($1); } ; +// Expressions + %% ast_Top *g_root; // Definition of variable (to match declaration earlier) diff --git a/c_parser/test/in/05.c b/c_parser/test/in/05.c new file mode 100644 index 0000000..a5ac579 --- /dev/null +++ b/c_parser/test/in/05.c @@ -0,0 +1,3 @@ +int foo(int x, int y) { + {} +} diff --git a/c_parser/test/in/06.c b/c_parser/test/in/06.c new file mode 100644 index 0000000..f182bd9 --- /dev/null +++ b/c_parser/test/in/06.c @@ -0,0 +1,6 @@ +int f(int a, int b, int c) { + int d; + { + int e; + } +} diff --git a/c_parser/test/out/01.stdout.txt b/c_parser/test/out/01.stdout.txt new file mode 100644 index 0000000..bd7cd1f --- /dev/null +++ b/c_parser/test/out/01.stdout.txt @@ -0,0 +1,9 @@ +<?xml version="1.0"?> +<Program> +<Variable id="a" /> +<Variable id="b" /> +<Variable id="c" /> +<Variable id="d" /> +<Variable id="e" /> +<Variable id="f" /> +</Program> diff --git a/c_parser/test/out/05.diff.txt b/c_parser/test/out/05.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/05.diff.txt diff --git a/c_parser/test/out/05.stderr.txt b/c_parser/test/out/05.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/05.stderr.txt diff --git a/c_parser/test/out/05.stdout.xml b/c_parser/test/out/05.stdout.xml new file mode 100644 index 0000000..7381e51 --- /dev/null +++ b/c_parser/test/out/05.stdout.xml @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<Program> +<Function id="foo"> +<Parameter id="x" /> +<Parameter id="y" /> +<Scope> +<Scope> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/06.diff.txt b/c_parser/test/out/06.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/06.diff.txt diff --git a/c_parser/test/out/06.stderr.txt b/c_parser/test/out/06.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/06.stderr.txt diff --git a/c_parser/test/out/06.stdout.xml b/c_parser/test/out/06.stdout.xml new file mode 100644 index 0000000..fbcbebd --- /dev/null +++ b/c_parser/test/out/06.stdout.xml @@ -0,0 +1,14 @@ +<?xml version="1.0"?> +<Program> +<Function id="f"> +<Parameter id="a" /> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Variable id="d" /> +<Scope> +<Variable id="e" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/ref/05.stdout.xml b/c_parser/test/ref/05.stdout.xml new file mode 100644 index 0000000..7381e51 --- /dev/null +++ b/c_parser/test/ref/05.stdout.xml @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<Program> +<Function id="foo"> +<Parameter id="x" /> +<Parameter id="y" /> +<Scope> +<Scope> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/ref/06.stdout.xml b/c_parser/test/ref/06.stdout.xml new file mode 100644 index 0000000..fbcbebd --- /dev/null +++ b/c_parser/test/ref/06.stdout.xml @@ -0,0 +1,14 @@ +<?xml version="1.0"?> +<Program> +<Function id="f"> +<Parameter id="a" /> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Variable id="d" /> +<Scope> +<Variable id="e" /> +</Scope> +</Scope> +</Function> +</Program> |