diff options
author | Bruce Wells <brucekwells@gmail.com> | 2020-06-04 18:43:16 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-04 18:43:16 +0300 |
commit | aa8ffe19f2eb6f194b3e6ee1aac4c2706659e6b9 (patch) | |
tree | 55ef6ebce4cdcb53bfe7cc142ef40f90756d9b6c /src | |
parent | ea898d7a7b85aeb677f81f13784232c99b61808a (diff) |
Variable fixes (#67)V2.0.3
* Reproduce if throws UnknownOperatorException
* Fix variable detection
* Adding IncorrectNumberOfFunctionParametersException
* Removing tabs
* Better exception message text
Diffstat (limited to 'src')
-rw-r--r-- | src/NXP/Classes/Calculator.php | 2 | ||||
-rw-r--r-- | src/NXP/Classes/CustomFunction.php | 5 | ||||
-rw-r--r-- | src/NXP/Classes/Operator.php | 1 | ||||
-rw-r--r-- | src/NXP/Classes/Tokenizer.php | 16 | ||||
-rw-r--r-- | src/NXP/Exception/IncorrectNumberOfFunctionParametersException.php | 16 |
5 files changed, 29 insertions, 11 deletions
diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index 94c6933..bcb4b4e 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -77,7 +77,7 @@ class Calculator } $result = array_pop($stack); if ($result === null || !empty($stack)) { - throw new IncorrectExpressionException(); + throw new IncorrectExpressionException('Stack must be empty'); } return $result->value; } diff --git a/src/NXP/Classes/CustomFunction.php b/src/NXP/Classes/CustomFunction.php index 06e21ef..f127ad2 100644 --- a/src/NXP/Classes/CustomFunction.php +++ b/src/NXP/Classes/CustomFunction.php @@ -3,7 +3,7 @@ namespace NXP\Classes; -use NXP\Exception\IncorrectExpressionException; +use NXP\Exception\IncorrectNumberOfFunctionParametersException; use ReflectionException; use ReflectionFunction; @@ -30,6 +30,7 @@ class CustomFunction * @param callable $function * @param int $places * @throws ReflectionException + * @throws IncorrectNumberOfFunctionParametersException */ public function __construct(string $name, callable $function, ?int $places = null) { @@ -46,7 +47,7 @@ class CustomFunction public function execute(array &$stack) : Token { if (count($stack) < $this->places) { - throw new IncorrectExpressionException(); + throw new IncorrectNumberOfFunctionParametersException($this->name); } $args = []; for ($i = 0; $i < $this->places; $i++) { diff --git a/src/NXP/Classes/Operator.php b/src/NXP/Classes/Operator.php index 86df549..00485bc 100644 --- a/src/NXP/Classes/Operator.php +++ b/src/NXP/Classes/Operator.php @@ -46,7 +46,6 @@ class Operator $this->isRightAssoc = $isRightAssoc; $this->priority = $priority; $this->function = $function; - $this->function = $function; $reflection = new ReflectionFunction($function); $this->places = $reflection->getNumberOfParameters(); } diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php index 6b14677..203724a 100644 --- a/src/NXP/Classes/Tokenizer.php +++ b/src/NXP/Classes/Tokenizer.php @@ -157,14 +157,16 @@ class Tokenizer } $this->emptyNumberBufferAsLiteral(); $this->emptyStrBufferAsVariable(); - if (count($this->tokens) > 0) { - if ($this->tokens[count($this->tokens) - 1]->type === Token::Operator) { - $this->tokens[count($this->tokens) - 1]->value .= $ch; + if ($ch != '$') { + if (count($this->tokens) > 0) { + if ($this->tokens[count($this->tokens) - 1]->type === Token::Operator) { + $this->tokens[count($this->tokens) - 1]->value .= $ch; + } else { + $this->tokens[] = new Token(Token::Operator, $ch); + } } else { $this->tokens[] = new Token(Token::Operator, $ch); } - } else { - $this->tokens[] = new Token(Token::Operator, $ch); } $this->allowNegative = true; } @@ -251,12 +253,12 @@ class Tokenizer break; case Token::Operator: if (!array_key_exists($token->value, $this->operators)) { - throw new UnknownOperatorException(); + throw new UnknownOperatorException($token->value); } $op1 = $this->operators[$token->value]; while ($stack->count() > 0 && $stack->top()->type === Token::Operator) { if (!array_key_exists($stack->top()->value, $this->operators)) { - throw new UnknownOperatorException(); + throw new UnknownOperatorException($stack->top()->value); } $op2 = $this->operators[$stack->top()->value]; if ($op2->priority >= $op1->priority) { diff --git a/src/NXP/Exception/IncorrectNumberOfFunctionParametersException.php b/src/NXP/Exception/IncorrectNumberOfFunctionParametersException.php new file mode 100644 index 0000000..4c66bdf --- /dev/null +++ b/src/NXP/Exception/IncorrectNumberOfFunctionParametersException.php @@ -0,0 +1,16 @@ +<?php + +/** + * This file is part of the MathExecutor package + * + * (c) Alexander Kiryukhin + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code + */ + +namespace NXP\Exception; + +class IncorrectNumberOfFunctionParametersException extends MathExecutorException +{ +} |