This commit is contained in:
David Bensel
2020-12-14 22:40:47 +03:00
parent 8320546641
commit bc9f7c83f4
3660 changed files with 183847 additions and 274871 deletions

View File

@@ -149,8 +149,8 @@ class EvalMath
$expr = trim($expr);
if (substr($expr, - 1, 1) == ';')
$expr = substr($expr, 0, strlen($expr) - 1); // strip semicolons at the end
// ===============
// is it a variable assignment?
// ===============
// is it a variable assignment?
$matches = array();
if (preg_match('/^\s*([a-z]\w*)\s*=\s*(.+)$/', $expr, $matches)) {
if (in_array($matches[1], $this->vb)) { // make sure we're not assigning to a constant
@@ -160,8 +160,8 @@ class EvalMath
return false; // get the result and make sure it's good
$this->v[$matches[1]] = $tmp; // if so, stick it in the variable array
return $this->v[$matches[1]]; // and return the resulting value
// ===============
// is it a function assignment?
// ===============
// is it a function assignment?
} 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]; // get the function name
if (in_array($matches[1], $this->fb)) { // make sure it isn't built in
@@ -235,7 +235,7 @@ class EvalMath
$ops_p = array('+' => 0, '-' => 0, '*' => 1, '/' => 1, '_' => 1, '^' => 2); // operator precedence
$expecting_op = false; // we use this in syntax-checking the expression
// and determining when a - is a negation
// and determining when a - is a negation
$matches = array();
if (preg_match("/[^\w\s+*^\/()\.,-]/", $expr, $matches)) { // make sure the characters are all good
@@ -244,7 +244,7 @@ class EvalMath
while (1) { // 1 Infinite Loop ;)
$op = substr($expr, $index, 1); // get the first character at the current index
// find out if we're currently at the beginning of a number/variable/function/parenthesis/operand
// find out if we're currently at the beginning of a number/variable/function/parenthesis/operand
$match = array();
$ex = preg_match('/^([a-z]\w*\(?|\d+(?:\.\d*)?|\.\d+|\()/', substr($expr, $index), $match);
// ===============
@@ -253,7 +253,7 @@ class EvalMath
$index++;
} elseif ($op == '_') { // we have to explicitly deny this, because it's legal on the stack
return $this->trigger(4, "illegal character '_'", "_"); // but not in the input expression
// ===============
// ===============
} elseif ((in_array($op, $ops) or $ex) and $expecting_op) { // are we putting an operator on the stack?
if ($ex) { // are we expecting an operator but have a number/variable/function/opening parethesis?
$op = '*';
@@ -372,7 +372,7 @@ class EvalMath
$stack = new EvalMathStack();
foreach ($tokens as $token) { // nice and easy
// if the token is a binary operator, pop two values off the stack, do the operation, and push the result back on
// if the token is a binary operator, pop two values off the stack, do the operation, and push the result back on
$matches = array();
if (in_array($token, array('+', '-', '*', '/', '^'))) {
if (is_null($op2 = $stack->pop()))
@@ -412,7 +412,7 @@ class EvalMath
$fnn = 'log';
eval('$stack->push('.$fnn.'($op1));'); // perfectly safe eval()
} elseif (array_key_exists($fnn, $this->f)) { // user function
// get args
// get args
$args = array();
for ($i = count($this->f[$fnn]['args']) - 1; $i >= 0; $i--) {
if (is_null($args[$this->f[$fnn]['args'][$i]] = $stack->pop()))