@@ -47,6 +47,7 @@ public static VCImplication apply(VCImplication implication) {
4747 * Folds the first foldable expression found
4848 */
4949 private static Expression fold (Expression expression ) {
50+ // enum constant -> literal
5051 if (expression instanceof Enum en && en .getResolvedLiteral () != null )
5152 return en .getResolvedLiteral ().clone ();
5253 if (expression instanceof BinaryExpression binary )
@@ -55,6 +56,7 @@ private static Expression fold(Expression expression) {
5556 return foldUnary (unary );
5657 if (expression instanceof Ite ite )
5758 return foldIte (ite );
59+ // (x) -> x
5860 if (expression instanceof GroupExpression group && group .getChildren ().size () == 1 )
5961 return group .getExpression ().clone ();
6062 return expression .clone ();
@@ -98,10 +100,13 @@ private static Expression foldUnary(UnaryExpression unary) {
98100
99101 String op = unary .getOp ();
100102
103+ // !true -> false
104+ // !false -> true
101105 if ("!" .equals (op ) && operand instanceof LiteralBoolean literal )
102106 return new LiteralBoolean (!literal .isBooleanTrue ());
103107
104108 if ("-" .equals (op )) {
109+ // -(x) -> -x
105110 if (operand instanceof LiteralInt literal )
106111 return new LiteralInt (-literal .getValue ());
107112 if (operand instanceof LiteralReal literal )
@@ -130,9 +135,12 @@ private static Expression foldIte(Ite ite) {
130135 if (!elseExpression .equals (foldedElse ))
131136 return new Ite (condition .clone (), thenExpression .clone (), foldedElse );
132137
138+ // true ? x : y -> x
139+ // false ? x : y -> y
133140 if (condition instanceof LiteralBoolean literal )
134141 return literal .isBooleanTrue () ? thenExpression : elseExpression ;
135142
143+ // y ? x : x -> x
136144 if (thenExpression .equals (elseExpression ))
137145 return thenExpression ;
138146
@@ -161,6 +169,8 @@ private static Expression foldLiteralBinary(Expression left, Expression right, S
161169 if (left instanceof Enum leftEnum && right instanceof Enum rightEnum
162170 && leftEnum .getTypeName ().equals (rightEnum .getTypeName ())) {
163171 boolean equal = leftEnum .getConstName ().equals (rightEnum .getConstName ());
172+ // Enum.A == Enum.A -> true
173+ // Enum.A != Enum.B -> true
164174 return switch (op ) {
165175 case "==" -> new LiteralBoolean (equal );
166176 case "!=" -> new LiteralBoolean (!equal );
@@ -191,10 +201,13 @@ private static Expression foldAdjacentInts(Expression left, Expression right, St
191201 int signedRight = "+" .equals (op ) ? rightLiteral .getValue () : -rightLiteral .getValue ();
192202 int constant = signedLeft + signedRight ;
193203 Expression base = leftBinary .getFirstOperand ().clone ();
204+ // x + n - n -> x
194205 if (constant == 0 )
195206 return base ;
207+ // x + n + m -> x + k
196208 if (constant > 0 )
197209 return new BinaryExpression (base , "+" , new LiteralInt (constant ));
210+ // x + n - m -> x - k
198211 return new BinaryExpression (base , "-" , new LiteralInt (-constant ));
199212 }
200213
0 commit comments