Skip to content

Commit f70f1ac

Browse files
committed
fix bug
1 parent 36b6986 commit f70f1ac

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

Expression.lib.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,13 @@ function nfx($expr)
195195
$ops_r = array('+' => 0, '-' => 0, '*' => 0, '/' => 0, '%' => 0, '^' => 1, '>' => 0,
196196
'<' => 0, '>=' => 0, '<=' => 0, '==' => 0, '!=' => 0, '=~' => 0,
197197
'&&' => 0, '||' => 0, '!' => 0); // right-associative operator?
198-
$ops_p = array('+' => 4, '-' => 4, '*' => 4, '/' => 4, '_' => 4, '%' => 4, '^' => 5, '>' => 2, '<' => 2,
199-
'>=' => 2, '<=' => 2, '==' => 2, '!=' => 2, '=~' => 2, '&&' => 1, '||' => 1, '!' => 5); // operator precedence
198+
$ops_p = array(
199+
'&&' => 1, '||' => 1,
200+
'>' => 2, '<' => 2, '>=' => 2, '<=' => 2, '==' => 2, '!=' => 2, '=~' => 2,
201+
'+' => 3, '-' => 3,
202+
'*' => 4, '/' => 4, '_' => 4, '%' => 4,
203+
'^' => 5, '!' => 5,
204+
); // operator precedence
200205

201206
$expecting_op = false; // we use this in syntax-checking the expression
202207
// and determining when a - is a negation

tests/Expression.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,22 @@ public function testCustomClosures()
269269
}
270270
}
271271

272+
public function testPriorityOperands()
273+
{
274+
$data = [
275+
'2+2*2' => 6,
276+
'2-2+2*2+2/2*-1+2' => 5,
277+
'2+1 > 2+2' => false,
278+
'2+1 < 2+2' => true,
279+
'2+2*2-2/2 >= 2*2+-2/2*2' => true,
280+
];
281+
282+
$expr = new Expression();
283+
foreach ($data as $formula => $result) {
284+
$this->assertEquals($expr->evaluate($formula), $result);
285+
}
286+
}
287+
272288
// -------------------------------------------------------------------------
273289
public function testBug_1()
274290
{

0 commit comments

Comments
 (0)