aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/NXP/Classes/Tokenizer.php70
-rw-r--r--src/NXP/MathExecutor.php7
2 files changed, 46 insertions, 31 deletions
diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php
index 203724a..bb6fa15 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 minus
+ if ($ch == '-') {
+ if ($this->allowNegative) {
+ $this->allowNegative = false;
+ $this->tokens[] = new Token(Token::Operator, '`');
+ 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 ca27539..b8e8a65 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -99,6 +99,13 @@ class MathExecutor
170,
false
],
+ '`' => [ // unary minus token
+ function ($a) {
+ return 0 - $a;
+ },
+ 200,
+ false
+ ],
'*' => [
function ($a, $b) {
return $a * $b;