Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions reports/evalwise.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,15 @@ public function set_data($data): void {
* @return array|false|mixed|null
*/
public function pfx($tokens, array $vars = []) {

if ($tokens === false) {
return false;
}

$stack = new EvalMathStack;

foreach ($tokens as $token) {

// If the token is a function, pop arguments off the stack, hand them to the function, and push the result back on.
if (is_array($token)) {

// It's a function!
$fnn = $token['fnn'];
$count = $token['argcount'];
Expand Down Expand Up @@ -110,19 +107,19 @@ public function pfx($tokens, array $vars = []) {
return $this->trigger("internal error");
}
$stack->push($res);
} else if (array_key_exists($fnn, $this->f)) {
// User function.

} else if (array_key_exists($fnn, $this->f)) { // User function.
// Get args.
$args = [];
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');
}
}

// Yay recursion!
$stack->push($this->pfx($this->f[$fnn]['func'], $args));
}

} else if (in_array($token, ['+', '-', '*', '/', '^'], true)) {
// If the token is a binary operator, pop two values off the stack, do the operation, and push the result back on.
if (is_null($op2 = $stack->pop())) {
Expand Down Expand Up @@ -166,14 +163,13 @@ public function pfx($tokens, array $vars = []) {
}
}
}

// When we're out of tokens, the stack should have a single element, the final result.
if ($stack->count != 1) {
return $this->trigger("internal error");
}

$last = $stack->pop();

return $this->data[$last] ?? false;
}

}
}