formula parser fix
This commit is contained in:
@@ -176,6 +176,8 @@ class Parser
|
||||
|
||||
$this->processStrings($expression, $modifiedExpression, $splitterIndexList, true);
|
||||
|
||||
$expressionOutOfBraceList = [];
|
||||
|
||||
for ($i = 0; $i < strlen($modifiedExpression); $i++) {
|
||||
if ($modifiedExpression[$i] === '(') {
|
||||
$braceCounter++;
|
||||
@@ -186,6 +188,11 @@ class Parser
|
||||
if ($braceCounter === 0 && $i < strlen($modifiedExpression) - 1) {
|
||||
$hasExcessBraces = false;
|
||||
}
|
||||
if ($braceCounter === 0) {
|
||||
$expressionOutOfBraceList[] = true;
|
||||
} else {
|
||||
$expressionOutOfBraceList[] = false;
|
||||
}
|
||||
}
|
||||
if ($braceCounter !== 0) {
|
||||
throw new Error('Incorrect round brackets in expression ' . $expression . '.');
|
||||
@@ -226,7 +233,13 @@ class Parser
|
||||
|
||||
foreach ($this->priorityList as $operationList) {
|
||||
foreach ($operationList as $operator) {
|
||||
$index = strpos($expression, $operator, 1);
|
||||
$startFrom = 1;
|
||||
while (true) {
|
||||
$index = strpos($expression, $operator, $startFrom);
|
||||
if ($index === false) break;
|
||||
if ($expressionOutOfBraceList[$index]) break;
|
||||
$startFrom = $index + 1;
|
||||
}
|
||||
if ($index !== false) {
|
||||
$possibleRightOperator = null;
|
||||
if (strlen($operator) === 1) {
|
||||
|
||||
@@ -77,4 +77,29 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase
|
||||
$actual = $this->evaluator->process($expression);
|
||||
$this->assertTrue($actual);
|
||||
}
|
||||
|
||||
function testSummationOfMultipleIfThenElse()
|
||||
{
|
||||
$expression = "
|
||||
ifThenElse(
|
||||
true,
|
||||
(1 + 0 + 1) - 1 * 0.5,
|
||||
0
|
||||
)
|
||||
+
|
||||
ifThenElse(
|
||||
true,
|
||||
(1 - 0) * 0.5,
|
||||
0
|
||||
)
|
||||
+
|
||||
ifThenElse(
|
||||
true,
|
||||
(1 - 0) * 0.5,
|
||||
0
|
||||
)
|
||||
";
|
||||
$actual = $this->evaluator->process($expression);
|
||||
$this->assertEquals(2.5, $actual);
|
||||
}
|
||||
}
|
||||
@@ -305,6 +305,7 @@ class ParserTest extends \PHPUnit\Framework\TestCase
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
|
||||
$expression = "!value * 10";
|
||||
|
||||
Reference in New Issue
Block a user