Skip to content

Commit

Permalink
Resolve division by NAN to NAN during operator strength reduction (ad…
Browse files Browse the repository at this point in the history
…dress #16)
  • Loading branch information
Muqsit committed Nov 3, 2022
1 parent 1990dfa commit 03440ab
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use function count;
use function gettype;
use function is_array;
use function is_nan;
use const NAN;

final class OperatorStrengthReductionExpressionOptimizer implements ExpressionOptimizer{

Expand Down Expand Up @@ -130,6 +132,14 @@ private function valueEquals(array $tokens, int $value) : bool{
};
}

/**
* @param ExpressionToken[] $tokens
* @return bool
*/
private function valueIsNan(array $tokens) : bool{
return count($tokens) === 1 && $tokens[0] instanceof NumericLiteralExpressionToken && is_nan($tokens[0]->value);
}

/**
* @param ExpressionToken[] $x
* @param ExpressionToken[] $y
Expand Down Expand Up @@ -209,6 +219,7 @@ private function processBinaryExpression(Parser $parser, Expression $expression,
$this->valueEquals($right, 0) => throw ParseException::unresolvableExpressionDivisionByZero($expression->getExpression(), Util::positionContainingExpressionTokens($right)),
$this->valueEquals($left, 0) => [new NumericLiteralExpressionToken(Util::positionContainingExpressionTokens([...$left, ...$right]), 0)],
$this->valueEquals($right, 1) => $left,
$this->valueIsNan($right) => [new NumericLiteralExpressionToken(Util::positionContainingExpressionTokens([...$left, ...$right]), NAN)],
default => $this->processDivision($parser, $expression, $operator_token, $left, $right)
},
"%" => match(true){
Expand Down

0 comments on commit 03440ab

Please sign in to comment.