aboutsummaryrefslogtreecommitdiffstats
path: root/c_compiler/src/expression.cpp
diff options
context:
space:
mode:
authorymherklotz <ymherklotz@gmail.com>2017-03-10 19:57:46 +0000
committerymherklotz <ymherklotz@gmail.com>2017-03-10 19:57:46 +0000
commita6fa6da5de8252a205e000dd7acc2589735aa55c (patch)
tree77a791e0877fc4925f8d0dba6eb746bb32eb88e9 /c_compiler/src/expression.cpp
parentfc4486443f1af75526306377437cb82c2c9f37ac (diff)
downloadCompiler-a6fa6da5de8252a205e000dd7acc2589735aa55c.tar.gz
Compiler-a6fa6da5de8252a205e000dd7acc2589735aa55c.zip
Can do simple add and subtract
Diffstat (limited to 'c_compiler/src/expression.cpp')
-rw-r--r--c_compiler/src/expression.cpp33
1 files changed, 27 insertions, 6 deletions
diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp
index 8935e37..fb5b62b 100644
--- a/c_compiler/src/expression.cpp
+++ b/c_compiler/src/expression.cpp
@@ -5,17 +5,25 @@
// Expression definition
+// There are no values to delete so it is just empty
Expression::~Expression()
{}
void Expression::print() const
-{}
+{
+ std::cerr << "This expression has not been implemented yet" << std::endl;
+}
void Expression::printxml() const
-{}
+{
+ // Does nothing as I do not want it to appear in the xml output
+}
int32_t Expression::getPostfixStackPosition(VariableStackBindings bindings) const
{
+ std::cerr << "Error: Can't call 'getPostfixStackPosition(VariableStackBindings " <<
+ "bindings)' on this type of expression" << std::endl;
+ (void)bindings;
return -1;
}
@@ -26,6 +34,7 @@ OperationExpression::OperationExpression(Expression* _lhs, Expression* _rhs)
: lhs(_lhs), rhs(_rhs)
{}
+// deletes the two member variables that have been initialized
OperationExpression::~OperationExpression()
{
delete lhs;
@@ -41,30 +50,41 @@ AssignmentExpression::AssignmentExpression(Expression* _lhs, Expression* _rhs)
VariableStackBindings AssignmentExpression::printasm(VariableStackBindings bindings) const
{
+ // the lhs is forced to have a stack position due to it being a function, array or other type of variable
int32_t store_stack_position = lhs->getPostfixStackPosition(bindings);
rhs->printasm(bindings);
+ // we are assigning so we don't have to evaluate the lhs as it will be overwritten anyways
std::cout << "\tsw\t$2," << store_stack_position << "($fp)" << std::endl;
-
return bindings;
}
// Additive Expression definition
-AdditiveExpression::AdditiveExpression(Expression* _lhs, const std::string& _operand, Expression* _rhs)
- : OperationExpression(_lhs, _rhs), operand(_operand)
+AdditiveExpression::AdditiveExpression(Expression* _lhs, const std::string& _operation, Expression* _rhs)
+ : OperationExpression(_lhs, _rhs), operation(_operation)
{}
VariableStackBindings AdditiveExpression::printasm(VariableStackBindings bindings) const
{
rhs->printasm(bindings);
+ // move the rhs out of the way to be able to evaluate the lhs
std::cout << "\tmove\t$3,$2" << std::endl;
lhs->printasm(bindings);
- std::cout << "\tadd\t$2,$2,$3" << std::endl;
+ // then perform the right operation
+
+ // currently using signed and sub because I only have signed numbers implemented
+ // must update this as I add more types
+ if(operation == "+")
+ std::cout << "\tadd\t$2,$2,$3" << std::endl;
+ else if(operation == "-")
+ std::cout << "\tsub\t$2,$2,$3" << std::endl;
+ else
+ std::cerr << "Don't recognize symbol: '" << operation << "'" << std::endl;
return bindings;
}
@@ -106,6 +126,7 @@ Constant::Constant(const int32_t& constant)
VariableStackBindings Constant::printasm(VariableStackBindings bindings) const
{
+ // constant only has to load to $2 because the other expression will take care of the rest
std::cout << "\tli\t$2," << m_constant << std::endl;
return bindings;