aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kiryukhin <alexander@kiryukhin.su>2018-09-12 20:05:09 +0300
committerGitHub <noreply@github.com>2018-09-12 20:05:09 +0300
commit4a672cfd94c07e1821227f27fd1edd2217685136 (patch)
treec50b777d06d9e9eeff6aeb59005c389ddf9a552b
parent76d1b4b8f02d555e4b4f4fd4d641597f0f6b5f4e (diff)
parent2722a5201d326317e391ae6c0e2fa7e0c2b537b4 (diff)
Merge pull request #28 from phpfui/support_for_double_quoted_strings
Support for double quoted strings
-rw-r--r--src/NXP/Classes/Calculator.php6
-rw-r--r--src/NXP/Classes/Lexer.php10
-rw-r--r--src/NXP/Classes/Token/TokenFunction.php2
-rw-r--r--src/NXP/Classes/Token/TokenString.php25
-rw-r--r--src/NXP/Classes/TokenFactory.php33
-rw-r--r--src/NXP/Exception/IncorrectBracketsException.php2
-rw-r--r--src/NXP/Exception/IncorrectExpressionException.php2
-rw-r--r--src/NXP/Exception/UnknownFunctionException.php2
-rw-r--r--src/NXP/Exception/UnknownOperatorException.php2
-rw-r--r--src/NXP/Exception/UnknownTokenException.php2
-rw-r--r--src/NXP/Exception/UnknownVariableException.php2
-rw-r--r--src/NXP/MathExecutor.php38
-rw-r--r--tests/MathTest.php42
13 files changed, 111 insertions, 57 deletions
diff --git a/src/NXP/Classes/Calculator.php b/src/NXP/Classes/Calculator.php
index b584d69..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,11 +33,14 @@ 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)) {
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 @@
+<?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\Classes\Token;
+
+/**
+ * @author Bruce Wells <brucekwells@gmail.com>
+ */
+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 85c4a62..d70dd55 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,27 +32,26 @@ 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
*
@@ -91,16 +91,6 @@ class TokenFactory
}
/**
- * Add variable
- * @param string $name
- * @param mixed $value
- */
- public function addVariable($name, $value)
- {
-
- }
-
- /**
* @return string
*/
public function getTokenParserRegex()
@@ -111,8 +101,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(),
@@ -141,6 +132,10 @@ class TokenFactory
return new TokenRightBracket();
}
+ if ($token[0] == '"') {
+ return new TokenString(str_replace('"', '', $token));
+ }
+
if ($token == ',') {
return new TokenComma();
}
diff --git a/src/NXP/Exception/IncorrectBracketsException.php b/src/NXP/Exception/IncorrectBracketsException.php
index e2aaed9..b3e8315 100644
--- a/src/NXP/Exception/IncorrectBracketsException.php
+++ b/src/NXP/Exception/IncorrectBracketsException.php
@@ -14,6 +14,6 @@ namespace NXP\Exception;
/**
* @author Alexander Kiryukhin <alexander@symdev.org>
*/
-class IncorrectBracketsException extends \Exception
+class IncorrectBracketsException extends MathExecutorException
{
}
diff --git a/src/NXP/Exception/IncorrectExpressionException.php b/src/NXP/Exception/IncorrectExpressionException.php
index ad5bc42..6f579c1 100644
--- a/src/NXP/Exception/IncorrectExpressionException.php
+++ b/src/NXP/Exception/IncorrectExpressionException.php
@@ -14,6 +14,6 @@ namespace NXP\Exception;
/**
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
-class IncorrectExpressionException extends \Exception
+class IncorrectExpressionException extends MathExecutorException
{
}
diff --git a/src/NXP/Exception/UnknownFunctionException.php b/src/NXP/Exception/UnknownFunctionException.php
index 5bb3658..e8ff0c0 100644
--- a/src/NXP/Exception/UnknownFunctionException.php
+++ b/src/NXP/Exception/UnknownFunctionException.php
@@ -14,6 +14,6 @@ namespace NXP\Exception;
/**
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
-class UnknownFunctionException extends \Exception
+class UnknownFunctionException extends MathExecutorException
{
}
diff --git a/src/NXP/Exception/UnknownOperatorException.php b/src/NXP/Exception/UnknownOperatorException.php
index b6617c3..acb4b98 100644
--- a/src/NXP/Exception/UnknownOperatorException.php
+++ b/src/NXP/Exception/UnknownOperatorException.php
@@ -14,6 +14,6 @@ namespace NXP\Exception;
/**
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
-class UnknownOperatorException extends \Exception
+class UnknownOperatorException extends MathExecutorException
{
}
diff --git a/src/NXP/Exception/UnknownTokenException.php b/src/NXP/Exception/UnknownTokenException.php
index b8a593f..bba28a4 100644
--- a/src/NXP/Exception/UnknownTokenException.php
+++ b/src/NXP/Exception/UnknownTokenException.php
@@ -14,6 +14,6 @@ namespace NXP\Exception;
/**
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
-class UnknownTokenException extends \Exception
+class UnknownTokenException extends MathExecutorException
{
}
diff --git a/src/NXP/Exception/UnknownVariableException.php b/src/NXP/Exception/UnknownVariableException.php
index 8de9493..8c215db 100644
--- a/src/NXP/Exception/UnknownVariableException.php
+++ b/src/NXP/Exception/UnknownVariableException.php
@@ -14,6 +14,6 @@ namespace NXP\Exception;
/**
* @author Alexander Kiryukhin <alexander@symdev.org>
*/
-class UnknownVariableException extends \Exception
+class UnknownVariableException extends MathExecutorException
{
}
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index c5c7add..d62d07a 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -15,6 +15,7 @@ use NXP\Classes\Calculator;
use NXP\Classes\Lexer;
use NXP\Classes\Token;
use NXP\Classes\TokenFactory;
+use NXP\Exception\UnknownVariableException;
/**
* Class MathExecutor
@@ -27,7 +28,7 @@ class MathExecutor
*
* @var array
*/
- private $variables = array();
+ private $variables = [];
/**
* @var TokenFactory
@@ -37,7 +38,7 @@ class MathExecutor
/**
* @var array
*/
- private $cache = array();
+ private $cache = [];
/**
* Base math operators
@@ -75,10 +76,36 @@ class MathExecutor
$this->tokenFactory->addFunction('max', 'max', 2);
$this->tokenFactory->addFunction('avg', function($arg1, $arg2) { return ($arg1 + $arg2) / 2; }, 2);
- $this->setVars(array(
+ $this->setVars([
'pi' => 3.14159265359,
'e' => 2.71828182846
- ));
+ ]);
+ }
+
+ /**
+ * Get all vars
+ *
+ * @return array
+ */
+ public function getVars()
+ {
+ return $this->variables;
+ }
+
+ /**
+ * Get a specific var
+ *
+ * @param string $variable
+ * @return integer|float
+ * @throws UnknownVariableException
+ */
+ public function getVar($variable)
+ {
+ if (! isset($this->variables[$variable])) {
+ throw new UnknownVariableException("Variable ({$variable}) not set");
+ }
+
+ return $this->variables[$variable];
}
/**
@@ -86,7 +113,6 @@ class MathExecutor
*
* @param string $variable
* @param integer|float $value
- * @throws \Exception
* @return MathExecutor
*/
public function setVar($variable, $value)
@@ -138,7 +164,7 @@ class MathExecutor
*/
public function removeVars()
{
- $this->variables = array();
+ $this->variables = [];
return $this;
}
diff --git a/tests/MathTest.php b/tests/MathTest.php
index 9bf8d97..db6e486 100644
--- a/tests/MathTest.php
+++ b/tests/MathTest.php
@@ -27,7 +27,7 @@ class MathTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($calculator->execute($expression), $phpResult);
}
- public function testZeroDevision()
+ public function testZeroDivision()
{
$calculator = new MathExecutor();
$this->assertEquals($calculator->execute('1 / 0'), 0);
@@ -44,35 +44,35 @@ class MathTest extends \PHPUnit_Framework_TestCase
*/
public function providerExpressions()
{
- return array(
- array('0.1 + 0.2'),
- array('1 + 2'),
+ return [
+ ['0.1 + 0.2'],
+ ['1 + 2'],
- array('0.1 - 0.2'),
- array('1 - 2'),
+ ['0.1 - 0.2'],
+ ['1 - 2'],
- array('0.1 * 2'),
- array('1 * 2'),
+ ['0.1 * 2'],
+ ['1 * 2'],
- array('0.1 / 0.2'),
- array('1 / 2'),
+ ['0.1 / 0.2'],
+ ['1 / 2'],
- array('2 * 2 + 3 * 3'),
+ ['2 * 2 + 3 * 3'],
- array('1 + 0.6 - 3 * 2 / 50'),
+ ['1 + 0.6 - 3 * 2 / 50'],
- array('(5 + 3) * -1'),
+ ['(5 + 3) * -1'],
- array('2+2*2'),
- array('(2+2)*2'),
- array('(2+2)*-2'),
- array('(2+-2)*2'),
+ ['2+2*2'],
+ ['(2+2)*2'],
+ ['(2+2)*-2'],
+ ['(2+-2)*2'],
- array('sin(10) * cos(50) / min(10, 20/2)'),
+ ['sin(10) * cos(50) / min(10, 20/2)'],
- array('100500 * 3.5E5'),
- array('100500 * 3.5E-5')
- );
+ ['100500 * 3.5E5'],
+ ['100500 * 3.5E-5'],
+ ];
}
public function testFunction()