173 $this->last_error =
null;
174 $this->last_error_code =
null;
176 if (substr($expr, - 1, 1) ==
';') {
177 $expr = substr($expr, 0, strlen($expr) - 1);
182 if (preg_match(
'/^\s*([a-z]\w*)\s*=\s*(.+)$/', $expr, $matches)) {
183 if (in_array($matches[1], $this->vb)) {
184 return $this->
trigger(1,
"cannot assign to constant '$matches[1]'", $matches[1]);
186 if (($tmp = $this->
pfx($this->
nfx($matches[2]))) ===
false) {
189 $this->v[$matches[1]] = $tmp;
190 return $this->v[$matches[1]];
193 } elseif (preg_match(
'/^\s*([a-z]\w*)\s*\(\s*([a-z]\w*(?:\s*,\s*[a-z]\w*)*)\s*\)\s*=\s*(.+)$/', $expr, $matches)) {
195 if (in_array($matches[1], $this->fb)) {
196 return $this->
trigger(2,
"cannot redefine built-in function '$matches[1]()'", $matches[1]);
198 $args = explode(
",", preg_replace(
"/\s+/",
"", $matches[2]));
199 if (($stack = $this->
nfx($matches[3])) ===
false) {
202 $nbstack = count($stack);
203 for ($i = 0; $i < $nbstack; $i++) {
205 if (preg_match(
'/^[a-z]\w*$/', $token) and !in_array($token, $args)) {
206 if (array_key_exists($token, $this->v)) {
207 $stack[$i] = $this->v[$token];
209 return $this->
trigger(3,
"undefined variable '$token' in function definition", $token);
213 $this->f[$fnn] = array(
'args' => $args,
'func' => $stack);
217 return $this->
pfx($this->
nfx($expr));
256 private function nfx($expr)
261 $expr = trim(strtolower($expr));
263 $ops = array(
'+',
'-',
'*',
'/',
'^',
'_');
264 $ops_r = array(
'+' => 0,
'-' => 0,
'*' => 0,
'/' => 0,
'^' => 1);
265 $ops_p = array(
'+' => 0,
'-' => 0,
'*' => 1,
'/' => 1,
'_' => 1,
'^' => 2);
267 $expecting_op =
false;
271 if (preg_match(
"/[^\w\s+*^\/()\.,-]/", $expr, $matches)) {
272 return $this->
trigger(4,
"illegal character '".$matches[0].
"'", $matches[0]);
276 $op = substr($expr, $index, 1);
279 $ex = preg_match(
'/^([a-z]\w*\(?|\d+(?:\.\d*)?|\.\d+|\()/', substr($expr, $index), $match);
281 if ($op ==
'-' and !$expecting_op) {
284 } elseif ($op ==
'_') {
285 return $this->
trigger(4,
"illegal character '_'",
"_");
287 } elseif ((in_array($op, $ops) or $ex) and $expecting_op) {
293 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])) {
294 $output[] = $stack->pop();
299 $expecting_op =
false;
301 } elseif ($op ==
')' and $expecting_op) {
302 while (($o2 = $stack->pop()) !=
'(') {
304 return $this->
trigger(5,
"unexpected ')'",
")");
309 if (preg_match(
"/^([a-z]\w*)\($/", $stack->last(2), $matches)) {
311 $arg_count = $stack->pop();
312 $output[] = $stack->pop();
313 if (in_array($fnn, $this->fb)) {
314 if ($arg_count > 1) {
315 return $this->
trigger(6,
"wrong number of arguments ($arg_count given, 1 expected)", array($arg_count, 1));
317 } elseif (array_key_exists($fnn, $this->f)) {
318 if ($arg_count != count($this->f[$fnn][
'args'])) {
319 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'])));
322 return $this->
trigger(7,
"internal error");
327 } elseif ($op ==
',' and $expecting_op) {
328 while (($o2 = $stack->pop()) !=
'(') {
330 return $this->
trigger(5,
"unexpected ','",
",");
336 if (!preg_match(
"/^([a-z]\w*)\($/", $stack->last(2), $matches)) {
337 return $this->
trigger(5,
"unexpected ','",
",");
339 $stack->push($stack->pop() + 1);
342 $expecting_op =
false;
344 } elseif ($op ==
'(' and !$expecting_op) {
349 } elseif ($ex and !$expecting_op) {
350 $expecting_op =
true;
352 if (preg_match(
"/^([a-z]\w*)\($/", $val, $matches)) {
353 if (in_array($matches[1], $this->fb) or array_key_exists($matches[1], $this->f)) {
357 $expecting_op =
false;
365 $index += strlen($val);
367 } elseif ($op ==
')') {
368 return $this->
trigger(5,
"unexpected ')'",
")");
369 } elseif (in_array($op, $ops) and !$expecting_op) {
370 return $this->
trigger(8,
"unexpected operator '$op'", $op);
372 return $this->
trigger(9,
"an unexpected error occurred");
374 if ($index == strlen($expr)) {
375 if (in_array($op, $ops)) {
376 return $this->
trigger(10,
"operator '$op' lacks operand", $op);
381 while (substr($expr, $index, 1) ==
' ') {
385 while (!is_null($ope = $stack->pop())) {
387 return $this->
trigger(11,
"expecting ')'",
")");
402 private function pfx($tokens, $vars = array())
406 foreach ($tokens as $token) {
409 if (in_array($token, array(
'+',
'-',
'*',
'/',
'^'))) {
410 if (is_null($op2 = $stack->pop())) {
411 return $this->
trigger(12,
"internal error");
413 if (is_null($op1 = $stack->pop())) {
414 return $this->
trigger(13,
"internal error");
418 $stack->push($op1 + $op2);
421 $stack->push($op1 - $op2);
424 $stack->push($op1 * $op2);
428 return $this->
trigger(14,
"division by zero");
430 $stack->push($op1 / $op2);
433 $stack->push(pow($op1, $op2));
437 } elseif ($token ==
"_") {
438 $stack->push(-1 * $stack->pop());
440 } elseif (preg_match(
"/^([a-z]\w*)\($/", $token, $matches)) {
442 if (in_array($fnn, $this->fb)) {
443 if (is_null($op1 = $stack->pop())) {
444 return $this->
trigger(15,
"internal error");
446 $fnn = preg_replace(
"/^arc/",
"a", $fnn);
451 eval(
'$stack->push('.$fnn.
'($op1));');
452 } elseif (array_key_exists($fnn, $this->f)) {
455 for ($i = count($this->f[$fnn][
'args']) - 1; $i >= 0; $i--) {
456 if (is_null($args[$this->f[$fnn][
'args'][$i]] = $stack->pop())) {
457 return $this->
trigger(16,
"internal error");
460 $stack->push($this->
pfx($this->f[$fnn][
'func'], $args));
464 if (is_numeric($token)) {
465 $stack->push($token);
466 } elseif (array_key_exists($token, $this->v)) {
467 $stack->push($this->v[$token]);
468 } elseif (array_key_exists($token, $vars)) {
469 $stack->push($vars[$token]);
471 return $this->
trigger(17,
"undefined variable '$token'", $token);
476 if ($stack->count != 1) {
477 return $this->
trigger(18,
"internal error");
479 return $stack->pop();
490 public function trigger($code, $msg, $info =
null)
492 $this->last_error = $msg;
493 $this->last_error_code = array($code, $info);
494 if (!$this->suppress_errors) {
495 trigger_error($msg, E_USER_WARNING);