From cab8e2d38ae1c8c7fb75022f7d9b0539a0a86d4e Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Fri, 15 May 2020 21:51:23 +0300 Subject: Massive refactoring More clean structure Parsing without regular expressions --- src/NXP/Classes/Calculator.php | 61 +++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 15 deletions(-) (limited to 'src/NXP/Classes/Calculator.php') diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index 7d82ecd..21f3178 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -13,10 +13,12 @@ namespace NXP\Classes; use NXP\Classes\Token\InterfaceOperator; use NXP\Classes\Token\TokenFunction; use NXP\Classes\Token\TokenNumber; -use NXP\Classes\Token\TokenStringSingleQuoted; use NXP\Classes\Token\TokenStringDoubleQuoted; +use NXP\Classes\Token\TokenStringSingleQuoted; use NXP\Classes\Token\TokenVariable; use NXP\Exception\IncorrectExpressionException; +use NXP\Exception\UnknownFunctionException; +use NXP\Exception\UnknownOperatorException; use NXP\Exception\UnknownVariableException; /** @@ -24,36 +26,65 @@ use NXP\Exception\UnknownVariableException; */ class Calculator { + /** + * @var CustomFunction[] + */ + private $functions; + + /** + * @var Operator[] + */ + private $operators; + + /** + * Calculator constructor. + * @param CustomFunction[] $functions + * @param Operator[] $operators + */ + public function __construct(array $functions, array $operators) + { + $this->functions = $functions; + $this->operators = $operators; + } + /** * Calculate array of tokens in reverse polish notation - * @param array $tokens - * @param array $variables - * @return number Result - * @throws \NXP\Exception\IncorrectExpressionException - * @throws \NXP\Exception\UnknownVariableException + * @param Token[] $tokens + * @param array $variables + * @return mixed + * @throws IncorrectExpressionException + * @throws UnknownVariableException */ public function calculate($tokens, $variables) { + /** @var Token[] $stack */ $stack = []; foreach ($tokens as $token) { - if ($token instanceof TokenNumber || $token instanceof TokenStringDoubleQuoted || $token instanceof TokenStringSingleQuoted) { + if ($token->type === Token::Literal || $token->type === Token::String) { $stack[] = $token; - } else if ($token instanceof TokenVariable) { - $variable = $token->getValue(); + } else if ($token->type === Token::Variable) { + $variable = $token->value; if (!array_key_exists($variable, $variables)) { throw new UnknownVariableException($variable); } $value = $variables[$variable]; - $stack[] = new TokenNumber($value); - } else if ($token instanceof InterfaceOperator || $token instanceof TokenFunction) { - $stack[] = $token->execute($stack); + $stack[] = new Token(Token::Literal, $value); + } else if ($token->type === Token::Function) { + if (!array_key_exists($token->value, $this->functions)) { + throw new UnknownFunctionException($token->value); + } + $stack[] = $this->functions[$token->value]->execute($stack); + } elseif ($token->type === Token::Operator) { + if (!array_key_exists($token->value, $this->operators)) { + throw new UnknownOperatorException($token->value); + } + $stack[] = $this->operators[$token->value]->execute($stack); } } $result = array_pop($stack); - if ($result === null || ! empty($stack)) { + if ($result === null || !empty($stack)) { throw new IncorrectExpressionException(); } - - return $result->getValue(); + return $result->value; } } -- cgit v1.2.3 From 043058f3c72deae2f4a40eac7e91a03208eaba23 Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Fri, 15 May 2020 21:52:35 +0300 Subject: Cleanup unused imports --- src/NXP/Classes/Calculator.php | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/NXP/Classes/Calculator.php') diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index 21f3178..0db49e1 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -10,12 +10,6 @@ namespace NXP\Classes; -use NXP\Classes\Token\InterfaceOperator; -use NXP\Classes\Token\TokenFunction; -use NXP\Classes\Token\TokenNumber; -use NXP\Classes\Token\TokenStringDoubleQuoted; -use NXP\Classes\Token\TokenStringSingleQuoted; -use NXP\Classes\Token\TokenVariable; use NXP\Exception\IncorrectExpressionException; use NXP\Exception\UnknownFunctionException; use NXP\Exception\UnknownOperatorException; -- cgit v1.2.3 From 1bb9f61423c1eed320e95a1288a146ece410e1f9 Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Tue, 19 May 2020 21:52:26 -0400 Subject: typed parameters and return types --- src/NXP/Classes/Calculator.php | 2 +- src/NXP/Classes/CustomFunction.php | 4 ++-- src/NXP/Classes/Operator.php | 2 +- src/NXP/Classes/Tokenizer.php | 20 ++++++++++---------- src/NXP/MathExecutor.php | 18 +++++++++--------- 5 files changed, 23 insertions(+), 23 deletions(-) (limited to 'src/NXP/Classes/Calculator.php') diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index 0db49e1..c6ccff1 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -49,7 +49,7 @@ class Calculator * @throws IncorrectExpressionException * @throws UnknownVariableException */ - public function calculate($tokens, $variables) + public function calculate(array $tokens, array $variables) { /** @var Token[] $stack */ $stack = []; diff --git a/src/NXP/Classes/CustomFunction.php b/src/NXP/Classes/CustomFunction.php index 944f7d9..636435c 100644 --- a/src/NXP/Classes/CustomFunction.php +++ b/src/NXP/Classes/CustomFunction.php @@ -32,7 +32,7 @@ class CustomFunction * @param int $places * @throws ReflectionException */ - public function __construct(string $name, callable $function, $places = null) + public function __construct(string $name, callable $function, int $places = null) { $this->name = $name; $this->function = $function; @@ -44,7 +44,7 @@ class CustomFunction } } - public function execute(&$stack) + public function execute(array &$stack) : Token { if (count($stack) < $this->places) { throw new IncorrectExpressionException(); diff --git a/src/NXP/Classes/Operator.php b/src/NXP/Classes/Operator.php index 53550a9..c3762ea 100644 --- a/src/NXP/Classes/Operator.php +++ b/src/NXP/Classes/Operator.php @@ -52,7 +52,7 @@ class Operator $this->places = $reflection->getNumberOfParameters(); } - public function execute(&$stack) + public function execute(array &$stack) : Token { if (count($stack) < $this->places) { throw new IncorrectExpressionException(); diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php index b72b869..caf395f 100644 --- a/src/NXP/Classes/Tokenizer.php +++ b/src/NXP/Classes/Tokenizer.php @@ -66,7 +66,7 @@ class Tokenizer $this->operators = $operators; } - public function tokenize() + public function tokenize() : self { foreach (str_split($this->input, 1) as $ch) { switch (true) { @@ -173,17 +173,17 @@ class Tokenizer return $this; } - private function isNumber($ch) + private function isNumber(string $ch) : bool { return $ch >= '0' && $ch <= '9'; } - private function isAlpha($ch) + private function isAlpha(string $ch) : bool { return $ch >= 'a' && $ch <= 'z' || $ch >= 'A' && $ch <= 'Z' || $ch == '_'; } - private function emptyNumberBufferAsLiteral() + private function emptyNumberBufferAsLiteral() : void { if ($this->numberBuffer != "") { $this->tokens[] = new Token(Token::Literal, $this->numberBuffer); @@ -191,22 +191,22 @@ class Tokenizer } } - private function isDot($ch) + private function isDot(string $ch) : bool { return $ch == '.'; } - private function isLP($ch) + private function isLP(string $ch) : bool { return $ch == '('; } - private function isRP($ch) + private function isRP(string $ch) : bool { return $ch == ')'; } - private function emptyStrBufferAsVariable() + private function emptyStrBufferAsVariable() : void { if ($this->stringBuffer != "") { $this->tokens[] = new Token(Token::Variable, $this->stringBuffer); @@ -214,7 +214,7 @@ class Tokenizer } } - private function isComma($ch) + private function isComma(string $ch) : bool { return $ch == ','; } @@ -224,7 +224,7 @@ class Tokenizer * @throws IncorrectBracketsException * @throws UnknownOperatorException */ - public function buildReversePolishNotation() + public function buildReversePolishNotation() : array { $tokens = []; /** @var SplStack $stack */ diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index a0a3f9b..53d8dfd 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -58,7 +58,7 @@ class MathExecutor * Set default operands and functions * @throws ReflectionException */ - protected function addDefaults() + protected function addDefaults() : void { foreach ($this->defaultOperators() as $name => $operator) { list($callable, $priority, $isRightAssoc) = $operator; @@ -75,7 +75,7 @@ class MathExecutor * * @return array of class names */ - protected function defaultOperators() + protected function defaultOperators() : array { return [ '+' => [ @@ -102,7 +102,7 @@ class MathExecutor '/' => [ function ($a, $b) { if ($b == 0) { - throw new DivisionByZeroException(); + throw new DivisionByZeroException(); } return $a / $b; }, @@ -181,7 +181,7 @@ class MathExecutor * @param Operator $operator * @return MathExecutor */ - public function addOperator(Operator $operator) + public function addOperator(Operator $operator) : self { $this->operators[$operator->operator] = $operator; return $this; @@ -193,7 +193,7 @@ class MathExecutor * * @return array */ - protected function defaultFunctions() + protected function defaultFunctions() : array { return [ 'abs' => function ($arg) { @@ -341,9 +341,9 @@ class MathExecutor * @throws Exception\UnknownOperatorException * @throws Exception\UnknownVariableException */ - public function execute($expression) + public function execute(string $expression) { - $cachekey = (string)$expression; + $cachekey = $expression; if (!array_key_exists($cachekey, $this->cache)) { $tokens = (new Tokenizer($expression, $this->operators))->tokenize()->buildReversePolishNotation(); $this->cache[$cachekey] = $tokens; @@ -363,7 +363,7 @@ class MathExecutor * @return MathExecutor * @throws ReflectionException */ - public function addFunction($name, $function = null, $places = null) + public function addFunction(string $name, callable $function = null, int $places = null) : self { $this->functions[$name] = new CustomFunction($name, $function, $places); return $this; @@ -374,7 +374,7 @@ class MathExecutor * * @return array */ - protected function defaultVars() + protected function defaultVars() : array { return [ 'pi' => 3.14159265359, -- cgit v1.2.3