Skip to content

Commit

Permalink
Refactor operatorChildToString
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Oct 25, 2023
1 parent e9ef065 commit 60965d4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/fintamath/expressions/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@ std::string operatorChildToString(const IOperator &oper, const ArgumentPtr &chil
bool shouldPutInBrackets = false;

if (childOper) {
shouldPutInBrackets = oper.getOperatorPriority() == IOperator::Priority::PostfixUnary ||
childOper->getOperatorPriority() > oper.getOperatorPriority();

shouldPutInBrackets = shouldPutInBrackets ||
(childOper->getFunctionType() == IFunction::Type::Unary
? childOper->getOperatorPriority() == oper.getOperatorPriority()
: !oper.isAssociative());
if (oper.getOperatorPriority() == IOperator::Priority::PostfixUnary) {
shouldPutInBrackets = true;
}
else if (childOper->getFunctionType() == IFunction::Type::Unary) {
shouldPutInBrackets = childOper->getOperatorPriority() >= oper.getOperatorPriority();
}
else {
shouldPutInBrackets = childOper->getOperatorPriority() > oper.getOperatorPriority() ||
!oper.isAssociative();
}
}

return shouldPutInBrackets ? putInBrackets(childStr) : childStr;
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/binary/DivExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ DivExpression::DivExpression(ArgumentPtr inLhsChild, ArgumentPtr inRhsChild)
}

std::string DivExpression::toString() const {
if (isNegated(lhsChild)) {
if (isNegated(lhsChild)) { // TODO: find more efficient solution
ArgumentPtr innerDiv = divExpr(negExpr(lhsChild)->toMinimalObject(), rhsChild);
return negExpr(innerDiv)->toString();
}
Expand Down

0 comments on commit 60965d4

Please sign in to comment.