diff options
author | Fatih Kızmaz <barka_21@hotmail.com> | 2022-05-13 15:55:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-13 15:55:52 +0300 |
commit | e21d59c9def9f25bc6a7c3fbd41e2e126d68b1df (patch) | |
tree | 379ebf22c76d48aa5897bb9db487c60cae542533 /src/NXP/Classes/Tokenizer.php | |
parent | da506a7ce05c6141e3c32f72c351eace38577f89 (diff) |
Support unlimited args for min, max default funcs. (#106)
* Support unlimited args for min, max default funcs.
Default functions max and min were requiring 2 arguments strictly. Now they supoort unlimited args, same as php's min, max funcs.
* Improved functions: support unlimited parameters (see min, max funcs), optional parameters (see round func), parameters with types (see round func, throws IncorrectFunctionParameterException on unmatched type, union types and intersection types not supported because of min php level! there is a todo for this, to support them later @see CustomFunction@execute) Also added unittests for improvements.
* Run php-cs-fixer fix
Diffstat (limited to 'src/NXP/Classes/Tokenizer.php')
-rw-r--r-- | src/NXP/Classes/Tokenizer.php | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php index 32404a2..23e1cc9 100644 --- a/src/NXP/Classes/Tokenizer.php +++ b/src/NXP/Classes/Tokenizer.php @@ -20,7 +20,7 @@ use SplStack; */ class Tokenizer { - /** @var array<Token> */ + /** @var array<Token> */ public array $tokens = []; private string $input = ''; @@ -31,7 +31,7 @@ class Tokenizer private bool $allowNegative = true; - /** @var array<Operator> */ + /** @var array<Operator> */ private array $operators = []; private bool $inSingleQuotedString = false; @@ -99,8 +99,8 @@ class Tokenizer break; } - // no break - // Intentionally fall through + // no break + // Intentionally fall through case $this->isAlpha($ch): if (\strlen($this->numberBuffer)) { $this->emptyNumberBufferAsLiteral(); @@ -196,8 +196,8 @@ class Tokenizer } /** - * @throws IncorrectBracketsException * @throws UnknownOperatorException + * @throws IncorrectBracketsException * @return Token[] Array of tokens in revers polish notation */ public function buildReversePolishNotation() : array @@ -205,6 +205,10 @@ class Tokenizer $tokens = []; /** @var SplStack<Token> $stack */ $stack = new SplStack(); + /** + * @var SplStack<int> $paramCounter + */ + $paramCounter = new SplStack(); foreach ($this->tokens as $token) { switch ($token->type) { @@ -213,9 +217,21 @@ class Tokenizer case Token::String: $tokens[] = $token; + if ($paramCounter->count() > 0 && 0 === $paramCounter->top()) { + $paramCounter->push($paramCounter->pop() + 1); + } + break; case Token::Function: + if ($paramCounter->count() > 0 && 0 === $paramCounter->top()) { + $paramCounter->push($paramCounter->pop() + 1); + } + $stack->push($token); + $paramCounter->push(0); + + break; + case Token::LeftParenthesis: $stack->push($token); @@ -228,6 +244,7 @@ class Tokenizer } $tokens[] = $stack->pop(); } + $paramCounter->push($paramCounter->pop() + 1); break; @@ -270,7 +287,12 @@ class Tokenizer } if ($stack->count() > 0 && Token::Function == $stack->top()->type) { - $tokens[] = $stack->pop(); + /** + * @var Token $f + */ + $f = $stack->pop(); + $f->paramCount = $paramCounter->pop(); + $tokens[] = $f; } break; |