diff options
author | Fatih Kızmaz <barka_21@hotmail.com> | 2022-05-17 00:57:37 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-17 00:57:37 +0300 |
commit | 5d6b4a5dfdea6d4e2814391afcfcd9cf4259c046 (patch) | |
tree | 7b793b462a6d59748eb0a8faff18ad81ee4dae4e /src | |
parent | 2874b1134189634853c3afaaf46c045f66d51ab9 (diff) |
Full support for arrays => min, max and avg funcs accept array argument. Also array function is defined which return arguments as array. Square bracket arrays are also supported. (#108)
valid expression -> "max([1,2,3])"
valid expression -> "max(array(1,2,3))"
valid expression -> "max($ages_arr)"
valid expression -> "max(ages_arr())"
Diffstat (limited to 'src')
-rw-r--r-- | src/NXP/Classes/Tokenizer.php | 11 | ||||
-rw-r--r-- | src/NXP/MathExecutor.php | 3 |
2 files changed, 11 insertions, 3 deletions
diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php index 23e1cc9..c8b415d 100644 --- a/src/NXP/Classes/Tokenizer.php +++ b/src/NXP/Classes/Tokenizer.php @@ -76,6 +76,13 @@ class Tokenizer continue 2; + case '[' === $ch: + $this->tokens[] = new Token(Token::Function, 'array'); + $this->allowNegative = true; + $this->tokens[] = new Token(Token::LeftParenthesis, ''); + + continue 2; + case ' ' == $ch || "\n" == $ch || "\r" == $ch || "\t" == $ch: $this->tokens[] = new Token(Token::Space, ''); @@ -140,7 +147,7 @@ class Tokenizer break; - case $this->isRP($ch): + case $this->isRP($ch) || ']' === $ch : $this->emptyNumberBufferAsLiteral(); $this->emptyStrBufferAsVariable(); $this->allowNegative = false; @@ -196,8 +203,8 @@ class Tokenizer } /** - * @throws UnknownOperatorException * @throws IncorrectBracketsException + * @throws UnknownOperatorException * @return Token[] Array of tokens in revers polish notation */ public function buildReversePolishNotation() : array diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index e7e8259..dd90e22 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -469,7 +469,8 @@ class MathExecutor 'tan' => static fn($arg) => \tan($arg), 'tanh' => static fn($arg) => \tanh($arg), 'tn' => static fn($arg) => \tan($arg), - 'tg' => static fn($arg) => \tan($arg) + 'tg' => static fn($arg) => \tan($arg), + 'array' => static fn(...$args) => [...$args] ]; } |