function ctools_math_expr::evaluate in Chaos Tool Suite (ctools) 6
Same name and namespace in other branches
- 7 includes/math-expr.inc \ctools_math_expr::evaluate()
1 call to ctools_math_expr::evaluate()
- ctools_math_expr::e in includes/math-expr.inc
File
- includes/math-expr.inc, line 112
Class
- ctools_math_expr
Code
function evaluate($expr) {
$this->last_error = null;
$expr = trim($expr);
if (substr($expr, -1, 1) == ';') {
$expr = substr($expr, 0, strlen($expr) - 1);
}
if (preg_match('/^\\s*([a-z]\\w*)\\s*=\\s*(.+)$/', $expr, $matches)) {
if (in_array($matches[1], $this->vb)) {
return $this
->trigger("cannot assign to constant '{$matches[1]}'");
}
if (($tmp = $this
->pfx($this
->nfx($matches[2]))) === false) {
return false;
}
$this->v[$matches[1]] = $tmp;
return $this->v[$matches[1]];
}
elseif (preg_match('/^\\s*([a-z]\\w*)\\s*\\(\\s*([a-z]\\w*(?:\\s*,\\s*[a-z]\\w*)*)\\s*\\)\\s*=\\s*(.+)$/', $expr, $matches)) {
$fnn = $matches[1];
if (in_array($matches[1], $this->fb)) {
return $this
->trigger("cannot redefine built-in function '{$matches[1]}()'");
}
$args = explode(",", preg_replace("/\\s+/", "", $matches[2]));
if (($stack = $this
->nfx($matches[3])) === false) {
return false;
}
for ($i = 0; $i < count($stack); $i++) {
$token = $stack[$i];
if (preg_match('/^[a-z]\\w*$/', $token) and !in_array($token, $args)) {
if (array_key_exists($token, $this->v)) {
$stack[$i] = $this->v[$token];
}
else {
return $this
->trigger("undefined variable '{$token}' in function definition");
}
}
}
$this->f[$fnn] = array(
'args' => $args,
'func' => $stack,
);
return true;
}
else {
return $this
->pfx($this
->nfx($expr));
}
}