Commit 9dbbc3b0bc9e8a1340c52fcda399bbf4e2be2c84

Authored by Benjamin Renard
1 parent 950d4b2d

Fix expression parsing when some brackets are added for operators priority

Showing 1 changed file with 13 additions and 11 deletions   Show diff stats
src/InputOutput/IHMImpl/Tools/IHMExpressionParserClass.php
... ... @@ -66,20 +66,22 @@ class IHMExpressionParserClass
66 66 {
67 67 //tests
68 68 $tests = array(
69   - "dst>3" => array ("expression" => "(greater_than(\$dst,3))", "params" => array(array("paramid" => "dst"))),
70   - "@const_1<dst" => array("expression" => "(lower_than(1.0E-10,\$dst))", "params" => array(array("paramid" =>"dst"))),
71   - "dst^#alias_2" => array("expression" => "(pow(\$dst,\$imf[1]))", "params" => array(array("paramid" => "dst"),array("paramid" => "imf"))),
72   - "dst^2+@const_2" => array("expression" => "(pow(\$dst,2))+-2.8", "params" => array(array("paramid" => "dst"))),
73   - "atan(imf(2)/imf(1))+speed/100.0" => array("expression" => "(atan((\$imf[2]/\$imf[1])))+(\$speed/100.0)", "params" => array(array("paramid" => "imf"), array("paramid" => "speed"))),
  69 + "dst>3" => array ("expression" => "greater_than(\$dst,3)", "params" => array(array("paramid" => "dst"))),
  70 + "@const_1<dst" => array("expression" => "lower_than(1.0E-10,\$dst)", "params" => array(array("paramid" =>"dst"))),
  71 + "dst^#alias_2" => array("expression" => "pow(\$dst,\$imf[1])", "params" => array(array("paramid" => "dst"),array("paramid" => "imf"))),
  72 + "dst^2+@const_2" => array("expression" => "pow(\$dst,2)+-2.8", "params" => array(array("paramid" => "dst"))),
  73 + "atan(imf(2)/imf(1))+speed/100.0" => array("expression" => "(atan(\$imf[2]/\$imf[1]))+\$speed/100.0", "params" => array(array("paramid" => "imf"), array("paramid" => "speed"))),
74 74 "shiftT_(clust1_hia_pad,60)" => array("expression" => "(#timeShift(\$clust1_hia_pad;60))", "params" => array(array("paramid" => "clust1_hia_pad"))),
75 75 "smooth_(density,1200)" => array("expression" => "(#boxcar(\$density;1200))", "params" => array(array("paramid" => "density"))),
76 76 "shiftT_(density,-600)" => array("expression" => "(#timeShift(\$density;-600))", "params" => array(array("paramid" => "density"))),
77 77 "deriv(density)" => array("expression" => "(#deriv(\$density))", "params" => array(array("paramid" => "density"))),
78 78 "abs(dst)" =>array("expression" => "(abs(\$dst))", "params" => array(array("paramid" => "dst"))),
79   - "density*speed+(density^2-1.0)/speed" => array("expression" => "(\$density*\$speed)+((pow(\$density,2))-1.0)/\$speed", "params" => array(array("paramid" => "density"),array("paramid" => "speed"))),
  79 + "density*speed+(density^2-1.0)/speed" => array("expression" => "\$density*\$speed+(pow(\$density,2)-1.0)/\$speed", "params" => array(array("paramid" => "density"),array("paramid" => "speed"))),
80 80 "(-7<dst)&(dst<-3)" => array("expression" => "And((lower_than(-7,\$dst)),(lower_than(\$dst,-3)))", "params" => array(array("paramid" => "dst"))),
81 81 "cass_caps_elssec4(range[0,10])" => array("expression" => "\$sum_into_table_range_cass_caps_elssec4_0_0_10", "params" => array(array("paramid" => "sum_into_table_range", "fullparamid" => "sum_into_table_range_cass_caps_elssec4_0_0_10", "template_args" => array("paramid"=>"cass_caps_elssec4","relateddim"=>0,"min"=>"0","max"=>"10")))),
82 82 "abs(dst)>0" => array("expression" => "greater_than((abs(\$dst)),0)", "params" => array(array("paramid" => "dst"))),
  83 + "2/(1+dst)" => array("expression" => "2/(1+\$dst)", "params" => array(array("paramid" => "dst"))),
  84 + "2/abs(1+dst)" => array("expression" => "2/(abs(1+\$dst))", "params" => array(array("paramid" => "dst"))),
83 85 );
84 86  
85 87 //init constants, aliases and functions for test
... ... @@ -100,6 +102,7 @@ class IHMExpressionParserClass
100 102 "smooth_" => array("kernel_name" => "#boxcar", "nb_args" => 1),
101 103 "deriv" => array("kernel_name" => "#deriv", "nb_args" => 0),
102 104 "abs" => array("kernel_name" => "abs", "nb_args" => 0),
  105 + "sqrt" => array("kernel_name" => "sqrt", "nb_args" => 0),
103 106 );
104 107  
105 108 //add operator to change in function
... ... @@ -449,7 +452,7 @@ class IHMExpressionParserClass
449 452 $els[] = $elements[$j];
450 453  
451 454 $elements = $els;
452   -
  455 +
453 456 //regroup negative number
454 457 $els = array();
455 458 $i = 0;
... ... @@ -474,14 +477,14 @@ class IHMExpressionParserClass
474 477 $elements = $els;
475 478  
476 479 //add brackets for operators priority
477   -
  480 +
478 481 $els = array();
479 482 $i = 0;
480 483 for ($i; $i < count($elements)-2; ++$i)
481 484 {
482 485 if ($elements[$i+1] == "*" || $elements[$i+1] == "/" || $elements[$i+1] == "^" || $elements[$i+1] == "<" || $elements[$i+1] == ">")
483 486 {
484   - if ($elements[$i] != ")")
  487 + if ($elements[$i] != ")" && ($this->isDecimal($elements[$i+1]) || $this->isInteger($elements[$i+1])))
485 488 {
486 489 $els[] = "(";
487 490 $els[] = $elements[$i];
... ... @@ -501,7 +504,7 @@ class IHMExpressionParserClass
501 504  
502 505 //split parameters and components
503 506 $elements = $els;
504   -
  507 +
505 508 $els = array();
506 509 $i = 0;
507 510 for ($i; $i < count($elements); ++$i)
... ... @@ -762,7 +765,6 @@ class IHMExpressionParserClass
762 765  
763 766 private function fixGroupFunction($tree,&$result) {
764 767 $result = array();
765   -
766 768 for ($i = 0; $i < count($tree); ++$i)
767 769 {
768 770 if (is_array($tree[$i])) {
... ...