From 00def17f0e9183544813427cddbdaed851986309 Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Wed, 12 Sep 2018 11:55:31 -0400 Subject: Support for double quoted strings Changed array() to [] syntax. Added variable in question to unknown variable exception. Added getVar and getVars accessor functions. Added getOperators and getFunctions accessor functions for completeness. Extended all Exceptions off MathExecutorException. --- src/NXP/Classes/Calculator.php | 8 ++++-- src/NXP/Classes/Lexer.php | 10 +++++-- src/NXP/Classes/Token/TokenFunction.php | 2 +- src/NXP/Classes/Token/TokenString.php | 25 +++++++++++++++++ src/NXP/Classes/TokenFactory.php | 50 ++++++++++++++++++++++----------- 5 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 src/NXP/Classes/Token/TokenString.php (limited to 'src/NXP/Classes') diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php index 41e0d9f..bfa7015 100644 --- a/src/NXP/Classes/Calculator.php +++ b/src/NXP/Classes/Calculator.php @@ -13,6 +13,7 @@ namespace NXP\Classes; use NXP\Classes\Token\InterfaceOperator; use NXP\Classes\Token\TokenFunction; use NXP\Classes\Token\TokenNumber; +use NXP\Classes\Token\TokenString; use NXP\Classes\Token\TokenVariable; use NXP\Exception\IncorrectExpressionException; use NXP\Exception\UnknownVariableException; @@ -32,15 +33,18 @@ class Calculator */ public function calculate($tokens, $variables) { - $stack = array(); + $stack = []; foreach ($tokens as $token) { if ($token instanceof TokenNumber) { array_push($stack, $token); } + if ($token instanceof TokenString) { + array_push($stack, $token); + } if ($token instanceof TokenVariable) { $variable = $token->getValue(); if (!array_key_exists($variable, $variables)) { - throw new UnknownVariableException(); + throw new UnknownVariableException($variable); } $value = $variables[$variable]; array_push($stack, new TokenNumber($value)); diff --git a/src/NXP/Classes/Lexer.php b/src/NXP/Classes/Lexer.php index e541732..82b2c53 100644 --- a/src/NXP/Classes/Lexer.php +++ b/src/NXP/Classes/Lexer.php @@ -17,6 +17,7 @@ use NXP\Classes\Token\TokenLeftBracket; use NXP\Classes\Token\TokenNumber; use NXP\Classes\Token\TokenRightBracket; use NXP\Classes\Token\TokenVariable; +use NXP\Classes\Token\TokenString; use NXP\Exception\IncorrectBracketsException; use NXP\Exception\IncorrectExpressionException; @@ -42,7 +43,7 @@ class Lexer */ public function stringToTokensStream($input) { - $matches = array(); + $matches = []; preg_match_all($this->tokenFactory->getTokenParserRegex(), $input, $matches); $tokenFactory = $this->tokenFactory; $tokensStream = array_map( @@ -62,10 +63,13 @@ class Lexer */ public function buildReversePolishNotation($tokensStream) { - $output = array(); - $stack = array(); + $output = []; + $stack = []; foreach ($tokensStream as $token) { + if ($token instanceof TokenString) { + $output[] = $token; + } if ($token instanceof TokenNumber) { $output[] = $token; } diff --git a/src/NXP/Classes/Token/TokenFunction.php b/src/NXP/Classes/Token/TokenFunction.php index 23f64bd..2ed8ace 100644 --- a/src/NXP/Classes/Token/TokenFunction.php +++ b/src/NXP/Classes/Token/TokenFunction.php @@ -29,7 +29,7 @@ class TokenFunction extends AbstractContainerToken implements InterfaceFunction */ public function execute(&$stack) { - $args = array(); + $args = []; list($places, $function) = $this->value; for ($i = 0; $i < $places; $i++) { array_push($args, array_pop($stack)->getValue()); diff --git a/src/NXP/Classes/Token/TokenString.php b/src/NXP/Classes/Token/TokenString.php new file mode 100644 index 0000000..cab0711 --- /dev/null +++ b/src/NXP/Classes/Token/TokenString.php @@ -0,0 +1,25 @@ + + */ +class TokenString extends AbstractContainerToken +{ + /** + * @return string + */ + public static function getRegex() + { + return '"([^"]|"")*"'; + } +} diff --git a/src/NXP/Classes/TokenFactory.php b/src/NXP/Classes/TokenFactory.php index 19ba1cf..dbe6624 100644 --- a/src/NXP/Classes/TokenFactory.php +++ b/src/NXP/Classes/TokenFactory.php @@ -17,6 +17,7 @@ use NXP\Classes\Token\TokenLeftBracket; use NXP\Classes\Token\TokenNumber; use NXP\Classes\Token\TokenRightBracket; use NXP\Classes\Token\TokenVariable; +use NXP\Classes\Token\TokenString; use NXP\Exception\UnknownFunctionException; use NXP\Exception\UnknownOperatorException; use NXP\Exception\UnknownTokenException; @@ -31,24 +32,36 @@ class TokenFactory * * @var array */ - protected $operators = array(); + protected $operators = []; /** * Available functions * * @var array */ - protected $functions = array(); + protected $functions = []; /** * Add function - * @param $name - * @param $function - * @param $places + * @param string $name + * @param callable $function + * @param int $places */ - public function addFunction($name, $function, $places = 1) + public function addFunction($name, callable $function, $places = 1) { - $this->functions[$name] = array($places, $function); + $this->functions[$name] = [$places, $function]; + } + + + /** + * get functions + * + * @return array containing callback and places indexed by + * function name + */ + public function getFunctions() + { + return $this->functions; } /** @@ -61,7 +74,7 @@ class TokenFactory $class = new \ReflectionClass($operatorClass); if (!in_array('NXP\Classes\Token\InterfaceToken', $class->getInterfaceNames())) { - throw new UnknownOperatorException; + throw new UnknownOperatorException($operatorClass); } $this->operators[] = $operatorClass; @@ -69,13 +82,13 @@ class TokenFactory } /** - * Add variable - * @param string $name - * @param mixed $value + * Get registered operators + * + * @return array of operator class names */ - public function addVariable($name, $value) + public function getOperators() { - + return $this->operators; } /** @@ -89,8 +102,9 @@ class TokenFactory } return sprintf( - '/(%s)|([%s])|(%s)|(%s)|([%s%s%s])/i', + '/(%s)|(%s)|([%s])|(%s)|(%s)|([%s%s%s])/i', TokenNumber::getRegex(), + TokenString::getRegex(), $operatorsRegex, TokenFunction::getRegex(), TokenVariable::getRegex(), @@ -119,6 +133,10 @@ class TokenFactory return new TokenRightBracket(); } + if ($token[0] == '"') { + return new TokenString(str_replace('"', '', $token)); + } + if ($token == ',') { return new TokenComma(); } @@ -140,10 +158,10 @@ class TokenFactory if (isset($this->functions[$token])) { return new TokenFunction($this->functions[$token]); } else { - throw new UnknownFunctionException(); + throw new UnknownFunctionException($token); } } - throw new UnknownTokenException(); + throw new UnknownTokenException($token); } } -- cgit v1.2.3 From 2722a5201d326317e391ae6c0e2fa7e0c2b537b4 Mon Sep 17 00:00:00 2001 From: Bruce Wells Date: Wed, 12 Sep 2018 13:02:03 -0400 Subject: Fixed merge error --- src/NXP/Classes/TokenFactory.php | 13 ------------- 1 file changed, 13 deletions(-) (limited to 'src/NXP/Classes') diff --git a/src/NXP/Classes/TokenFactory.php b/src/NXP/Classes/TokenFactory.php index e5aa865..d70dd55 100644 --- a/src/NXP/Classes/TokenFactory.php +++ b/src/NXP/Classes/TokenFactory.php @@ -52,19 +52,6 @@ class TokenFactory $this->functions[$name] = [$places, $function]; } - - /** - * get functions - * - * @return array containing callback and places indexed by - * function name - */ - public function getFunctions() - { - return $this->functions; - } - - /** * get functions * -- cgit v1.2.3