Commit 224a3ea97ba98df293145afa0b578d60e895a798

Authored by Benjamin Renard
1 parent 8d780912

Fix expression parser when a function have more than one argument

Showing 1 changed file with 24 additions and 13 deletions   Show diff stats
src/InputOutput/IHMImpl/Tools/IHMExpressionParserClass.php
... ... @@ -21,6 +21,7 @@ class IHMExpressionParserClass
21 21 private static $functionsNode = 'function';
22 22 private static $functionsNameAtt = 'name';
23 23 private static $functionsArgsAtt = 'args';
  24 + private static $functionsParamsAtt = 'params';
24 25 private static $functionsNewKernelNode = 'new_kernel';
25 26  
26 27 //operator
... ... @@ -82,6 +83,7 @@ class IHMExpressionParserClass
82 83 "abs(dst)>0" => array("expression" => "greater_than((abs(\$dst)),0)", "params" => array(array("paramid" => "dst"))),
83 84 "2/(1+dst)" => array("expression" => "2/(1+\$dst)", "params" => array(array("paramid" => "dst"))),
84 85 "2/abs(1+dst)" => array("expression" => "2/(abs(1+\$dst))", "params" => array(array("paramid" => "dst"))),
  86 + "cross(vect_1,vect_2)" => array("expression" => "(cross(\$vect_1,\$vect_2))", "params" => array(array("paramid" => "vect_1"), array("paramid" => "vect_2"))),
85 87 );
86 88  
87 89 //init constants, aliases and functions for test
... ... @@ -96,13 +98,14 @@ class IHMExpressionParserClass
96 98 );
97 99  
98 100 $this->functionsArray = array(
99   - "func_old" => array("kernel_name" => "func_new", "nb_args" => 0),
100   - "atan" => array("kernel_name" => "atan", "nb_args" => 0),
101   - "shiftT_" => array("kernel_name" => "#timeShift", "nb_args" => 1),
102   - "smooth_" => array("kernel_name" => "#boxcar", "nb_args" => 1),
103   - "deriv" => array("kernel_name" => "#deriv", "nb_args" => 0),
104   - "abs" => array("kernel_name" => "abs", "nb_args" => 0),
105   - "sqrt" => array("kernel_name" => "sqrt", "nb_args" => 0),
  101 + "func_old" => array("kernel_name" => "func_new", "nb_args" => 0, "nb_params" => 1),
  102 + "atan" => array("kernel_name" => "atan", "nb_args" => 0, "nb_params" => 1),
  103 + "shiftT_" => array("kernel_name" => "#timeShift", "nb_args" => 1, "nb_params" => 1),
  104 + "smooth_" => array("kernel_name" => "#boxcar", "nb_args" => 1, "nb_params" => 1),
  105 + "deriv" => array("kernel_name" => "#deriv", "nb_args" => 0, "nb_params" => 1),
  106 + "abs" => array("kernel_name" => "abs", "nb_args" => 0, "nb_params" => 1),
  107 + "sqrt" => array("kernel_name" => "sqrt", "nb_args" => 0, "nb_params" => 1),
  108 + "cross" => array("kernel_name" => "cross", "nb_args" => 0, "nb_params" => 2),
106 109 );
107 110  
108 111 //add operator to change in function
... ... @@ -304,6 +307,7 @@ class IHMExpressionParserClass
304 307 {
305 308 $tempArr = explode('(', $functions_->item($i)->getAttribute(self::$functionsNameAtt));
306 309 $nbArgs = $functions_->item($i)->getAttribute(self::$functionsArgsAtt);
  310 + $nbParams = $functions_->item($i)->getAttribute(self::$functionsParamsAtt);
307 311 $kernelFunctions_ = $functions_->item($i)->getElementsByTagName(self::$functionsNewKernelNode);
308 312 if ($kernelFunctions_->length == 0)
309 313 $kernelFunction = "";
... ... @@ -312,6 +316,7 @@ class IHMExpressionParserClass
312 316 $this->functionsArray[$tempArr[0]] = array(
313 317 "kernel_name" => $kernelFunction,
314 318 "nb_args" => intval($nbArgs),
  319 + "nb_params" => intval($nbParams),
315 320 "isOperator" => false
316 321 );
317 322 }
... ... @@ -790,7 +795,7 @@ class IHMExpressionParserClass
790 795 /*
791 796 * @brief process used to replace "," by ";" for function
792 797 */
793   - private function fixComaFunction($tree,$isFunctionGroup,&$result)
  798 + private function fixComaFunction($tree,$crt_function,&$result,&$nbParams)
794 799 {
795 800 for ($i = 0; $i < count($tree); ++$i)
796 801 {
... ... @@ -802,15 +807,20 @@ class IHMExpressionParserClass
802 807 $groupFunc = $this->isFunction($tree[$i-1]);
803 808 }
804 809 $res = array();
805   - $this->fixComaFunction($tree[$i],$groupFunc,$res);
  810 + $nbParams = 0;
  811 + $this->fixComaFunction($tree[$i],$groupFunc ? $tree[$i-1] : "",$res,$nbParams);
806 812 $result[] = $res;
807 813 }
808 814 else
809 815 {
810   - if ($isFunctionGroup && ($tree[$i] == ','))
811   - $result[] = ';';
812   - else
  816 + if (!empty($crt_function) && ($tree[$i] == ',')) {
  817 + $nb_func_params = $this->functionsArray[$crt_function]["nb_params"];
  818 + $result[] = ($nb_func_params <= $nbParams) ? ';' : ',';
  819 + }
  820 + else {
  821 + ++$nbParams;
813 822 $result[] = $tree[$i];
  823 + }
814 824 }
815 825 }
816 826 }
... ... @@ -920,7 +930,8 @@ class IHMExpressionParserClass
920 930 $this->fixGroupFunction($res_1,$res_2);
921 931  
922 932 $res_3 = array();
923   - $this->fixComaFunction($res_2,false,$res_3);
  933 + $nbParams = 0;
  934 + $this->fixComaFunction($res_2,"",$res_3,$nbParams);
924 935  
925 936 $res_4 = array();
926 937 $this->fixOperatorToFunction($res_3,$res_4);
... ...