Skip to content

Commit 1508985

Browse files
committed
Add Documentation to Methods
1 parent 893b1e0 commit 1508985

File tree

6 files changed

+47
-13
lines changed

6 files changed

+47
-13
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414

1515
public class ConstantFolding {
1616

17+
/**
18+
* Performs constant folding on a derivation node by evaluating nodes with constant values. Returns a new derivation
19+
* node representing the folding steps taken
20+
*/
1721
public static ValDerivationNode fold(ValDerivationNode node) {
1822
Expression exp = node.getValue();
19-
if (exp instanceof BinaryExpression) {
23+
if (exp instanceof BinaryExpression)
2024
return foldBinary(node);
21-
}
22-
if (exp instanceof UnaryExpression) {
25+
26+
if (exp instanceof UnaryExpression)
2327
return foldUnary(node);
24-
}
28+
2529
if (exp instanceof GroupExpression) {
2630
GroupExpression group = (GroupExpression) exp;
2731
if (group.getChildren().size() == 1) {
@@ -31,6 +35,9 @@ public static ValDerivationNode fold(ValDerivationNode node) {
3135
return node;
3236
}
3337

38+
/**
39+
* Folds a binary expression node if both children are constant values (e.g. 1 + 2 => 3)
40+
*/
3441
private static ValDerivationNode foldBinary(ValDerivationNode node) {
3542
BinaryExpression binExp = (BinaryExpression) node.getValue();
3643
DerivationNode parent = node.getOrigin();
@@ -142,6 +149,9 @@ else if (left instanceof LiteralBoolean && right instanceof LiteralBoolean) {
142149
return new ValDerivationNode(binExp, origin);
143150
}
144151

152+
/**
153+
* Folds a unary expression node if the child (operand) is a constant value (e.g. !true => false)
154+
*/
145155
private static ValDerivationNode foldUnary(ValDerivationNode node) {
146156
UnaryExpression unaryExp = (UnaryExpression) node.getValue();
147157
DerivationNode parent = node.getOrigin();

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@
1414

1515
public class ConstantPropagation {
1616

17+
/**
18+
* Performs constant propagation on an expression, by substituting variables with their constant values. Uses the
19+
* VariableResolver to extract variable equalities from the expression first. Returns a derivation node representing
20+
* the propagation steps taken.
21+
*/
1722
public static ValDerivationNode propagate(Expression exp) {
1823
Map<String, Expression> substitutions = VariableResolver.resolve(exp);
1924
return propagateRecursive(exp, substitutions);
2025
}
2126

27+
/**
28+
* Recursively performs constant propagation on an expression (e.g. x + y && x == 1 && y == 2 => 1 + 2)
29+
*/
2230
private static ValDerivationNode propagateRecursive(Expression exp, Map<String, Expression> subs) {
2331

2432
// substitute variable

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99

1010
public class ExpressionSimplifier {
1111

12+
/**
13+
* Simplifies an expression by applying constant propagation, constant folding and removing redundant conjuncts
14+
* Returns a derivation node representing the tree of simplifications applied
15+
*/
1216
public static ValDerivationNode simplify(Expression exp) {
1317
ValDerivationNode prop = ConstantPropagation.propagate(exp);
1418
ValDerivationNode fold = ConstantFolding.fold(prop);
1519
return simplifyDerivationTree(fold);
1620
}
1721

1822
/**
19-
* Recursively simplify the derivation tree by removing redundant conjuncts
23+
* Recursively simplifies the derivation tree by removing redundant conjuncts
2024
*/
2125
private static ValDerivationNode simplifyDerivationTree(ValDerivationNode node) {
2226
Expression value = node.getValue();
@@ -49,7 +53,7 @@ private static ValDerivationNode simplifyDerivationTree(ValDerivationNode node)
4953
}
5054

5155
/**
52-
* Check if an expression is redundant
56+
* Checks if an expression is redundant (e.g. true or x == x)
5357
*/
5458
private static boolean isRedundant(Expression exp) {
5559
// true

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@
1111

1212
public class VariableResolver {
1313

14+
/**
15+
* Extracts variables with constant values from an expression Returns a map from variable names to their values
16+
*/
1417
public static Map<String, Expression> resolve(Expression exp) {
1518
Map<String, Expression> map = new HashMap<>();
1619
resolveRecursive(exp, map);
1720
return resolveTransitive(map);
1821
}
1922

23+
/**
24+
* Recursively extracts variable equalities from an expression (e.g. ... && x == 1 && y == 2 => map: x -> 1, y -> 2)
25+
* Modifies the given map in place
26+
*/
2027
private static void resolveRecursive(Expression exp, Map<String, Expression> map) {
2128
if (!(exp instanceof BinaryExpression))
2229
return;
@@ -30,14 +37,16 @@ private static void resolveRecursive(Expression exp, Map<String, Expression> map
3037
Expression left = be.getFirstOperand();
3138
Expression right = be.getSecondOperand();
3239
if (left instanceof Var && (right.isLiteral() || right instanceof Var)) {
33-
map.put(left.toString(), right.clone());
40+
map.put(((Var) left).getName(), right.clone());
3441
} else if (right instanceof Var && left.isLiteral()) {
35-
map.put(right.toString(), left.clone());
42+
map.put(((Var) right).getName(), left.clone());
3643
}
3744
}
3845
}
3946

40-
// e.g. x == y && y == 1 => x == 1
47+
/**
48+
* Handles transitive variable equalities in the map (e.g. map: x -> y, y -> 1 => map: x -> 1, y -> 1)
49+
*/
4150
private static Map<String, Expression> resolveTransitive(Map<String, Expression> map) {
4251
Map<String, Expression> result = new HashMap<>();
4352
for (Map.Entry<String, Expression> entry : map.entrySet()) {
@@ -46,6 +55,10 @@ private static Map<String, Expression> resolveTransitive(Map<String, Expression>
4655
return result;
4756
}
4857

58+
/**
59+
* Returns the value of a variable by looking up in the map recursively Uses the seen set to avoid circular
60+
* references (e.g. x -> y, y -> x) which would cause infinite recursion
61+
*/
4962
private static Expression lookup(Expression exp, Map<String, Expression> map, Set<String> seen) {
5063
if (!(exp instanceof Var))
5164
return exp;

liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/derivation_node/DerivationNode.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
public abstract class DerivationNode {
77

8-
private static final Gson gson = new GsonBuilder()
9-
.setPrettyPrinting() // remove later
10-
.disableHtmlEscaping() // to not escape characters like &, >, <, =, etc.
11-
.create();
8+
// disable html escaping to avoid escaping characters like &, >, <, =, etc.
9+
private static final Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
1210

1311
@Override
1412
public String toString() {

liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/derivation_node/ValDerivationNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public DerivationNode getOrigin() {
3434
return origin;
3535
}
3636

37+
// Custom serializer to handle Expression subclasses properly
3738
private static class ExpressionSerializer implements JsonSerializer<Expression> {
3839
@Override
3940
public JsonElement serialize(Expression exp, Type typeOfSrc, JsonSerializationContext context) {

0 commit comments

Comments
 (0)