aboutsummaryrefslogtreecommitdiffstats
path: root/c_compiler/src
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-03-18 09:58:17 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-03-18 09:58:17 +0000
commitf8d984811645f0af6859deff1bf53250317ca542 (patch)
tree2759399fb9cfdc53c2459ca1664c1d0a7bb6dc50 /c_compiler/src
parenta9663b327230e08a6bc5cfe4f20ed8d066f33338 (diff)
downloadCompiler-f8d984811645f0af6859deff1bf53250317ca542.tar.gz
Compiler-f8d984811645f0af6859deff1bf53250317ca542.zip
added while loop and if working
Diffstat (limited to 'c_compiler/src')
-rw-r--r--c_compiler/src/statement.cpp53
1 files changed, 42 insertions, 11 deletions
diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp
index 23fbc55..dc18fa8 100644
--- a/c_compiler/src/statement.cpp
+++ b/c_compiler/src/statement.cpp
@@ -109,8 +109,10 @@ SelectionStatement::SelectionStatement(Expression* condition, Statement* _if, St
void SelectionStatement::print() const
{
+ condition_->print();
if_->print();
- else_->print();
+ if(else_ != nullptr)
+ else_->print();
}
void SelectionStatement::printXml() const
@@ -125,13 +127,23 @@ void SelectionStatement::printXml() const
else_->printXml();
}
-VariableStackBindings SelectionStatement::printAsm(VariableStackBindings bindings, unsigned& label_count) const
+VariableStackBindings SelectionStatement::printAsm(VariableStackBindings bindings,
+ unsigned& label_count) const
{
+ unsigned if_label = label_count++;
+
condition_->printAsm(bindings, label_count);
- std::cout << "\tbeq\t$2,$0,$" << label_count++ << "_else\n";
+ std::cout << "\tbeq\t$2,$0,$" << if_label << "_else\n\tnop\n";
+
if_->printAsm(bindings, label_count);
- // TODO insert label for else and then end of statement
+ std::cout << "\tb\t$" << if_label << "_if_end\n\tnop\n$" << if_label << "_else:\n";
+
+ if(else_ != nullptr)
+ else_->printAsm(bindings, label_count);
+
+ std::cout << "$" << if_label << "_if_end:\n";
+
return bindings;
}
@@ -255,8 +267,8 @@ void JumpStatement::countArguments(unsigned& argument_count) const
// Iteration Statement definition
-IterationStatement::IterationStatement(Statement* statement)
- : statement_(statement)
+IterationStatement::IterationStatement(Expression* condition, Statement* statement)
+ : condition_(condition), statement_(statement)
{}
void IterationStatement::print() const
@@ -271,11 +283,6 @@ void IterationStatement::printXml() const
statement_->printXml();
}
-VariableStackBindings IterationStatement::printAsm(VariableStackBindings bindings, unsigned& label_count) const
-{
- return bindings;
-}
-
void IterationStatement::countVariables(unsigned& var_count) const
{
if(next_statement_ != nullptr)
@@ -293,3 +300,27 @@ void IterationStatement::countArguments(unsigned int &argument_count) const
if(statement_ != nullptr)
statement_->countArguments(argument_count);
}
+
+
+// While Loop definition
+
+WhileLoop::WhileLoop(Expression* condition, Statement* statement)
+ : IterationStatement(condition, statement)
+{}
+
+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;
+}