Skip to content

Commit 0350e9a

Browse files
committed
Refactoring
1 parent 0a69e86 commit 0350e9a

3 files changed

Lines changed: 13 additions & 13 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public static Expression fold(Expression exp) {
2222
// try to fold the current expression
2323
if (exp instanceof BinaryExpression) {
2424
return foldBinaryExpression((BinaryExpression) exp);
25-
} else if (exp instanceof UnaryExpression) {
25+
}
26+
if (exp instanceof UnaryExpression) {
2627
return foldUnaryExpression((UnaryExpression) exp);
2728
}
2829
return exp;
@@ -55,7 +56,7 @@ private static Expression foldBinaryExpression(BinaryExpression binExp) {
5556
}
5657

5758
// arithmetic operations with real literals
58-
else if (left instanceof LiteralReal && right instanceof LiteralReal) {
59+
if (left instanceof LiteralReal && right instanceof LiteralReal) {
5960
double l = ((LiteralReal) left).getValue();
6061
double r = ((LiteralReal) right).getValue();
6162
return switch (op) {
@@ -75,7 +76,7 @@ else if (left instanceof LiteralReal && right instanceof LiteralReal) {
7576
}
7677

7778
// mixed integer and real operations
78-
else if ((left instanceof LiteralInt && right instanceof LiteralReal) || (left instanceof LiteralReal && right instanceof LiteralInt)) {
79+
if ((left instanceof LiteralInt && right instanceof LiteralReal) || (left instanceof LiteralReal && right instanceof LiteralInt)) {
7980
double l = left instanceof LiteralInt ? ((LiteralInt) left).getValue() : ((LiteralReal) left).getValue();
8081
double r = right instanceof LiteralInt ? ((LiteralInt) right).getValue() : ((LiteralReal) right).getValue();
8182
return switch (op) {
@@ -95,7 +96,7 @@ else if ((left instanceof LiteralInt && right instanceof LiteralReal) || (left i
9596
}
9697

9798
// boolean operations with boolean literals
98-
else if (left instanceof LiteralBoolean && right instanceof LiteralBoolean) {
99+
if (left instanceof LiteralBoolean && right instanceof LiteralBoolean) {
99100
boolean l = ((LiteralBoolean) left).isBooleanTrue();
100101
boolean r = ((LiteralBoolean) right).isBooleanTrue();
101102
return switch (op) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class ConstantPropagation {
88

99
public static Expression propagate(Expression exp) {
10-
Map<String, Expression> substitutions = VariableExtractor.extract(exp);
10+
Map<String, Expression> substitutions = VariableCollector.collect(exp);
1111
return propagateRecursive(exp, substitutions);
1212
}
1313

liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/VariableExtractor.java renamed to liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/VariableCollector.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
import liquidjava.rj_language.ast.LiteralReal;
1111
import liquidjava.rj_language.ast.Var;
1212

13-
public class VariableExtractor {
13+
public class VariableCollector {
1414

15-
public static Map<String, Expression> extract(Expression exp) {
15+
public static Map<String, Expression> collect(Expression exp) {
1616
Map<String, Expression> assertions = new HashMap<>();
1717

1818
// only extract assertions if the expression contains conjunctions (&&)
1919
if (containsConjunction(exp)) {
20-
extractRecursive(exp, assertions);
20+
collectRecursive(exp, assertions);
2121
}
2222
return assertions;
2323
}
@@ -41,20 +41,19 @@ private static boolean containsConjunction(Expression exp) {
4141
return false;
4242
}
4343

44-
private static void extractRecursive(Expression exp, Map<String, Expression> assertions) {
44+
private static void collectRecursive(Expression exp, Map<String, Expression> assertions) {
4545
if (exp instanceof BinaryExpression) {
4646
BinaryExpression binExp = (BinaryExpression) exp;
4747
String operator = binExp.getOperator();
4848

4949
if (operator.equals("&&")) {
5050
// for conjunctions recursively extract from both sides
51-
extractRecursive(binExp.getFirstOperand(), assertions);
52-
extractRecursive(binExp.getSecondOperand(), assertions);
51+
collectRecursive(binExp.getFirstOperand(), assertions);
52+
collectRecursive(binExp.getSecondOperand(), assertions);
5353
} else if (operator.equals("==")) {
5454
// for assertions check if one side is a variable and the other is a literal
5555
Expression left = binExp.getFirstOperand();
5656
Expression right = binExp.getSecondOperand();
57-
5857
if (left instanceof Var && isLiteral(right)) {
5958
assertions.put(((Var) left).getName(), right);
6059
} else if (right instanceof Var && isLiteral(left)) {
@@ -65,7 +64,7 @@ private static void extractRecursive(Expression exp, Map<String, Expression> ass
6564
// for other expressions, recurse into children
6665
else if (exp.hasChildren()) {
6766
for (Expression child : exp.getChildren()) {
68-
extractRecursive(child, assertions);
67+
collectRecursive(child, assertions);
6968
}
7069
}
7170
}

0 commit comments

Comments
 (0)