100 public $suppress_errors =
false;
102 public $last_error =
null;
104 public $last_error_code =
null;
106 public $v = array(
'e' => 2.71,
'pi' => 3.14159);
112 public $vb = array(
'e',
'pi');
116 'sin',
'sinh',
'arcsin',
'asin',
'arcsinh',
'asinh',
'cos',
'cosh',
'arccos',
'acos',
'arccosh',
'acosh',
'tan',
'tanh',
'arctan',
'atan',
'arctanh',
'atanh',
'sqrt',
'abs',
'ln',
'log',
'intval');
124 $this->v[
'pi'] = pi();
125 $this->v[
'e'] = exp(1);
134 public function e($expr)
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));
207 unset($output[
'pi']);
220 foreach ($this->f as $fnn => $dat) {
221 $output[] = $fnn.
'('.implode(
',', $dat[
'args']).
')';
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 occured");
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 ')'",
")");
379 private function pfx($tokens, $vars = array())
383 foreach ($tokens as $token) {
386 if (in_array($token, array(
'+',
'-',
'*',
'/',
'^'))) {
387 if (is_null($op2 = $stack->pop())) {
388 return $this->
trigger(12,
"internal error");
390 if (is_null($op1 = $stack->pop())) {
391 return $this->
trigger(13,
"internal error");
395 $stack->push($op1 + $op2);
398 $stack->push($op1 - $op2);
401 $stack->push($op1 * $op2);
405 return $this->
trigger(14,
"division by zero");
407 $stack->push($op1 / $op2);
410 $stack->push(pow($op1, $op2));
414 } elseif ($token ==
"_") {
415 $stack->push(-1 * $stack->pop());
417 } elseif (preg_match(
"/^([a-z]\w*)\($/", $token, $matches)) {
419 if (in_array($fnn, $this->fb)) {
420 if (is_null($op1 = $stack->pop())) {
421 return $this->
trigger(15,
"internal error");
423 $fnn = preg_replace(
"/^arc/",
"a", $fnn);
427 eval(
'$stack->push('.$fnn.
'($op1));');
428 } elseif (array_key_exists($fnn, $this->f)) {
431 for ($i = count($this->f[$fnn][
'args']) - 1; $i >= 0; $i--) {
432 if (is_null($args[$this->f[$fnn][
'args'][$i]] = $stack->pop())) {
433 return $this->
trigger(16,
"internal error");
436 $stack->push($this->
pfx($this->f[$fnn][
'func'], $args));
440 if (is_numeric($token)) {
441 $stack->push($token);
442 } elseif (array_key_exists($token, $this->v)) {
443 $stack->push($this->v[$token]);
444 } elseif (array_key_exists($token, $vars)) {
445 $stack->push($vars[$token]);
447 return $this->
trigger(17,
"undefined variable '$token'", $token);
452 if ($stack->count != 1) {
453 return $this->
trigger(18,
"internal error");
455 return $stack->pop();
466 public function trigger($code, $msg, $info =
null)
468 $this->last_error = $msg;
469 $this->last_error_code = array($code, $info);
470 if (!$this->suppress_errors) {
471 trigger_error($msg, E_USER_WARNING);
483 public $stack = array();
495 $this->stack[$this->count] = $val;
506 if ($this->count > 0) {
508 return $this->stack[$this->count];
521 if (isset($this->stack[$this->count - $n])) {
522 return $this->stack[$this->count - $n];