/** * Evaluates the given $expression using the $dataContext as variable source. * * Internally the ExpressionParser uses a infix to postfix conversion to easily * be able to evaluate mathematical expressions * * @param string $expression * @param array $dataContext * @return string evaluated result or an exception of failure */ public static function evaluate($expression, $dataContext) { $outputTokens = ExpLexer::process($expression); $result = ExpParser::parse($outputTokens, $dataContext); return $result->value; }
/** * Tests ExpLexer::process */ public function testProcess() { $actualTokenStream = ExpLexer::process($this->input); $this->assertEquals($this->tokenStream, $actualTokenStream); }
private static function validateTokens($tokenStream) { // No return value --- throws exception when error encountered. $stack = array(); for ($i = 0; $i < count($tokenStream); $i++) { $node = $tokenStream[$i]; switch ($node->type) { case ExpType::$TERNARY: // drop through // drop through case ExpType::$PAREN: // Check balance if (in_array($node->value, ExpLexer::$ENDS)) { $stackLeft = array_pop($stack); if (ExpLexer::$PAIRS[$node->value] != $stackLeft) { throw new ExpLexerException("Unbalanced expression"); } } else { array_push($stack, $node->value); } break; case ExpType::$UNARY_OP: // Check unary operator gramma if (!ExpLexer::hasUnaryLeft($tokenStream, $i)) { throw new ExpLexerException("Mal-format unary operator"); } break; case ExpType::$BINARY_OP: // Check binary operator gramma if (ExpLexer::hasUnaryLeft($tokenStream, $i) || !ExpLexer::hasBinaryRight($tokenStream, $i)) { throw new ExpLexerException("Mal-format binary operator"); } break; case ExpType::$DOT: // Check dot gramma if ($i == count($tokenStream) - 1 || $tokenStream[$i + 1]->type != ExpType::$IDENTITY) { throw new ExpLexerException("Mal-format dot"); } break; case ExpType::$FUNCTION: // Check function gramma if ($i == count($tokenStream) - 1 || $tokenStream[$i + 1]->value != '(') { throw new ExpLexerException("Mal-format function"); } break; } } if (count($stack) != 0) { throw new ExpLexerException("Unbalanced expression"); } }