<?php
namespace NXP\Classes;
use NXP\Exception\IncorrectExpressionException;
use NXP\Exception\UnknownFunctionException;
use NXP\Exception\UnknownOperatorException;
use NXP\Exception\UnknownVariableException;
class Calculator
{
public function __construct(private array $functions, private array $operators)
{
}
public function calculate(array $tokens, array $variables, ?callable $onVarNotFound = null)
{
$stack = [];
foreach ($tokens as $token) {
if (Token::Literal === $token->type || Token::String === $token->type) {
$stack[] = $token;
} elseif (Token::Variable === $token->type) {
$variable = $token->value;
$value = null;
if (\array_key_exists($variable, $variables)) {
$value = $variables[$variable];
} elseif ($onVarNotFound) {
$value = \call_user_func($onVarNotFound, $variable);
} else {
throw new UnknownVariableException($variable);
}
$stack[] = new Token(Token::Literal, $value, $variable);
} elseif (Token::Function === $token->type) {
if (! \array_key_exists($token->value, $this->functions)) {
throw new UnknownFunctionException($token->value);
}
$stack[] = $this->functions[$token->value]->execute($stack, $token->paramCount);
} elseif (Token::Operator === $token->type) {
if (! \array_key_exists($token->value, $this->operators)) {
throw new UnknownOperatorException($token->value);
}
$stack[] = $this->operators[$token->value]->execute($stack);
}
}
$result = \array_pop($stack);
if (null === $result || ! empty($stack)) {
throw new IncorrectExpressionException('Stack must be empty');
}
return $result->value;
}
}