aboutsummaryrefslogtreecommitdiff
path: root/src/NXP
diff options
context:
space:
mode:
Diffstat (limited to 'src/NXP')
-rw-r--r--src/NXP/Classes/Tokenizer.php70
-rw-r--r--src/NXP/MathExecutor.php138
2 files changed, 157 insertions, 51 deletions
diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php
index 203724a..f8056c3 100644
--- a/src/NXP/Classes/Tokenizer.php
+++ b/src/NXP/Classes/Tokenizer.php
@@ -27,15 +27,15 @@ class Tokenizer
/**
* @var string
*/
- private $input = "";
+ private $input = '';
/**
* @var string
*/
- private $numberBuffer = "";
+ private $numberBuffer = '';
/**
* @var string
*/
- private $stringBuffer = "";
+ private $stringBuffer = '';
/**
* @var bool
*/
@@ -74,25 +74,25 @@ class Tokenizer
if ($ch === "'") {
$this->tokens[] = new Token(Token::String, $this->stringBuffer);
$this->inSingleQuotedString = false;
- $this->stringBuffer = "";
+ $this->stringBuffer = '';
continue 2;
}
$this->stringBuffer .= $ch;
continue 2;
case $this->inDoubleQuotedString:
- if ($ch === "\"") {
+ if ($ch === '"') {
$this->tokens[] = new Token(Token::String, $this->stringBuffer);
$this->inDoubleQuotedString = false;
- $this->stringBuffer = "";
+ $this->stringBuffer = '';
continue 2;
}
$this->stringBuffer .= $ch;
continue 2;
- case $ch == " " || $ch == "\n" || $ch == "\r" || $ch == "\t":
- $this->tokens[] = new Token(Token::Space, "");
+ case $ch == ' ' || $ch == "\n" || $ch == "\r" || $ch == "\t":
+ $this->tokens[] = new Token(Token::Space, '');
continue 2;
case $this->isNumber($ch):
- if ($this->stringBuffer != "") {
+ if ($this->stringBuffer != '') {
$this->stringBuffer .= $ch;
continue 2;
}
@@ -100,22 +100,22 @@ class Tokenizer
$this->allowNegative = false;
break;
/** @noinspection PhpMissingBreakStatementInspection */
- case strtolower($ch) === "e":
- if ($this->numberBuffer != "" && strpos($this->numberBuffer, ".") !== false) {
- $this->numberBuffer .= "e";
- $this->allowNegative = true;
+ case strtolower($ch) === 'e':
+ if (strlen($this->numberBuffer) && strpos($this->numberBuffer, '.') !== false) {
+ $this->numberBuffer .= 'e';
+ $this->allowNegative = false;
break;
}
// no break
case $this->isAlpha($ch):
- if ($this->numberBuffer != "") {
+ if (strlen($this->numberBuffer)) {
$this->emptyNumberBufferAsLiteral();
- $this->tokens[] = new Token(Token::Operator, "*");
+ $this->tokens[] = new Token(Token::Operator, '*');
}
$this->allowNegative = false;
$this->stringBuffer .= $ch;
break;
- case $ch == "\"":
+ case $ch == '"':
$this->inDoubleQuotedString = true;
continue 2;
case $ch == "'":
@@ -127,33 +127,41 @@ class Tokenizer
$this->allowNegative = false;
break;
case $this->isLP($ch):
- if ($this->stringBuffer != "") {
+ if ($this->stringBuffer != '') {
$this->tokens[] = new Token(Token::Function, $this->stringBuffer);
- $this->stringBuffer = "";
- } elseif ($this->numberBuffer != "") {
+ $this->stringBuffer = '';
+ } elseif (strlen($this->numberBuffer)) {
$this->emptyNumberBufferAsLiteral();
- $this->tokens[] = new Token(Token::Operator, "*");
+ $this->tokens[] = new Token(Token::Operator, '*');
}
$this->allowNegative = true;
- $this->tokens[] = new Token(Token::LeftParenthesis, "");
+ $this->tokens[] = new Token(Token::LeftParenthesis, '');
break;
case $this->isRP($ch):
$this->emptyNumberBufferAsLiteral();
$this->emptyStrBufferAsVariable();
$this->allowNegative = false;
- $this->tokens[] = new Token(Token::RightParenthesis, "");
+ $this->tokens[] = new Token(Token::RightParenthesis, '');
break;
case $this->isComma($ch):
$this->emptyNumberBufferAsLiteral();
$this->emptyStrBufferAsVariable();
$this->allowNegative = true;
- $this->tokens[] = new Token(Token::ParamSeparator, "");
+ $this->tokens[] = new Token(Token::ParamSeparator, '');
break;
default:
- if ($this->allowNegative && $ch == "-") {
- $this->allowNegative = false;
- $this->numberBuffer .= "-";
- continue 2;
+ // special case for unary operations
+ if ($ch == '-' || $ch == '+') {
+ if ($this->allowNegative) {
+ $this->allowNegative = false;
+ $this->tokens[] = new Token(Token::Operator, $ch == '-' ? 'uNeg' : 'uPos');
+ continue 2;
+ }
+ // could be in exponent, in which case negative should be added to the numberBuffer
+ if ($this->numberBuffer && $this->numberBuffer[strlen($this->numberBuffer) - 1] == 'e') {
+ $this->numberBuffer .= '-';
+ continue 2;
+ }
}
$this->emptyNumberBufferAsLiteral();
$this->emptyStrBufferAsVariable();
@@ -188,9 +196,9 @@ class Tokenizer
private function emptyNumberBufferAsLiteral() : void
{
- if ($this->numberBuffer != "") {
+ if (strlen($this->numberBuffer)) {
$this->tokens[] = new Token(Token::Literal, $this->numberBuffer);
- $this->numberBuffer = "";
+ $this->numberBuffer = '';
}
}
@@ -211,9 +219,9 @@ class Tokenizer
private function emptyStrBufferAsVariable() : void
{
- if ($this->stringBuffer != "") {
+ if ($this->stringBuffer != '') {
$this->tokens[] = new Token(Token::Variable, $this->stringBuffer);
- $this->stringBuffer = "";
+ $this->stringBuffer = '';
}
}
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index b7abcad..a774561 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -16,8 +16,8 @@ use NXP\Classes\CustomFunction;
use NXP\Classes\Operator;
use NXP\Classes\Tokenizer;
use NXP\Exception\DivisionByZeroException;
-use NXP\Exception\UnknownVariableException;
use NXP\Exception\MathExecutorException;
+use NXP\Exception\UnknownVariableException;
use ReflectionException;
/**
@@ -85,28 +85,42 @@ class MathExecutor
protected function defaultOperators() : array
{
return [
- '+' => [
+ '+' => [
function ($a, $b) {
return $a + $b;
},
170,
false
],
- '-' => [
+ '-' => [
function ($a, $b) {
return $a - $b;
},
170,
false
],
- '*' => [
+ 'uPos' => [ // unary positive token
+ function ($a) {
+ return $a;
+ },
+ 200,
+ false
+ ],
+ 'uNeg' => [ // unary minus token
+ function ($a) {
+ return 0 - $a;
+ },
+ 200,
+ false
+ ],
+ '*' => [
function ($a, $b) {
return $a * $b;
},
180,
false
],
- '/' => [
+ '/' => [
function ($a, $b) {
if ($b == 0) {
throw new DivisionByZeroException();
@@ -116,28 +130,28 @@ class MathExecutor
180,
false
],
- '^' => [
+ '^' => [
function ($a, $b) {
return pow($a, $b);
},
220,
true
],
- '&&' => [
+ '&&' => [
function ($a, $b) {
return $a && $b;
},
100,
false
],
- '||' => [
+ '||' => [
function ($a, $b) {
return $a || $b;
},
90,
false
],
- '==' => [
+ '==' => [
function ($a, $b) {
if (is_string($a) || is_string($b)) {
return strcmp($a, $b) == 0;
@@ -148,7 +162,7 @@ class MathExecutor
140,
false
],
- '!=' => [
+ '!=' => [
function ($a, $b) {
if (is_string($a) || is_string($b)) {
return strcmp($a, $b) != 0;
@@ -159,28 +173,28 @@ class MathExecutor
140,
false
],
- '>=' => [
+ '>=' => [
function ($a, $b) {
return $a >= $b;
},
150,
false
],
- '>' => [
+ '>' => [
function ($a, $b) {
return $a > $b;
},
150,
false
],
- '<=' => [
+ '<=' => [
function ($a, $b) {
return $a <= $b;
},
150,
false
],
- '<' => [
+ '<' => [
function ($a, $b) {
return $a < $b;
},
@@ -220,6 +234,36 @@ class MathExecutor
'acosh' => function ($arg) {
return acosh($arg);
},
+ 'arcsin' => function ($arg) {
+ return asin($arg);
+ },
+ 'arcctg' => function ($arg) {
+ return M_PI/2 - atan($arg);
+ },
+ 'arccot' => function ($arg) {
+ return M_PI/2 - atan($arg);
+ },
+ 'arccotan' => function ($arg) {
+ return M_PI/2 - atan($arg);
+ },
+ 'arcsec' => function ($arg) {
+ return acos(1/$arg);
+ },
+ 'arccosec' => function ($arg) {
+ return asin(1/$arg);
+ },
+ 'arccsc' => function ($arg) {
+ return asin(1/$arg);
+ },
+ 'arccos' => function ($arg) {
+ return acos($arg);
+ },
+ 'arctan' => function ($arg) {
+ return atan($arg);
+ },
+ 'arctg' => function ($arg) {
+ return atan($arg);
+ },
'asin' => function ($arg) {
return asin($arg);
},
@@ -247,9 +291,30 @@ class MathExecutor
'cos' => function ($arg) {
return cos($arg);
},
+ 'cosec' => function ($arg) {
+ return 1 / sin($arg);
+ },
+ 'csc' => function ($arg) {
+ return 1 / sin($arg);
+ },
'cosh' => function ($arg) {
return cosh($arg);
},
+ 'ctg' => function ($arg) {
+ return cos($arg) / sin($arg);
+ },
+ 'cot' => function ($arg) {
+ return cos($arg) / sin($arg);
+ },
+ 'cotan' => function ($arg) {
+ return cos($arg) / sin($arg);
+ },
+ 'cotg' => function ($arg) {
+ return cos($arg) / sin($arg);
+ },
+ 'ctn' => function ($arg) {
+ return cos($arg) / sin($arg);
+ },
'decbin' => function ($arg) {
return decbin($arg);
},
@@ -295,6 +360,12 @@ class MathExecutor
'intdiv' => function ($arg1, $arg2) {
return intdiv($arg1, $arg2);
},
+ 'ln' => function ($arg) {
+ return log($arg);
+ },
+ 'lg' => function ($arg) {
+ return log10($arg);
+ },
'log' => function ($arg) {
return log($arg);
},
@@ -317,7 +388,7 @@ class MathExecutor
return pi();
},
'pow' => function ($arg1, $arg2) {
- return pow($arg1, $arg2);
+ return $arg1 ** $arg2;
},
'rad2deg' => function ($arg) {
return rad2deg($arg);
@@ -331,6 +402,9 @@ class MathExecutor
'sinh' => function ($arg) {
return sinh($arg);
},
+ 'sec' => function ($arg) {
+ return 1 / cos($arg);
+ },
'sqrt' => function ($arg) {
return sqrt($arg);
},
@@ -342,6 +416,9 @@ class MathExecutor
},
'tn' => function ($arg) {
return tan($arg);
+ },
+ 'tg' => function ($arg) {
+ return tan($arg);
}
];
}
@@ -349,22 +426,26 @@ class MathExecutor
/**
* Execute expression
*
- * @param $expression
+ * @param string $expression
+ * @param bool $cache
* @return number
- * @throws Exception\IncorrectExpressionException
* @throws Exception\IncorrectBracketsException
+ * @throws Exception\IncorrectExpressionException
* @throws Exception\UnknownOperatorException
- * @throws Exception\UnknownVariableException
+ * @throws UnknownVariableException
*/
- public function execute(string $expression)
+ public function execute(string $expression, bool $cache = true)
{
$cachekey = $expression;
if (!array_key_exists($cachekey, $this->cache)) {
$tokens = (new Tokenizer($expression, $this->operators))->tokenize()->buildReversePolishNotation();
- $this->cache[$cachekey] = $tokens;
+ if ($cache) {
+ $this->cache[$cachekey] = $tokens;
+ }
} else {
$tokens = $this->cache[$cachekey];
}
+
$calculator = new Calculator($this->functions, $this->operators);
return $calculator->calculate($tokens, $this->variables, $this->onVarNotFound);
}
@@ -533,6 +614,23 @@ class MathExecutor
return $this;
}
+ /**
+ * Get cache array with tokens
+ * @return array
+ */
+ public function getCache() : array
+ {
+ return $this->cache;
+ }
+
+ /**
+ * Clear token's cache
+ */
+ public function clearCache() : void
+ {
+ $this->cache = [];
+ }
+
public function __clone()
{
$this->addDefaults();