formula parser fix

This commit is contained in:
yuri
2018-07-18 10:29:06 +03:00
parent 9d49f418c8
commit a4e23b8adb
3 changed files with 40 additions and 1 deletions
+14 -1
View File
@@ -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";