66 */
77class TreeCompiler
88{
9- /** @var string Current level of indentation */
109 private $ indentation ;
11-
12- /** @var string Compiled source code */
1310 private $ source ;
11+ private $ vars ;
1412
1513 /**
1614 * @param array $ast AST to compile.
@@ -21,6 +19,7 @@ class TreeCompiler
2119 */
2220 public function visit (array $ ast , $ fnName , $ expr )
2321 {
22+ $ this ->vars = [];
2423 $ this ->source = $ this ->indentation = '' ;
2524 $ this ->write ("<?php \n" );
2625 $ this ->write ("// {$ expr }" );
@@ -41,6 +40,15 @@ private function dispatch(array $node)
4140 return $ this ->{"visit_ {$ node ['type ' ]}" }($ node );
4241 }
4342
43+ private function makeVar ($ type )
44+ {
45+ if (!isset ($ this ->vars [$ type ])) {
46+ $ this ->vars [$ type ] = 0 ;
47+ }
48+
49+ return $ type . ++$ this ->vars [$ type ];
50+ }
51+
4452 /**
4553 * Writes the given line of source code
4654 *
@@ -77,15 +85,14 @@ private function indent()
7785
7886 private function visit_or (array $ node )
7987 {
80- $ id = uniqid ( );
88+ $ a = $ this -> makeVar ( ' beforeOr ' );
8189
8290 return $ this
83- ->write (' $beforeOr_ ' . $ id . ' = $value;' )
91+ ->write ("\$ $ a = \ $value; " )
8492 ->dispatch ($ node ['children ' ][0 ])
85- ->write ('' )
8693 ->write ('if (!$value && $value !== "0" && $value !== 0) { ' )
8794 ->indent ()
88- ->write (' $value = $beforeOr_ ' . $ id . ' ; ' )
95+ ->write ("\ $value = \$ $ a ; " )
8996 ->dispatch ($ node ['children ' ][1 ])
9097 ->outdent ()
9198 ->write ('} ' );
@@ -143,12 +150,13 @@ private function visit_index(array $node)
143150 }
144151
145152 // Account for negative indices
146- $ tmpCount = uniqid ('count_ ' );
153+ $ a = $ this ->makeVar ('count ' );
154+
147155 $ this
148156 ->write ('if (is_array($value) || ($value instanceof \ArrayAccess && $value instanceof \Countable)) { ' )
149157 ->indent ()
150- ->write ("\${$ tmpCount } = count( \$value) + {$ node ['index ' ]}; " )
151- ->write ("\$value = isset( \$value[ \${$ tmpCount }]) ? \$value[ \${$ tmpCount }] : null; " )
158+ ->write ("\${$ a } = count( \$value) + {$ node ['index ' ]}; " )
159+ ->write ("\$value = isset( \$value[ \${$ a }]) ? \$value[ \${$ a }] : null; " )
152160 ->outdent ()
153161 ->write ('} else { ' )
154162 ->indent ()
@@ -179,9 +187,9 @@ private function visit_multi_select_list(array $node)
179187
180188 private function visit_multi_select_hash (array $ node )
181189 {
182- $ tmpCurrent = uniqid ( ' cur_ ' );
183- $ listVal = uniqid ( ' list_ ' );
184- $ value = uniqid ( ' prev_ ' );
190+ $ tmpCurrent = $ this -> makeVar ( ' cur ' );
191+ $ listVal = $ this -> makeVar ( ' list ' );
192+ $ value = $ this -> makeVar ( ' prev ' );
185193
186194 $ this
187195 ->write ('if ($value !== null) { ' )
@@ -215,9 +223,9 @@ private function visit_multi_select_hash(array $node)
215223
216224 private function visit_function (array $ node )
217225 {
218- $ value = uniqid ( ' value_ ' );
219- $ current = uniqid ( ' current_ ' );
220- $ args = uniqid ( ' args_ ' );
226+ $ value = $ this -> makeVar ( ' val ' );
227+ $ current = $ this -> makeVar ( ' current ' );
228+ $ args = $ this -> makeVar ( ' args ' );
221229
222230 $ this ->write ("\${$ value } = \$value; " )
223231 ->write ("\${$ current } = \$current; " )
@@ -266,9 +274,8 @@ private function visit_expression(array $node)
266274 private function visit_flatten (array $ node )
267275 {
268276 $ this ->dispatch ($ node ['children ' ][0 ]);
269-
270- $ tmpMerged = uniqid ('merged_ ' );
271- $ tmpVal = uniqid ('val_ ' );
277+ $ tmpMerged = $ this ->makeVar ('merged ' );
278+ $ tmpVal = $ this ->makeVar ('val ' );
272279
273280 $ this
274281 ->write ('// Visiting merge node ' )
@@ -313,8 +320,8 @@ private function visit_projection(array $node)
313320 $ this ->write ('if (!\JmesPath\TreeInterpreter::isArray($value)) $value = null; ' );
314321 }
315322
316- $ tmpVal = uniqid ( ' v ' );
317- $ tmpCollected = uniqid ( ' collected_ ' );
323+ $ tmpVal = $ this -> makeVar ( ' val ' );
324+ $ tmpCollected = $ this -> makeVar ( ' collected ' );
318325
319326 $ this ->write ('if ($value !== null) { ' )
320327 ->indent ()
@@ -351,10 +358,10 @@ private function visit_condition(array $node)
351358
352359 private function visit_comparator (array $ node )
353360 {
354- $ tmpValue = uniqid ( ' val_ ' );
355- $ tmpCurrent = uniqid ( ' cur_ ' );
356- $ tmpA = uniqid ( ' left_ ' );
357- $ tmpB = uniqid ( ' right_ ' );
361+ $ tmpValue = $ this -> makeVar ( ' val ' );
362+ $ tmpCurrent = $ this -> makeVar ( ' cur ' );
363+ $ tmpA = $ this -> makeVar ( ' left ' );
364+ $ tmpB = $ this -> makeVar ( ' right ' );
358365
359366 $ this
360367 ->write ('// Visiting comparator node ' )
0 commit comments