*/ class Calculator { /** * @param array $tokens Tokens in reverse polish notation * @return number * @throws \NXP\Exception\IncorrectExpressionException */ public function calculate($tokens) { $stack = array(); foreach ($tokens as $token) { if ($token instanceof TokenNumber) { array_push($stack, $token); } if ($token instanceof InterfaceOperator || $token instanceof TokenFunction) { array_push($stack, $token->execute($stack)); } } $result = array_pop($stack); if (!empty($stack)) { throw new IncorrectExpressionException(); } return $result->getValue(); } }