From 29c5b5006ccb5cb308b9467ca25813b29be8cfc6 Mon Sep 17 00:00:00 2001 From: franksl Date: Tue, 26 Nov 2019 15:00:24 +0100 Subject: Logicandcompare (#50) * TokenFactory: allowing multicharacter tokens * Added logical and compare operators: <, <=, >, >=, ==, !=, ||, && * Fixed operator priorities * Error messages fixes * Fixed operators priority The priorities are assigned by following the php language standard (https://www.php.net/manual/en/language.operators.precedence.php) I've assigned precedence in steps of 10 units by following the linked page: 230 clone new 220 ** 210 ++ -- ~ (int) (float) (string) (array) (object) (bool) @ 200 instanceof 190 ! 180 * / % 170 + - . 160 << >> 150 < <= > >= 140 == != === !== <> <=> 130 & 120 ^ 110 | 100 && 90 || 80 ?? 70 ? : 60 = += -= *= **= /= .= %= &= |= ^= <<= >>= 50 yield from 40 yield 30 and 20 xor 10 or * Added if() function * Cache key fix There are cases where the cache key creation raised an error, for example while evaluating the expression "if(cos(2), cos(2), 0)", because the if() function was passing a float to the MathExecutor:execute() method. --- src/NXP/Classes/TokenFactory.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/NXP/Classes/TokenFactory.php') diff --git a/src/NXP/Classes/TokenFactory.php b/src/NXP/Classes/TokenFactory.php index 74b5789..5aa634a 100644 --- a/src/NXP/Classes/TokenFactory.php +++ b/src/NXP/Classes/TokenFactory.php @@ -133,21 +133,21 @@ class TokenFactory { $operatorsRegex = ''; foreach ($this->operators as $operator) { - $operatorsRegex .= $operator::getRegex(); + $operatorsRegex .= '|(' . $operator::getRegex() . ')'; } - - return sprintf( - '/(%s)|(%s)|(%s)|([%s])|(%s)|(%s)|([%s%s%s])/i', + $s = sprintf( + '/(%s)|(%s)|(%s)|(%s)|(%s)|([%s%s%s])', TokenNumber::getRegex(), TokenStringDoubleQuoted::getRegex(), TokenStringSingleQuoted::getRegex(), - $operatorsRegex, TokenFunction::getRegex(), TokenVariable::getRegex(), TokenLeftBracket::getRegex(), TokenRightBracket::getRegex(), TokenComma::getRegex() ); + $s .= $operatorsRegex . '/i'; + return $s; } /** -- cgit v1.2.3