Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
optimistex committed Oct 24, 2016
1 parent 36b6986 commit f70f1ac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Expression.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,13 @@ function nfx($expr)
$ops_r = array('+' => 0, '-' => 0, '*' => 0, '/' => 0, '%' => 0, '^' => 1, '>' => 0,
'<' => 0, '>=' => 0, '<=' => 0, '==' => 0, '!=' => 0, '=~' => 0,
'&&' => 0, '||' => 0, '!' => 0); // right-associative operator?
$ops_p = array('+' => 4, '-' => 4, '*' => 4, '/' => 4, '_' => 4, '%' => 4, '^' => 5, '>' => 2, '<' => 2,
'>=' => 2, '<=' => 2, '==' => 2, '!=' => 2, '=~' => 2, '&&' => 1, '||' => 1, '!' => 5); // operator precedence
$ops_p = array(
'&&' => 1, '||' => 1,
'>' => 2, '<' => 2, '>=' => 2, '<=' => 2, '==' => 2, '!=' => 2, '=~' => 2,
'+' => 3, '-' => 3,
'*' => 4, '/' => 4, '_' => 4, '%' => 4,
'^' => 5, '!' => 5,
); // operator precedence

$expecting_op = false; // we use this in syntax-checking the expression
// and determining when a - is a negation
Expand Down
16 changes: 16 additions & 0 deletions tests/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,22 @@ public function testCustomClosures()
}
}

public function testPriorityOperands()
{
$data = [
'2+2*2' => 6,
'2-2+2*2+2/2*-1+2' => 5,
'2+1 > 2+2' => false,
'2+1 < 2+2' => true,
'2+2*2-2/2 >= 2*2+-2/2*2' => true,
];

$expr = new Expression();
foreach ($data as $formula => $result) {
$this->assertEquals($expr->evaluate($formula), $result);
}
}

// -------------------------------------------------------------------------
public function testBug_1()
{
Expand Down

0 comments on commit f70f1ac

Please sign in to comment.