Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
Visitor\Expression - fix pass for division - type(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Jun 29, 2015
1 parent 7e23c8d commit 96b4b15
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/Visitor/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,22 @@ protected function passBinaryOpDiv(Node\Expr\BinaryOp\Div $expr)
$expression = new Expression($this->context);
$left = $expression->compile($expr->left);

$expression = new Expression($this->context);
$right = $expression->compile($expr->right);

switch ($left->getType()) {
case CompiledExpression::LNUMBER:
case CompiledExpression::DNUMBER:
if ($left->isEquals(0)) {
$this->context->notice(
'division-zero',
sprintf('You trying to use division from %s/{expr}', $left->getValue()),
$expr
);

return new CompiledExpression(CompiledExpression::DNUMBER, 0.0);
}
break;
case CompiledExpression::LNUMBER:
case CompiledExpression::BOOLEAN:
if ($left->isEquals(0)) {
$this->context->notice(
Expand All @@ -493,16 +506,19 @@ protected function passBinaryOpDiv(Node\Expr\BinaryOp\Div $expr)
$expr
);

/**
* Micro optimization -> 0/{expr} -> 0
*/
return new CompiledExpression(CompiledExpression::LNUMBER, 0);
switch ($right->getType()) {
case CompiledExpression::LNUMBER:
case CompiledExpression::BOOLEAN:
return new CompiledExpression(CompiledExpression::LNUMBER, 0);
break;
case CompiledExpression::DNUMBER:
return new CompiledExpression(CompiledExpression::DNUMBER, 0.0);
break;
}
}
break;
}

$expression = new Expression($this->context);
$right = $expression->compile($expr->right);
switch ($right->getType()) {
case CompiledExpression::LNUMBER:
case CompiledExpression::DNUMBER:
Expand Down

0 comments on commit 96b4b15

Please sign in to comment.