Skip to content

Commit ad21eb0

Browse files
committed
Substitute Internal Variables by Counter
1 parent 1afe4b6 commit ad21eb0

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

liquidjava-verifier/src/main/java/liquidjava/rj_language/ast/Var.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,15 @@ public boolean equals(Object obj) {
7070
return name.equals(other.name);
7171
}
7272
}
73+
74+
public boolean isInternal() {
75+
return name.startsWith("#");
76+
}
77+
78+
public int getCounter() {
79+
if (!isInternal())
80+
throw new IllegalStateException("Cannot get counter of non-internal variable");
81+
int lastUnderscore = name.lastIndexOf('_');
82+
return Integer.parseInt(name.substring(lastUnderscore + 1));
83+
}
7384
}

liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/VariableResolver.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ private static void resolveRecursive(Expression exp, Map<String, Expression> map
5858
map.put(leftVar.getName(), right.clone());
5959
} else if (rightVar.isInternal() && !leftVar.isInternal()) {
6060
map.put(rightVar.getName(), left.clone());
61+
} else if (leftVar.isInternal() && rightVar.isInternal()) {
62+
// substitute the lower-counter variable with the higher-counter one
63+
boolean isLeftCounterLower = leftVar.getCounter() <= rightVar.getCounter();
64+
Var lowerVar = isLeftCounterLower ? leftVar : rightVar;
65+
Var higherVar = isLeftCounterLower ? rightVar : leftVar;
66+
map.putIfAbsent(lowerVar.getName(), higherVar.clone());
6167
}
6268
}
6369
}

0 commit comments

Comments
 (0)