7
7
import de .peeeq .wurstscript .jassIm .*;
8
8
import de .peeeq .wurstscript .translation .imtranslation .ImTranslator .VarsForTupleResult ;
9
9
import de .peeeq .wurstscript .types .TypesHelper ;
10
- import de .peeeq .wurstscript .utils .Pair ;
11
10
import de .peeeq .wurstscript .utils .Utils ;
12
11
13
12
import java .util .ArrayList ;
24
23
*/
25
24
public class EliminateTuples {
26
25
27
- /*
28
- TODO this could be simplified:
29
26
30
- Visit and rewrite only the expressions that must be changed:
31
-
32
-
33
-
34
- 2. Normalize Tuples in statement-expressions (move to first tuple param)
35
-
36
- Normalize
37
- {stmts >> <e1,e2,e3>}
38
- becomes <{stmts >> e1}, e2, e3}
39
-
40
-
41
- 3. Remove tuple expressions
42
-
43
-
44
- - In parameters: Just flatten
45
- - Assignments: Become several assignments
46
- - In Return: Use temp returns
47
-
48
- */
49
27
50
28
public static void eliminateTuplesProg (ImProg imProg , ImTranslator translator ) {
51
29
@@ -108,7 +86,6 @@ public void visit(ImTupleSelection ts) {
108
86
extractSideEffect (te , stmts );
109
87
} else { // if it is the part we want to return ...
110
88
result = extractSideEffect (te , stmts );
111
- // TODO in this and all the following remove side effects and add them to stmts
112
89
}
113
90
}
114
91
assert result != null ;
@@ -404,8 +381,16 @@ private static ImStatementExpr inSet(ImSet imSet, ImFunction f) {
404
381
ImTupleExpr right = (ImTupleExpr ) imSet .getRight ();
405
382
406
383
ImStmts stmts = JassIm .ImStmts ();
384
+
385
+ // 1) extract side effects from left expressions
386
+ List <ImExpr > leftExprs = new ArrayList <>();
387
+ for (ImExpr expr : left .getExprs ()) {
388
+ leftExprs .add (extractSideEffect (expr , stmts ));
389
+ }
390
+
391
+
407
392
List <ImVar > tempVars = new ArrayList <>();
408
- // first assign right hand side to temporary variables:
393
+ // 2) assign right hand side to temporary variables:
409
394
for (ImExpr expr : right .getExprs ()) {
410
395
ImVar temp = JassIm .ImVar (expr .attrTrace (), expr .attrTyp (), "tuple_temp" , false );
411
396
expr .setParent (null );
@@ -414,9 +399,8 @@ private static ImStatementExpr inSet(ImSet imSet, ImFunction f) {
414
399
f .getLocals ().add (temp );
415
400
}
416
401
// then assign right vars
417
- // TODO side effects from left expressions should come before side effects on right
418
- for (int i = 0 ; i < left .getExprs ().size (); i ++) {
419
- ImLExpr leftE = (ImLExpr ) left .getExprs ().get (i );
402
+ for (int i = 0 ; i < leftExprs .size (); i ++) {
403
+ ImLExpr leftE = (ImLExpr ) leftExprs .get (i );
420
404
leftE .setParent (null );
421
405
stmts .add (JassIm .ImSet (imSet .getTrace (), leftE , JassIm .ImVarAccess (tempVars .get (i ))));
422
406
}
0 commit comments