From 5bb277cc67c7c3fa8fa3024f1ed3da81a71636e2 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 19 Mar 2017 19:36:49 +0000 Subject: Finished better expressions --- c_compiler/src/expression.cpp | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'c_compiler/src/expression.cpp') diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index e780a55..e19d845 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -6,22 +6,25 @@ // Expression definition void Expression::print() const -{ - std::cerr << "This expression has not been implemented yet" << std::endl; -} +{} void Expression::printXml() const +{} + +void Expression::countArguments(unsigned& argument_count) const { - // Does nothing as I do not want it to appear in the xml output + // by default don't do anything to the count + (void)argument_count; } -void Expression::countArguments(unsigned int &argument_count) const +void Expression::expressionDepth(unsigned& depth_count) const { - (void)argument_count; + (void)depth_count; } int Expression::postfixStackPosition(VariableStackBindings bindings) const { + // call this if the expression is not a postfix expression std::cerr << "Error : Can't call 'getPostfixStackPosition(VariableStackBindings " << "bindings)' on this type of expression" << std::endl; (void)bindings; @@ -30,11 +33,13 @@ int Expression::postfixStackPosition(VariableStackBindings bindings) const void Expression::setPostfixExpression(Expression *postfix_expression) { + // do nothing if expression isn't a postfix expression (void)postfix_expression; } std::string Expression::id() const { + // by default return empty id, which cannot be valid. return ""; } @@ -56,6 +61,20 @@ OperationExpression::OperationExpression(Expression* lhs, Expression* rhs) : lhs_(lhs), rhs_(rhs) {} +void OperationExpression::expressionDepth(unsigned& depth_count) const +{ + unsigned lhs_depth_count = depth_count; + unsigned rhs_depth_count = depth_count+1; + + lhs_->expressionDepth(lhs_depth_count); + rhs_->expressionDepth(rhs_depth_count); + + if(lhs_depth_count > rhs_depth_count) + depth_count = lhs_depth_count; + else + depth_count = rhs_depth_count; +} + void OperationExpression::evaluateExpression(VariableStackBindings bindings, unsigned& label_count) const { // I can just evaluate the lhs with the same entry stack position @@ -414,7 +433,6 @@ 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 @@ -484,4 +502,3 @@ VariableStackBindings Constant::printAsm(VariableStackBindings bindings, unsigne std::cout << "\tsw\t$2," << bindings.currentExpressionStackPosition() << "($fp)" << std::endl; return bindings; } - -- cgit