diff options
author | Bruce Wells <bruce.wells@simparel.com> | 2019-11-27 19:19:42 +0300 |
---|---|---|
committer | Bruce Wells <bruce.wells@simparel.com> | 2019-11-27 19:19:42 +0300 |
commit | 44a13487b5a89951d244ab7a5e723cf7ec893a54 (patch) | |
tree | 88cafa1f3fe84e1db662470851a68947973cadda /src/NXP/MathExecutor.php | |
parent | adf43bc705fd2d839a639c162f53407434867206 (diff) | |
parent | f975f0bfbc6ac28f0a868b2c237cca071c37c39e (diff) |
Merge branch 'master' of https://github.com/neonxp/MathExecutor
# Conflicts:
# .gitignore
# tests/MathTest.php
Diffstat (limited to 'src/NXP/MathExecutor.php')
-rw-r--r-- | src/NXP/MathExecutor.php | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index 6325d35..2a5f9fb 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -223,13 +223,14 @@ class MathExecutor */ public function execute($expression) { - if (!array_key_exists($expression, $this->cache)) { + $cachekey = (string)$expression; + if (!array_key_exists($cachekey, $this->cache)) { $lexer = new Lexer($this->tokenFactory); $tokensStream = $lexer->stringToTokensStream($expression); $tokens = $lexer->buildReversePolishNotation($tokensStream); - $this->cache[$expression] = $tokens; + $this->cache[$cachekey] = $tokens; } else { - $tokens = $this->cache[$expression]; + $tokens = $this->cache[$cachekey]; } $calculator = new Calculator(); $result = $calculator->calculate($tokens, $this->variables); @@ -263,6 +264,14 @@ class MathExecutor 'NXP\Classes\Token\TokenMultiply', 'NXP\Classes\Token\TokenDivision', 'NXP\Classes\Token\TokenDegree', + 'NXP\Classes\Token\TokenAnd', + 'NXP\Classes\Token\TokenOr', + 'NXP\Classes\Token\TokenEqual', + 'NXP\Classes\Token\TokenUnequal', + 'NXP\Classes\Token\TokenGreaterThanOrEqual', + 'NXP\Classes\Token\TokenGreaterThan', + 'NXP\Classes\Token\TokenLessThanOrEqual', + 'NXP\Classes\Token\TokenLessThan', ]; } @@ -296,6 +305,18 @@ class MathExecutor 'avg' => function ($arg1, $arg2) { return ($arg1 + $arg2) / 2; }, + 'if' => function ($expr, $trueval, $falseval) { + if ($expr === true || $expr === false) { + $exres = $expr; + } else { + $exres = $this->execute($expr); + } + if ($exres) { + return $this->execute($trueval); + } else { + return $this->execute($falseval); + } + } ]; } |