00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 class EvalMath {
00090
00091 var $suppress_errors = false;
00092 var $last_error = null;
00093
00094 var $v = array('e'=>2.71,'pi'=>3.14);
00095 var $f = array();
00096 var $vb = array('e', 'pi');
00097 var $fb = array(
00098 'sin','sinh','arcsin','asin','arcsinh','asinh',
00099 'cos','cosh','arccos','acos','arccosh','acosh',
00100 'tan','tanh','arctan','atan','arctanh','atanh',
00101 'sqrt','abs','ln','log');
00102
00103 function EvalMath() {
00104
00105 $this->v['pi'] = pi();
00106 $this->v['e'] = exp(1);
00107 }
00108
00109 function e($expr) {
00110 return $this->evaluate($expr);
00111 }
00112
00113 function evaluate($expr) {
00114 $this->last_error = null;
00115 $expr = trim($expr);
00116 if (substr($expr, -1, 1) == ';') $expr = substr($expr, 0, strlen($expr)-1);
00117
00118
00119 if (preg_match('/^\s*([a-z]\w*)\s*=\s*(.+)$/', $expr, $matches)) {
00120 if (in_array($matches[1], $this->vb)) {
00121 return $this->trigger("cannot assign to constant '$matches[1]'");
00122 }
00123 if (($tmp = $this->pfx($this->nfx($matches[2]))) === false) return false;
00124 $this->v[$matches[1]] = $tmp;
00125 return $this->v[$matches[1]];
00126
00127
00128 } elseif (preg_match('/^\s*([a-z]\w*)\s*\(\s*([a-z]\w*(?:\s*,\s*[a-z]\w*)*)\s*\)\s*=\s*(.+)$/', $expr, $matches)) {
00129 $fnn = $matches[1];
00130 if (in_array($matches[1], $this->fb)) {
00131 return $this->trigger("cannot redefine built-in function '$matches[1]()'");
00132 }
00133 $args = explode(",", preg_replace("/\s+/", "", $matches[2]));
00134 if (($stack = $this->nfx($matches[3])) === false) return false;
00135 for ($i = 0; $i<count($stack); $i++) {
00136 $token = $stack[$i];
00137 if (preg_match('/^[a-z]\w*$/', $token) and !in_array($token, $args)) {
00138 if (array_key_exists($token, $this->v)) {
00139 $stack[$i] = $this->v[$token];
00140 } else {
00141 return $this->trigger("undefined variable '$token' in function definition");
00142 }
00143 }
00144 }
00145 $this->f[$fnn] = array('args'=>$args, 'func'=>$stack);
00146 return true;
00147
00148 } else {
00149 return $this->pfx($this->nfx($expr));
00150 }
00151 }
00152
00153 function vars() {
00154 $output = $this->v;
00155 unset($output['pi']);
00156 unset($output['e']);
00157 return $output;
00158 }
00159
00160 function funcs() {
00161 $output = array();
00162 foreach ($this->f as $fnn=>$dat)
00163 $output[] = $fnn . '(' . implode(',', $dat['args']) . ')';
00164 return $output;
00165 }
00166
00167
00168
00169
00170 function nfx($expr) {
00171
00172 $index = 0;
00173 $stack = new EvalMathStack;
00174 $output = array();
00175 $expr = trim(strtolower($expr));
00176
00177 $ops = array('+', '-', '*', '/', '^', '_');
00178 $ops_r = array('+'=>0,'-'=>0,'*'=>0,'/'=>0,'^'=>1);
00179 $ops_p = array('+'=>0,'-'=>0,'*'=>1,'/'=>1,'_'=>1,'^'=>2);
00180
00181 $expecting_op = false;
00182
00183
00184 if (preg_match("/[^\w\s+*^\/()\.,-]/", $expr, $matches)) {
00185 return $this->trigger("illegal character '{$matches[0]}'");
00186 }
00187
00188 while(1) {
00189 $op = substr($expr, $index, 1);
00190
00191 $ex = preg_match('/^([01]+[bB]|[\da-fA-F]+[hH]|[a-z]\w*\(?|\d+(?:\.\d*)?|\.\d+|\()/', substr($expr, $index), $match);
00192
00193 if ($op == '-' and !$expecting_op) {
00194 $stack->push('_');
00195 $index++;
00196 } elseif ($op == '_') {
00197 return $this->trigger("illegal character '_'");
00198
00199 } elseif ((in_array($op, $ops) or $ex) and $expecting_op) {
00200 if ($ex) {
00201 $op = '*'; $index--;
00202 }
00203
00204 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])) {
00205 $output[] = $stack->pop();
00206 }
00207
00208 $stack->push($op);
00209 $index++;
00210 $expecting_op = false;
00211
00212 } elseif ($op == ')' and $expecting_op) {
00213 while (($o2 = $stack->pop()) != '(') {
00214 if (is_null($o2)) return $this->trigger("unexpected ')'");
00215 else $output[] = $o2;
00216 }
00217 if (preg_match("/^([a-z]\w*)\($/", $stack->last(2), $matches)) {
00218 $fnn = $matches[1];
00219 $arg_count = $stack->pop();
00220 $output[] = $stack->pop();
00221 if (in_array($fnn, $this->fb)) {
00222 if($arg_count > 1)
00223 return $this->trigger("too many arguments ($arg_count given, 1 expected)");
00224 } elseif (array_key_exists($fnn, $this->f)) {
00225 if ($arg_count != count($this->f[$fnn]['args']))
00226 return $this->trigger("wrong number of arguments ($arg_count given, " . count($this->f[$fnn]['args']) . " expected)");
00227 } else {
00228 return $this->trigger("internal error");
00229 }
00230 }
00231 $index++;
00232
00233 } elseif ($op == ',' and $expecting_op) {
00234 while (($o2 = $stack->pop()) != '(') {
00235 if (is_null($o2)) return $this->trigger("unexpected ','");
00236 else $output[] = $o2;
00237 }
00238
00239 if (!preg_match("/^([a-z]\w*)\($/", $stack->last(2), $matches))
00240 return $this->trigger("unexpected ','");
00241 $stack->push($stack->pop()+1);
00242 $stack->push('(');
00243 $index++;
00244 $expecting_op = false;
00245
00246 } elseif ($op == '(' and !$expecting_op) {
00247 $stack->push('(');
00248 $index++;
00249 $allow_neg = true;
00250
00251 } elseif ($ex and !$expecting_op) {
00252 $expecting_op = true;
00253 $val = $match[1];
00254 if (preg_match("/^([a-z]\w*)\($/", $val, $matches)) {
00255 if (in_array($matches[1], $this->fb) or array_key_exists($matches[1], $this->f)) {
00256 $stack->push($val);
00257 $stack->push(1);
00258 $stack->push('(');
00259 $expecting_op = false;
00260 } else {
00261 $val = $matches[1];
00262 $output[] = $val;
00263 }
00264 } else {
00265 $output[] = $val;
00266 }
00267 $index += strlen($val);
00268
00269 } elseif ($op == ')') {
00270 return $this->trigger("unexpected ')'");
00271 } elseif (in_array($op, $ops) and !$expecting_op) {
00272 return $this->trigger("unexpected operator '$op'");
00273 } else {
00274 return $this->trigger("an unexpected error occured");
00275 }
00276 if ($index == strlen($expr)) {
00277 if (in_array($op, $ops)) {
00278 return $this->trigger("operator '$op' lacks operand");
00279 } else {
00280 break;
00281 }
00282 }
00283 while (substr($expr, $index, 1) == ' ') {
00284 $index++;
00285 }
00286
00287 }
00288 while (!is_null($op = $stack->pop())) {
00289 if ($op == '(') return $this->trigger("expecting ')'");
00290 $output[] = $op;
00291 }
00292 return $output;
00293 }
00294
00295
00296 function pfx($tokens, $vars = array()) {
00297
00298 if ($tokens == false) return false;
00299
00300 $stack = new EvalMathStack;
00301
00302 foreach ($tokens as $token) {
00303
00304 if (in_array($token, array('+', '-', '*', '/', '^'))) {
00305 if (is_null($op2 = $stack->pop())) return $this->trigger("internal error");
00306 if (is_null($op1 = $stack->pop())) return $this->trigger("internal error");
00307 switch ($token) {
00308 case '+':
00309 $stack->push($op1+$op2); break;
00310 case '-':
00311 $stack->push($op1-$op2); break;
00312 case '*':
00313 $stack->push($op1*$op2); break;
00314 case '/':
00315 if ($op2 == 0) return $this->trigger("division by zero");
00316 $stack->push($op1/$op2); break;
00317 case '^':
00318 $stack->push(pow($op1, $op2)); break;
00319 }
00320
00321 } elseif ($token == "_") {
00322 $stack->push(-1*$stack->pop());
00323
00324 } elseif (preg_match("/^([a-z]\w*)\($/", $token, $matches)) {
00325 $fnn = $matches[1];
00326 if (in_array($fnn, $this->fb)) {
00327 if (is_null($op1 = $stack->pop())) return $this->trigger("internal error");
00328 $fnn = preg_replace("/^arc/", "a", $fnn);
00329 if ($fnn == 'ln') $fnn = 'log';
00330 eval('$stack->push(' . $fnn . '($op1));');
00331 } elseif (array_key_exists($fnn, $this->f)) {
00332
00333 $args = array();
00334 for ($i = count($this->f[$fnn]['args'])-1; $i >= 0; $i--) {
00335 if (is_null($args[$this->f[$fnn]['args'][$i]] = $stack->pop())) return $this->trigger("internal error");
00336 }
00337 $stack->push($this->pfx($this->f[$fnn]['func'], $args));
00338 }
00339
00340 } else {
00341 if (is_numeric($token)) {
00342 $stack->push($token);
00343 } elseif (($hex=$this->from_hexbin($token))!==FALSE) {
00344 $stack->push($hex);
00345 } elseif (array_key_exists($token, $this->v)) {
00346 $stack->push($this->v[$token]);
00347 } elseif (array_key_exists($token, $vars)) {
00348 $stack->push($vars[$token]);
00349 } else {
00350 return $this->trigger("undefined variable '$token'");
00351 }
00352 }
00353 }
00354
00355 if ($stack->count != 1) return $this->trigger("internal error");
00356 return $stack->pop();
00357 }
00358
00359
00360 function trigger($msg) {
00361 $this->last_error = $msg;
00362 if (!$this->suppress_errors) trigger_error($msg, E_USER_WARNING);
00363 return false;
00364 }
00365
00366
00367
00368 function from_hexbin($token) {
00369 if (strtoupper(substr($token, -1, 1))=='H') return hexdec($token);
00370 if (strtoupper(substr($token, -1, 1))=='B') return bindec($token);
00371 return FALSE;
00372 }
00373 }
00374
00375
00376 class EvalMathStack {
00377
00378 var $stack = array();
00379 var $count = 0;
00380
00381 function push($val) {
00382 $this->stack[$this->count] = $val;
00383 $this->count++;
00384 }
00385
00386 function pop() {
00387 if ($this->count > 0) {
00388 $this->count--;
00389 return $this->stack[$this->count];
00390 }
00391 return null;
00392 }
00393
00394 function last($n=1) {
00395 return $this->stack[$this->count-$n];
00396 }
00397 }
00398
00399 ?>