diff options
author | Bruce Wells <phpfui@users.noreply.github.com> | 2018-10-25 18:54:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-25 18:54:54 +0300 |
commit | 43f0ff3f28d198fbb4e36346fc5f36fa91cf3e18 (patch) | |
tree | 28a0abef9e0e48df01660985339e93c69a2cd746 /src/NXP/Classes/Token/TokenDegree.php | |
parent | 4a672cfd94c07e1821227f27fd1edd2217685136 (diff) |
Support for better invalid expression detection and divide by zero (#30)
* Additional validation for bad expressions (*+ for example)
* Removing DivisionByZeroException testing for now
Added more unit tests.
Diffstat (limited to 'src/NXP/Classes/Token/TokenDegree.php')
-rw-r--r-- | src/NXP/Classes/Token/TokenDegree.php | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/src/NXP/Classes/Token/TokenDegree.php b/src/NXP/Classes/Token/TokenDegree.php index c31b66e..3eec23d 100644 --- a/src/NXP/Classes/Token/TokenDegree.php +++ b/src/NXP/Classes/Token/TokenDegree.php @@ -10,6 +10,8 @@ namespace NXP\Classes\Token; +use NXP\Exception\IncorrectExpressionException; + /** * @author Alexander Kiryukhin <alexander@symdev.org> */ @@ -41,12 +43,20 @@ class TokenDegree extends AbstractOperator /** * @param InterfaceToken[] $stack + * * @return TokenNumber + * + * @throws \NXP\Exception\IncorrectExpressionException */ public function execute(&$stack) { $op2 = array_pop($stack); $op1 = array_pop($stack); + + if ($op1 === null || $op2 === null) { + throw new IncorrectExpressionException("Power operator requires two operators"); + } + $result = $op1->getValue() ** $op2->getValue(); return new TokenNumber($result); |