diff --git a/Formula1.php b/Formula1.php
index ca4d4a8..5a15474 100644
--- a/Formula1.php
+++ b/Formula1.php
@@ -2,6 +2,7 @@
class Formula1 {
public $tree=[];
+ public $parse_error='';
private $dual_hp_ops = ['/','*','%','^'];
private $dual_lp_ops = ['+','-'];
private $single_ops = ['+','-'];
@@ -12,11 +13,10 @@ class Formula1 {
$this->precision_mode = $precision_mode;
$f = $this->replace_parenth($formula, 0);
if(strrpos($f, ')')>-1) {
- echo('PARSING ERROR: Too many closing parenthesis !');
+ $this->parse_error = 'PARSING ERROR: Too many closing parenthesis !';
return(false);
}
$this->tree = $this->parse($f);
- return(true);
}
private function do_single_op($op, $x){
@@ -59,7 +59,7 @@ class Formula1 {
if($c=='(') {
$closing_idx = $this->find_closingp($f, $i);
if($closing_idx===false) {
- echo("PARSING ERROR: missing closing parenthesis !");
+ $this->parse_error = "PARSING ERROR: missing closing parenthesis !";
return('');
}
$expr = substr($f, $i+1, $closing_idx-$i-1);
@@ -76,28 +76,74 @@ class Formula1 {
private function parse($f){
+//echo('
Parsing:'.$f.'
');
$re = '/^(.+?)([\\'.implode('\\',$this->dual_lp_ops).'])(.+)$/'; // dual low priority op
if(preg_match($re, $f, $matches, PREG_OFFSET_CAPTURE, 0)){
- return([$this->parse($matches[1][0]), $matches[2][0], $this->parse($matches[3][0])]);
+ $recur = $this->parse($matches[3][0]);
+ if($recur!==false) return([$this->parse($matches[1][0]), $matches[2][0], $recur]);
+ else return(false);
} else {
$re = '/^(.+?)([\\'.implode('\\',$this->dual_hp_ops).'])(.+)$/'; // dual high priority op
if(preg_match($re, $f, $matches, PREG_OFFSET_CAPTURE, 0)){
- return([$this->parse($matches[1][0]), $matches[2][0], $this->parse($matches[3][0])]);
+ $recur = $this->parse($matches[3][0]);
+ if($recur!==false) return([$this->parse($matches[1][0]), $matches[2][0], $recur]);
+ else return(false);
} else {
$re = '/^([\\'.implode('\\',$this->single_ops).'])(.+)$/'; // single op
if(preg_match($re, $f, $matches, PREG_OFFSET_CAPTURE, 0)){
- return([null, $matches[1][0], $this->parse($matches[2][0])]);
+ $recur = $this->parse($matches[2][0]);
+ if($recur!==false) return([null, $matches[1][0], $recur]);
+ else return(false);
} else {
- $re = '/^([A-Za-z][A-Za-z0-9_]*)$/';
- if(preg_match($re, $f, $matches, PREG_OFFSET_CAPTURE, 0)){
- if(array_key_exists($matches[0][0], $this->params)) return($this->parse($this->params[$matches[0][0]]));
- else echo('PARSING ERROR: Unknown expression "'.$matches[0][0].'" ');
- } else {
- $re = '/^\d*(\.\d+)?$/'; // unsigned float or int
- if(preg_match($re, $f, $matches, PREG_OFFSET_CAPTURE, 0)){
- return($matches[0][0]);
+ $re1 = '/^([A-Za-z][A-Za-z0-9_]*)\[\"([A-Za-z0-9_]+)\"\]$/'; // arrays with alphanum keys
+ $re2 = "/^([A-Za-z][A-Za-z0-9_]*)\[\'([A-Za-z0-9_]+)\'\]$/"; // arrays with alphanum keys
+ if(preg_match($re1, $f, $matches1, PREG_OFFSET_CAPTURE, 0)||preg_match($re2, $f, $matches2, PREG_OFFSET_CAPTURE, 0)){
+ $array_name = (count($matches1)>0) ? $matches1[1][0] : $matches2[1][0];
+ $array_key = (count($matches1)>0) ? $matches1[2][0] : $matches2[2][0];
+ if(array_key_exists($array_name, $this->params) && is_array($this->params[$array_name])) {
+ if(array_key_exists($array_key, $this->params[$array_name])) {
+ return($this->parse($this->params[$array_name][$array_key]));
+ } else {
+ $this->parse_error = 'PARSING ERROR: Unknown key "'.$array_key.'" for array "'.$array_name.'" ';
+ return(false);
+ }
} else {
- echo('PARSING ERROR at '.$f.'
');
+ $this->parse_error = 'PARSING ERROR: Unknown array "'.$array_name.'" ';
+ return(false);
+ }
+ } else {
+ $re = '/^([A-Za-z][A-Za-z0-9_]*)\[(\d+)\]$/'; // arrays with alphanum keys
+ if(preg_match($re, $f, $matches, PREG_OFFSET_CAPTURE, 0)){
+ $array_name = $matches[1][0];
+ $array_index = $matches[2][0];
+ if(array_key_exists($array_name, $this->params) && is_array($this->params[$array_name])) {
+ if(isset($this->params[$array_name][$array_index])) {
+ return($this->parse($this->params[$array_name][$array_index]));
+ } else {
+ $this->parse_error = 'PARSING ERROR: Unknown index "'.$array_index.'" for array "'.$array_name.'" ';
+ return(false);
+ }
+ } else {
+ $this->parse_error = 'PARSING ERROR: Unknown array "'.$array_name.'" ';
+ return(false);
+ }
+ } else {
+ $re = '/^([A-Za-z][A-Za-z0-9_]*)$/'; // simple variables
+ if(preg_match($re, $f, $matches, PREG_OFFSET_CAPTURE, 0)){
+ if(array_key_exists($matches[0][0], $this->params)) return($this->parse($this->params[$matches[0][0]]));
+ else {
+ $this->parse_error = 'PARSING ERROR: Unknown expression "'.$matches[0][0].'" ';
+ return(false);
+ }
+ } else {
+ $re = '/^\d*(\.\d+)?$/'; // unsigned float or int
+ if(preg_match($re, $f, $matches, PREG_OFFSET_CAPTURE, 0)){
+ return($matches[0][0]);
+ } else {
+ $this->parse_error = 'PARSING ERROR at '.$f.'
';
+ return(false);
+ }
+ }
}
}
}