From 60965d49259309f97af95df830dfd724616c32eb Mon Sep 17 00:00:00 2001 From: fintarin Date: Wed, 25 Oct 2023 10:22:06 +0300 Subject: [PATCH] Refactor operatorChildToString --- src/fintamath/expressions/ExpressionUtils.cpp | 17 ++++++++++------- .../expressions/binary/DivExpression.cpp | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/fintamath/expressions/ExpressionUtils.cpp b/src/fintamath/expressions/ExpressionUtils.cpp index afb997e37..9e5078ed7 100644 --- a/src/fintamath/expressions/ExpressionUtils.cpp +++ b/src/fintamath/expressions/ExpressionUtils.cpp @@ -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; diff --git a/src/fintamath/expressions/binary/DivExpression.cpp b/src/fintamath/expressions/binary/DivExpression.cpp index cff018c8c..8a5439c5c 100644 --- a/src/fintamath/expressions/binary/DivExpression.cpp +++ b/src/fintamath/expressions/binary/DivExpression.cpp @@ -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(); }