From f8d984811645f0af6859deff1bf53250317ca542 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sat, 18 Mar 2017 09:58:17 +0000 Subject: added while loop and if working --- Notes.org | 11 ++++++--- c_compiler/include/statement.hpp | 46 ++++++++++++++++++++++++++-------- c_compiler/src/statement.cpp | 53 +++++++++++++++++++++++++++++++--------- c_compiler/test/in/IfElse.c | 12 ++++++++- c_compiler/test/in/recursion.c | 13 ++++++++++ 5 files changed, 109 insertions(+), 26 deletions(-) create mode 100644 c_compiler/test/in/recursion.c diff --git a/Notes.org b/Notes.org index c4ef6a5..d865313 100644 --- a/Notes.org +++ b/Notes.org @@ -128,7 +128,6 @@ - The declaration class should only be in charge of storing it in the right location in the stack and adding that to the bindings. **** TODO Add more expressions - CLOCK: [2017-03-17 Fri 20:59] CLOCK: [2017-03-17 Fri 17:08]--[2017-03-17 Fri 20:59] => 3:51 CLOCK: [2017-03-17 Fri 13:21]--[2017-03-17 Fri 15:43] => 2:22 @@ -136,9 +135,13 @@ **** TODO Add more statements -***** If statement -***** For statement -***** While statement +***** TODO If statement + CLOCK: [2017-03-18 Sat 08:42]--[2017-03-18 Sat 09:57] => 1:15 + + + +***** TODO For statement +***** TODO While statement **** DONE Fixing Seg fault 1 CLOCK: [2017-03-17 Fri 15:44]--[2017-03-17 Fri 16:00] => 0:16 diff --git a/c_compiler/include/statement.hpp b/c_compiler/include/statement.hpp index fe509c2..ce96543 100644 --- a/c_compiler/include/statement.hpp +++ b/c_compiler/include/statement.hpp @@ -1,5 +1,5 @@ -#ifndef AST_STATEMENT_HPP -#define AST_STATEMENT_HPP +#ifndef STATEMENT_HPP +#define STATEMENT_HPP #include "bindings.hpp" #include "declaration.hpp" @@ -13,7 +13,8 @@ class Statement; typedef std::shared_ptr StatementPtr; -class Statement : public Node { +class Statement : public Node +{ protected: StatementPtr next_statement_; @@ -31,7 +32,8 @@ public: }; -class CompoundStatement : public Statement { +class CompoundStatement : public Statement +{ protected: DeclarationPtr declaration_; StatementPtr statement_; @@ -49,7 +51,8 @@ public: }; -class SelectionStatement : public Statement { +class SelectionStatement : public Statement +{ protected: ExpressionPtr condition_; StatementPtr if_; @@ -66,7 +69,8 @@ public: }; -class ExpressionStatement : public Statement { +class ExpressionStatement : public Statement +{ protected: ExpressionPtr expression_; public: @@ -81,7 +85,8 @@ public: }; -class JumpStatement : public Statement { +class JumpStatement : public Statement +{ protected: ExpressionPtr expression_; public: @@ -96,19 +101,40 @@ public: }; -class IterationStatement : public Statement { +class IterationStatement : public Statement +{ protected: + ExpressionPtr condition_; StatementPtr statement_; public: - IterationStatement(Statement* statement); + IterationStatement(Expression* condition, Statement* statement); virtual void print() const; virtual void printXml() const; - virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned& label_count) const; + virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned& label_count) const = 0; virtual void countVariables(unsigned& var_count) const; virtual void countArguments(unsigned& argument_count) const; }; +class WhileLoop : public IterationStatement +{ +public: + WhileLoop(Expression* condition, Statement* statement); + + virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned& label_cout) const; +}; + +class ForLoop : public IterationStatement +{ +private: + ExpressionPtr initializer_; + ExpressionPtr incrementer_; +public: + ForLoop(Expression* intializer, Expression* condition, Expression* incrementer, Statement* statement); + + virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned& label_count) const; +}; + #endif 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; +} diff --git a/c_compiler/test/in/IfElse.c b/c_compiler/test/in/IfElse.c index 9c60a85..8c87ce7 100644 --- a/c_compiler/test/in/IfElse.c +++ b/c_compiler/test/in/IfElse.c @@ -1,4 +1,14 @@ int main() { - return 5 != 8; + int x = 87; + if( 2 > 1) + { + x = 45; + } + else + { + x = 92; + } + + return x; } diff --git a/c_compiler/test/in/recursion.c b/c_compiler/test/in/recursion.c new file mode 100644 index 0000000..3c16e33 --- /dev/null +++ b/c_compiler/test/in/recursion.c @@ -0,0 +1,13 @@ +int fact(int n) +{ + if(n <= 1) { + return 1; + } else { + int x = fact(n-1); + return x * n; + } +} + +int main() { + return fact(5); +} -- cgit