diff options
-rw-r--r-- | src/NXP/Classes/Lexer.php | 4 | ||||
-rw-r--r-- | tests/MathTest.php | 9 |
2 files changed, 10 insertions, 3 deletions
diff --git a/src/NXP/Classes/Lexer.php b/src/NXP/Classes/Lexer.php index a7fe697..9964de9 100644 --- a/src/NXP/Classes/Lexer.php +++ b/src/NXP/Classes/Lexer.php @@ -76,10 +76,10 @@ class Lexer $output[] = $token; } elseif ($token instanceof TokenNumber) { // if the number starts with a minus sign, it could be a negative number, or it could be an operator grabbed by the greedy regex - // if previous token is an operator, then it negative, otherwise remove the minus sign and put a negative operator on the stack + // if previous token is an operator or open bracket, then it negative, otherwise remove the minus sign and put a negative operator on the stack if ($lastToken !== null) { $value = $token->getValue(); - if (($value < 0 || $this->isNegativeZero($value)) && ! ($lastToken instanceof AbstractOperator)) { + if (($value < 0 || $this->isNegativeZero($value)) && ! ($lastToken instanceof AbstractOperator || $lastToken instanceof TokenLeftBracket)) { $token = new TokenNumber(abs($value)); $output[] = $token; $output[] = new TokenMinus('-'); diff --git a/tests/MathTest.php b/tests/MathTest.php index cc4cd1a..7cbc7de 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -114,6 +114,7 @@ class MathTest extends \PHPUnit\Framework\TestCase ['(5 + 3) * -1'], + ['-2- 2*2'], ['2- 2*2'], ['2-(2*2)'], ['(2- 2)*2'], @@ -155,6 +156,7 @@ class MathTest extends \PHPUnit\Framework\TestCase ['-1*-2'], ['(1+2+3+4-5)*7/100'], + ['(-1+2+3+4- 5)*7/100'], ['(1+2+3+4- 5)*7/100'], ['( 1 + 2 + 3 + 4 - 5 ) * 7 / 100'], @@ -176,6 +178,7 @@ class MathTest extends \PHPUnit\Framework\TestCase ['3 <= 5'], ['5 <= 5'], ['10 < 9 || 4 > (2+1)'], + ['10 < 9 || 4 > (-2+1)'], ['10 < 9 || 4 > (2+1) && 5 == 5 || 4 != 6 || 3 >= 4 || 3 <= 7'], ['1 + 5 == 3 + 1'], @@ -193,7 +196,11 @@ class MathTest extends \PHPUnit\Framework\TestCase ['(-4)'], ['(-4 + 5)'], - + ['(3 * 1)'], + ['(-3 * -1)'], + ['1 + (-3 * -1)'], + ['1 + ( -3 * 1)'], + ['1 + (3 * -1)'], ['1 - 0'], ['1-0'], ]; |