diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-18 14:34:12 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-18 14:34:12 +0000 |
commit | fac528593e216830c89360f5c6ce64dbe7c5176f (patch) | |
tree | 18cbabda0f8bfcec83669de702e1ce3bd2660eda /c_compiler | |
parent | eae17d816ea2aa8075d671635c45d3d6b7891e90 (diff) | |
download | Compiler-fac528593e216830c89360f5c6ce64dbe7c5176f.tar.gz Compiler-fac528593e216830c89360f5c6ce64dbe7c5176f.zip |
Broke compound statement
Diffstat (limited to 'c_compiler')
-rw-r--r-- | c_compiler/include/statement.hpp | 2 | ||||
-rw-r--r-- | c_compiler/src/c_parser.y | 7 | ||||
-rw-r--r-- | c_compiler/src/expression.cpp | 1 | ||||
-rw-r--r-- | c_compiler/src/statement.cpp | 48 | ||||
-rw-r--r-- | c_compiler/test/in/for.c | 12 |
5 files changed, 53 insertions, 17 deletions
diff --git a/c_compiler/include/statement.hpp b/c_compiler/include/statement.hpp index ce96543..121db2c 100644 --- a/c_compiler/include/statement.hpp +++ b/c_compiler/include/statement.hpp @@ -131,7 +131,7 @@ private: ExpressionPtr initializer_; ExpressionPtr incrementer_; public: - ForLoop(Expression* intializer, Expression* condition, Expression* incrementer, Statement* statement); + ForLoop(Expression* initializer, Expression* condition, Expression* incrementer, Statement* statement); virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned& label_count) const; }; diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index 8f70a90..315d227 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -177,7 +177,7 @@ IdentifierList: T_IDENTIFIER { $$ = new Declaration(); } StatementList: Statement { $$ = $1; } - | StatementList Statement { $2->linkStatement($$); $$ = $2; } +| StatementList Statement { $2->linkStatement($$); $$ = $2; printf("# Making statement list\n"); } ; Statement: CompoundStatement { $$ = $1; } @@ -205,7 +205,7 @@ SelectionStatement: ExpressionStatement: T_SC { $$ = new ExpressionStatement(); } - | Expression T_SC { $$ = new ExpressionStatement($1); } +| Expression T_SC { $$ = new ExpressionStatement($1); printf("# Adding Expression statement\n"); } ; JumpStatement: T_RETURN Expression T_SC { $$ = new JumpStatement($2); } @@ -214,7 +214,8 @@ JumpStatement: T_RETURN Expression T_SC { $$ = new JumpStatement($2); } IterationStatement: T_WHILE T_LRB Expression T_RRB Statement { $$ = new WhileLoop($3, $5); } | T_DO Statement T_WHILE T_LRB Expression T_RRB T_SC { $$ = $2; } - | T_FOR T_LRB Expression T_SC Expression T_SC Expression T_RRB Statement { $$ = $9; } + | T_FOR T_LRB Expression T_SC Expression T_SC Expression T_RRB Statement + { $$ = new ForLoop($3, $5, $7, $9); } ; // Expressions diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index b7965eb..e780a55 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -414,6 +414,7 @@ AssignmentExpression::AssignmentExpression(Expression* lhs, Expression* rhs) VariableStackBindings AssignmentExpression::printAsm(VariableStackBindings bindings, unsigned& label_count) const { + std::cout << "# Assignment at stack position: " << lhs_->postfixStackPosition(bindings) << std::endl; // TODO add stack and store results in there, also for addition and multiplication. // get the current location of lhs in the stack so that I can store result there diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp index dc18fa8..2720570 100644 --- a/c_compiler/src/statement.cpp +++ b/c_compiler/src/statement.cpp @@ -51,13 +51,16 @@ void CompoundStatement::printXml() const std::cout << "</Scope>" << std::endl; } -VariableStackBindings CompoundStatement::printAsm(VariableStackBindings bindings, unsigned& label_count) const +VariableStackBindings CompoundStatement::printAsm(VariableStackBindings bindings, + unsigned& label_count) const { + std::cout << "# printint compound statement" << std::endl; VariableStackBindings outer_scope_bindings = bindings; if(next_statement_ != nullptr) next_statement_->printAsm(bindings, label_count); - + else + std::cout << "# No statement\n"; if(declaration_ != nullptr) bindings = declaration_->printAsm(bindings, label_count); @@ -130,6 +133,7 @@ void SelectionStatement::printXml() const VariableStackBindings SelectionStatement::printAsm(VariableStackBindings bindings, unsigned& label_count) const { + std::cout << "# If Statement\n"; unsigned if_label = label_count++; condition_->printAsm(bindings, label_count); @@ -184,11 +188,10 @@ void ExpressionStatement::print() const void ExpressionStatement::printXml() const {} -VariableStackBindings ExpressionStatement::printAsm(VariableStackBindings bindings, unsigned& label_count) const +VariableStackBindings ExpressionStatement::printAsm(VariableStackBindings bindings, + unsigned& label_count) const { - if(next_statement_ != nullptr) - next_statement_->printAsm(bindings, label_count); - + std::cout << "# Expression Statement\n"; if(expression_ != nullptr) expression_->printAsm(bindings, label_count); @@ -231,7 +234,8 @@ void JumpStatement::printXml() const next_statement_->printXml(); } -VariableStackBindings JumpStatement::printAsm(VariableStackBindings bindings, unsigned& label_count) const +VariableStackBindings JumpStatement::printAsm(VariableStackBindings bindings, + unsigned& label_count) const { if(next_statement_ != nullptr) next_statement_->printAsm(bindings, label_count); @@ -308,19 +312,37 @@ WhileLoop::WhileLoop(Expression* condition, Statement* statement) : IterationStatement(condition, statement) {} -VariableStackBindings WhileLoop::printAsm(VariableStackBindings bindings, unsigned& label_count) const -{ +VariableStackBindings WhileLoop::printAsm(VariableStackBindings bindings, + unsigned& label_count) const +{ int while_label = label_count++; + std::cout << "\tb\t$" << while_label << "_while_cond\n\tnop\n$" << while_label << "_while_body:\n"; - statement_->printAsm(bindings, label_count); - std::cout << "$" << while_label << "_while_cond:\n"; - condition_->printAsm(bindings, label_count); - std::cout << "\tbne\t$2,$0,$" << while_label << "_while_body\n\tnop\n"; return bindings; } + +ForLoop::ForLoop(Expression* initializer, Expression* condition, + Expression* incrementer, Statement* statement) + : IterationStatement(condition, statement), initializer_(initializer), incrementer_(incrementer) +{} + +VariableStackBindings ForLoop::printAsm(VariableStackBindings bindings, unsigned& label_count) const +{ + int for_label = label_count++; + + initializer_->printAsm(bindings, label_count); + std::cout << "\tb\t$" << for_label << "_for_cond\n\tnop\n$" << for_label << "_for_body:\n"; + statement_->printAsm(bindings, label_count); + incrementer_->printAsm(bindings, label_count); + std::cout << "$" << for_label << "_for_cond:\n"; + condition_->printAsm(bindings, label_count); + std::cout << "\tbne\t$2,$0,$" << for_label << "_for_body\n\tnop\n"; + + return bindings; +} diff --git a/c_compiler/test/in/for.c b/c_compiler/test/in/for.c new file mode 100644 index 0000000..0af6158 --- /dev/null +++ b/c_compiler/test/in/for.c @@ -0,0 +1,12 @@ +int main() +{ + int x, y, z; + + x = 2 * 3; + y = 1 * 3; + z = 9; + + y = x + y + z; + + return y; +} |