improved bracket handling in function string generation.

This commit is contained in:
suter_a 2024-04-20 09:40:04 +02:00
parent c7b0d2b40f
commit 65066596e8

View File

@ -597,6 +597,7 @@ void PFunction::EvalTreeForString(tree_parse_info<> info)
void PFunction::EvalTreeForStringExpression(iter_t const& i)
{
static Int_t termOp = 0;
static bool funcFlag = false;
if (i->value.id() == PFunctionGrammar::realID) {
assert(i->children.size() == 0);
@ -628,6 +629,7 @@ void PFunction::EvalTreeForStringExpression(iter_t const& i)
fFuncString += boost::algorithm::trim_copy(std::string(i->value.begin(), i->value.end())).c_str();
} else if (i->value.id() == PFunctionGrammar::functionID) {
assert(i->children.size() == 3);
funcFlag = true;
fFuncString += std::string(i->value.begin(), i->value.end()).c_str(); // keep function name
fFuncString += "(";
// '(', expression, ')'
@ -635,6 +637,7 @@ void PFunction::EvalTreeForStringExpression(iter_t const& i)
fFuncString += ")";
} else if (i->value.id() == PFunctionGrammar::powerID) {
assert(i->children.size() == 5);
funcFlag = true;
fFuncString += std::string(i->value.begin(), i->value.end()).c_str(); // keep function name
fFuncString += "(";
// '(', expression, ',' expression, ')'
@ -655,15 +658,21 @@ void PFunction::EvalTreeForStringExpression(iter_t const& i)
assert(i->children.size() == 2);
EvalTreeForStringExpression(i->children.begin());
fFuncString += " / ";
fFuncString += "(";
if (((i->children.begin()+1)->children.size()>1) &&
((i->children.begin()+1)->value.id() != PFunctionGrammar::functionID) &&
(i->children.begin()+1)->value.id() != PFunctionGrammar::powerID) // check if denominator is non-trivial and not a function
fFuncString += "(";
EvalTreeForStringExpression(i->children.begin()+1);
fFuncString += ")";
if (((i->children.begin()+1)->children.size()>1) &&
((i->children.begin()+1)->value.id() != PFunctionGrammar::functionID) &&
(i->children.begin()+1)->value.id() != PFunctionGrammar::powerID) // check if denominator is non-trivial and not a function
fFuncString += ")";
} else {
assert(0);
}
termOp--;
} else if (i->value.id() == PFunctionGrammar::expressionID) {
if (termOp > 0)
if ((termOp > 0) && !funcFlag)
fFuncString += "(";
if (*i->value.begin() == '+') {
assert(i->children.size() == 2);
@ -678,8 +687,9 @@ void PFunction::EvalTreeForStringExpression(iter_t const& i)
} else {
assert(0);
}
if (termOp > 0)
if ((termOp > 0) && !funcFlag)
fFuncString += ")";
funcFlag = false;
} else if (i->value.id() == PFunctionGrammar::assignmentID) {
assert(i->children.size() == 3);
EvalTreeForStringExpression(i->children.begin());