151 $this->last_error =
null;
152 $this->last_error_code =
null;
154 if (substr($expr, - 1, 1) ==
';') {
155 $expr = substr($expr, 0, strlen($expr) - 1);
160 if (preg_match(
'/^\s*([a-z]\w*)\s*=\s*(.+)$/', $expr, $matches)) {
161 if (in_array($matches[1], $this->vb)) {
162 return $this->
trigger(1,
"cannot assign to constant '$matches[1]'", $matches[1]);
164 if (($tmp = $this->
pfx($this->
nfx($matches[2]))) ===
false) {
167 $this->v[$matches[1]] = $tmp;
168 return $this->v[$matches[1]];
171 } elseif (preg_match(
'/^\s*([a-z]\w*)\s*\(\s*([a-z]\w*(?:\s*,\s*[a-z]\w*)*)\s*\)\s*=\s*(.+)$/', $expr, $matches)) {
173 if (in_array($matches[1], $this->fb)) {
174 return $this->
trigger(2,
"cannot redefine built-in function '$matches[1]()'", $matches[1]);
176 $args = explode(
",", preg_replace(
"/\s+/",
"", $matches[2]));
177 if (($stack = $this->
nfx($matches[3])) ===
false) {
180 $nbstack = count($stack);
181 for ($i = 0; $i < $nbstack; $i++) {
183 if (preg_match(
'/^[a-z]\w*$/', $token) and !in_array($token, $args)) {
184 if (array_key_exists($token, $this->v)) {
185 $stack[$i] = $this->v[$token];
187 return $this->
trigger(3,
"undefined variable '$token' in function definition", $token);
191 $this->f[$fnn] = array(
'args' => $args,
'func' => $stack);
195 return $this->
pfx($this->
nfx($expr));
234 private function nfx($expr)
239 $expr = trim(strtolower($expr));
241 $ops = array(
'+',
'-',
'*',
'/',
'^',
'_');
242 $ops_r = array(
'+' => 0,
'-' => 0,
'*' => 0,
'/' => 0,
'^' => 1);
243 $ops_p = array(
'+' => 0,
'-' => 0,
'*' => 1,
'/' => 1,
'_' => 1,
'^' => 2);
245 $expecting_op =
false;
249 if (preg_match(
"/[^\w\s+*^\/()\.,-]/", $expr, $matches)) {
250 return $this->
trigger(4,
"illegal character '".$matches[0].
"'", $matches[0]);
254 $op = substr($expr, $index, 1);
257 $ex = preg_match(
'/^([a-z]\w*\(?|\d+(?:\.\d*)?|\.\d+|\()/', substr($expr, $index), $match);
259 if ($op ==
'-' and !$expecting_op) {
262 } elseif ($op ==
'_') {
263 return $this->
trigger(4,
"illegal character '_'",
"_");
265 } elseif ((in_array($op, $ops) or $ex) and $expecting_op) {
271 while ($stack->count > 0 and ($o2 = $stack->last()) and in_array($o2, $ops) and ($ops_r[$op] ? $ops_p[$op] < $ops_p[$o2] : $ops_p[$op] <= $ops_p[$o2])) {
272 $output[] = $stack->pop();
277 $expecting_op =
false;
279 } elseif ($op ==
')' and $expecting_op) {
280 while (($o2 = $stack->pop()) !=
'(') {
282 return $this->
trigger(5,
"unexpected ')'",
")");
287 if (preg_match(
"/^([a-z]\w*)\($/", $stack->last(2), $matches)) {
289 $arg_count = $stack->pop();
290 $output[] = $stack->pop();
291 if (in_array($fnn, $this->fb)) {
292 if ($arg_count > 1) {
293 return $this->
trigger(6,
"wrong number of arguments ($arg_count given, 1 expected)", array($arg_count, 1));
295 } elseif (array_key_exists($fnn, $this->f)) {
296 if ($arg_count != count($this->f[$fnn][
'args'])) {
297 return $this->
trigger(6,
"wrong number of arguments ($arg_count given, ".count($this->f[$fnn][
'args']).
" expected)", array($arg_count, count($this->f[$fnn][
'args'])));
300 return $this->
trigger(7,
"internal error");
305 } elseif ($op ==
',' and $expecting_op) {
306 while (($o2 = $stack->pop()) !=
'(') {
308 return $this->
trigger(5,
"unexpected ','",
",");
314 if (!preg_match(
"/^([a-z]\w*)\($/", $stack->last(2), $matches)) {
315 return $this->
trigger(5,
"unexpected ','",
",");
317 $stack->push($stack->pop() + 1);
320 $expecting_op =
false;
322 } elseif ($op ==
'(' and !$expecting_op) {
327 } elseif ($ex and !$expecting_op) {
328 $expecting_op =
true;
330 if (preg_match(
"/^([a-z]\w*)\($/", $val, $matches)) {
331 if (in_array($matches[1], $this->fb) or array_key_exists($matches[1], $this->f)) {
335 $expecting_op =
false;
343 $index += strlen($val);
345 } elseif ($op ==
')') {
346 return $this->
trigger(5,
"unexpected ')'",
")");
347 } elseif (in_array($op, $ops) and !$expecting_op) {
348 return $this->
trigger(8,
"unexpected operator '$op'", $op);
350 return $this->
trigger(9,
"an unexpected error occurred");
352 if ($index == strlen($expr)) {
353 if (in_array($op, $ops)) {
354 return $this->
trigger(10,
"operator '$op' lacks operand", $op);
359 while (substr($expr, $index, 1) ==
' ') {
363 while (!is_null($op = $stack->pop())) {
365 return $this->
trigger(11,
"expecting ')'",
")");
380 private function pfx($tokens, $vars = array())
384 foreach ($tokens as $token) {
387 if (in_array($token, array(
'+',
'-',
'*',
'/',
'^'))) {
388 if (is_null($op2 = $stack->pop())) {
389 return $this->
trigger(12,
"internal error");
391 if (is_null($op1 = $stack->pop())) {
392 return $this->
trigger(13,
"internal error");
396 $stack->push($op1 + $op2);
399 $stack->push($op1 - $op2);
402 $stack->push($op1 * $op2);
406 return $this->
trigger(14,
"division by zero");
408 $stack->push($op1 / $op2);
411 $stack->push(pow($op1, $op2));
415 } elseif ($token ==
"_") {
416 $stack->push(-1 * $stack->pop());
418 } elseif (preg_match(
"/^([a-z]\w*)\($/", $token, $matches)) {
420 if (in_array($fnn, $this->fb)) {
421 if (is_null($op1 = $stack->pop())) {
422 return $this->
trigger(15,
"internal error");
424 $fnn = preg_replace(
"/^arc/",
"a", $fnn);
429 eval(
'$stack->push('.$fnn.
'($op1));');
430 } elseif (array_key_exists($fnn, $this->f)) {
433 for ($i = count($this->f[$fnn][
'args']) - 1; $i >= 0; $i--) {
434 if (is_null($args[$this->f[$fnn][
'args'][$i]] = $stack->pop())) {
435 return $this->
trigger(16,
"internal error");
438 $stack->push($this->
pfx($this->f[$fnn][
'func'], $args));
442 if (is_numeric($token)) {
443 $stack->push($token);
444 } elseif (array_key_exists($token, $this->v)) {
445 $stack->push($this->v[$token]);
446 } elseif (array_key_exists($token, $vars)) {
447 $stack->push($vars[$token]);
449 return $this->
trigger(17,
"undefined variable '$token'", $token);
454 if ($stack->count != 1) {
455 return $this->
trigger(18,
"internal error");
457 return $stack->pop();
468 public function trigger($code, $msg, $info =
null)
470 $this->last_error = $msg;
471 $this->last_error_code = array($code, $info);
472 if (!$this->suppress_errors) {
473 trigger_error($msg, E_USER_WARNING);