diff options
Diffstat (limited to 'c_compiler/src')
-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 |
3 files changed, 40 insertions, 16 deletions
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; +} |