diff options
Diffstat (limited to 'c_compiler/src/statement.cpp')
-rw-r--r-- | c_compiler/src/statement.cpp | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp new file mode 100644 index 0000000..7b98631 --- /dev/null +++ b/c_compiler/src/statement.cpp @@ -0,0 +1,205 @@ +#include "statement.hpp" +#include "declaration.hpp" +#include "expression.hpp" + +#include <iostream> + + +// General base Statement definition + +Statement::Statement(Statement* statement) + : next_statement(statement) +{} + +void Statement::addStatement(Statement* _next) +{ + next_statement = _next; +} + + +// Compound Statement definition + +CompoundStatement::CompoundStatement(Declaration* decl, Statement* statement) + : Statement(), m_decl(decl), m_statement(statement) +{} + +CompoundStatement::CompoundStatement(Statement* statement) + : m_statement(statement) +{} + +void CompoundStatement::print() const +{ + if(m_decl != nullptr) + m_decl->print(); + + if(m_statement != nullptr) + m_statement->print(); +} + +void CompoundStatement::printxml() const +{ + if(next_statement != nullptr) + next_statement->printxml(); + + std::cout << "<Scope>" << std::endl; + + if(m_decl != nullptr) + m_decl->printxml(); + + if(m_statement != nullptr) + m_statement->printxml(); + + std::cout << "</Scope>" << std::endl; +} + +VariableStackBindings CompoundStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->printasm(bindings, var_count); + + // TODO printasm for the declaration + + if(m_statement != nullptr) + m_statement->printasm(bindings, var_count); +} + +void CompoundStatement::count_variables(int32_t& var_count) const +{ + Declaration* declaration = m_decl; + + if(next_statement != nullptr) + next_statement->count_variables(var_count); + + if(m_statement != nullptr) + m_statement->count_variables(var_count); + + while(declaration != nullptr) { + Declaration* declaration_list = declaration->getNextListItem(); + + while(declaration_list != nullptr) { + var_count++; + + declaration_list = declaration_list->getNextListItem(); + } + + var_count++; + + declaration = declaration->getNext(); + } +} + + +// Selection Statement definition + +SelectionStatement::SelectionStatement(Statement* _if, Statement* _else) + : Statement(), m_if(_if), m_else(_else) {} + +void SelectionStatement::print() const +{ + m_if->print(); + m_else->print(); +} + +void SelectionStatement::printxml() const +{ + if(next_statement != nullptr) + next_statement->printxml(); + if(m_if != nullptr) + m_if->printxml(); + if(m_else != nullptr) + m_else->printxml(); +} + +VariableStackBindings SelectionStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const +{} + +void SelectionStatement::count_variables(int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->count_variables(var_count); + + if(m_if != nullptr) + m_if->count_variables(var_count); + + if(m_else != nullptr) + m_else->count_variables(var_count); +} + + +// Expression Statement definition + +ExpressionStatement::ExpressionStatement(Expression* expr) + : Statement(), m_expr(expr) +{} + +void ExpressionStatement::print() const +{} + +void ExpressionStatement::printxml() const +{} + +VariableStackBindings ExpressionStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const +{} + +void ExpressionStatement::count_variables(int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->count_variables(var_count); +} + + +// Jump Statement definition + +JumpStatement::JumpStatement(Expression* expr) + : m_expr(expr) +{} + +void JumpStatement::print() const +{} + +void JumpStatement::printxml() const +{ + if(next_statement != nullptr) + next_statement->printxml(); +} + +VariableStackBindings JumpStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const +{ + m_expr->printasm(bindings, var_count); +} + +void JumpStatement::count_variables(int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->count_variables(var_count); +} + + +// Iteration Statement definition + +IterationStatement::IterationStatement(Statement* statement) + : m_statement(statement) +{} + +void IterationStatement::print() const +{} + +void IterationStatement::printxml() const +{ + if(next_statement != nullptr) + next_statement->printxml(); + if(m_statement != nullptr) + m_statement->printxml(); +} + +VariableStackBindings IterationStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const +{} + +void IterationStatement::count_variables(int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->count_variables(var_count); + + if(m_statement != nullptr) + m_statement->count_variables(var_count); +} |