aboutsummaryrefslogtreecommitdiff
path: root/src/NXP/Classes/Token/TokenDivision.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/NXP/Classes/Token/TokenDivision.php')
-rw-r--r--src/NXP/Classes/Token/TokenDivision.php12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/NXP/Classes/Token/TokenDivision.php b/src/NXP/Classes/Token/TokenDivision.php
index f1c35ff..5bbc35e 100644
--- a/src/NXP/Classes/Token/TokenDivision.php
+++ b/src/NXP/Classes/Token/TokenDivision.php
@@ -10,6 +10,9 @@
namespace NXP\Classes\Token;
+use NXP\Exception\IncorrectExpressionException;
+use NXP\Exception\DivisionByZeroException;
+
/**
* @author Alexander Kiryukhin <alexander@symdev.org>
*/
@@ -41,12 +44,21 @@ class TokenDivision extends AbstractOperator
/**
* @param InterfaceToken[] $stack
+ *
* @return $this
+ *
+ * @throws \NXP\Exception\IncorrectExpressionException
+ * @throws \NXP\Exception\DivisionByZeroException
*/
public function execute(&$stack)
{
$op2 = array_pop($stack);
$op1 = array_pop($stack);
+
+ if ($op1 === null || $op2 === null) {
+ throw new IncorrectExpressionException("Division requires two operators");
+ }
+
$result = $op2->getValue() != 0 ? $op1->getValue() / $op2->getValue() : 0;
return new TokenNumber($result);