Skip to content

Commit

Permalink
Apply x / x => 1 transformation only on sub-expressions containing …
Browse files Browse the repository at this point in the history
…non-determinstic parts (fixes abdc2a0, addresses #16)
  • Loading branch information
Muqsit committed Nov 3, 2022
1 parent abdc2a0 commit db1df91
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use muqsit\arithmexp\token\UnaryOperatorToken;
use muqsit\arithmexp\Util;
use RuntimeException;
use function array_filter;
use function array_splice;
use function assert;
use function count;
Expand Down Expand Up @@ -389,7 +390,10 @@ private function processDivisionBetween(Parser $parser, FunctionCallExpressionTo
}

// reduce (x / x) to (1 / 1)
if($this->tokensEqualByReturnValue($left_operand, $right_operand)){
if(
$this->tokensEqualByReturnValue($left_operand, $right_operand) &&
count(array_filter([...$left_operand, ...$right_operand], static fn(ExpressionToken $token) : bool => !$token->isDeterministic())) > 0
){
return [
// on cancelling a value in the numerator with a value in the denominator, replace them operands with 1 (identity element of division)
[new NumericLiteralExpressionToken(Util::positionContainingExpressionTokens($left_operand), 1)],
Expand Down

0 comments on commit db1df91

Please sign in to comment.