protected function WebformEvalMath::pfx in Webform Calculator 7.2
1 call to WebformEvalMath::pfx()
- WebformEvalMath::evaluate in ./matheval.class.php
File
- ./matheval.class.php, line 296
Class
- WebformEvalMath
Code
protected function pfx($tokens, $vars = array()) {
if ($tokens == false) {
return false;
}
$stack = new WebformEvalMathStack();
foreach ($tokens as $token) {
if (in_array($token, array(
'+',
'-',
'*',
'/',
'^',
))) {
if (is_null($op2 = $stack
->pop())) {
return $this
->trigger("internal error");
}
if (is_null($op1 = $stack
->pop())) {
return $this
->trigger("internal error");
}
switch ($token) {
case '+':
$stack
->push($op1 + $op2);
break;
case '-':
$stack
->push($op1 - $op2);
break;
case '*':
$stack
->push($op1 * $op2);
break;
case '/':
if ($op2 == 0) {
return $this
->trigger("division by zero");
}
$stack
->push($op1 / $op2);
break;
case '^':
$stack
->push(pow($op1, $op2));
break;
}
}
elseif ($token == "_") {
$stack
->push(-1 * $stack
->pop());
}
elseif (preg_match("/^([a-z]\\w*)\\(\$/", $token, $matches)) {
$fnn = $matches[1];
if (in_array($fnn, $this->fb)) {
if (is_null($op1 = $stack
->pop())) {
return $this
->trigger("internal error");
}
$fnn = preg_replace("/^arc/", "a", $fnn);
if ($fnn == 'ln') {
$fnn = 'log';
}
$stack
->push($fnn($op1));
}
elseif (array_key_exists($fnn, $this->f)) {
$args = array();
for ($i = count($this->f[$fnn]['args']) - 1; $i >= 0; $i--) {
if (is_null($args[$this->f[$fnn]['args'][$i]] = $stack
->pop())) {
return $this
->trigger("internal error");
}
}
$stack
->push($this
->pfx($this->f[$fnn]['func'], $args));
}
}
else {
if (is_numeric($token)) {
$stack
->push($token);
}
elseif (array_key_exists($token, $this->v)) {
$stack
->push($this->v[$token]);
}
elseif (array_key_exists($token, $vars)) {
$stack
->push($vars[$token]);
}
else {
return $this
->trigger("undefined variable '{$token}'");
}
}
}
if ($stack->count != 1) {
return $this
->trigger("internal error");
}
return $stack
->pop();
}