-
Notifications
You must be signed in to change notification settings - Fork 35
Simplify Expressions #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
52b9225
Simplify Expressions
rcosta358 cd52d41
Simple Approach
rcosta358 c3d05d1
Build Derivation with Linked List
rcosta358 9ef9595
Improve Switch Cases
rcosta358 0a69e86
Fix Unary Minus Constant Folding
rcosta358 0350e9a
Refactoring
rcosta358 b3d1518
Simplify Expressions Using Derivation Nodes
rcosta358 0374a67
Fix Circular References in Variable Resolver
rcosta358 328a839
Add Tests for ExpressionSimplifier (Sonnet 4.5)
rcosta358 88e063c
Renamed printCostumeError to printCustomError
rcosta358 893b1e0
DerivationNode JSON Serialization Using Gson
rcosta358 1508985
Add Documentation to Methods
rcosta358 9dfc61d
Merge branch 'main' into simplify-expressions
rcosta358 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/ConstantFolding.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| package liquidjava.rj_language.opt; | ||
|
|
||
| import liquidjava.rj_language.ast.BinaryExpression; | ||
| import liquidjava.rj_language.ast.Expression; | ||
| import liquidjava.rj_language.ast.GroupExpression; | ||
| import liquidjava.rj_language.ast.LiteralBoolean; | ||
| import liquidjava.rj_language.ast.LiteralInt; | ||
| import liquidjava.rj_language.ast.LiteralReal; | ||
| import liquidjava.rj_language.ast.UnaryExpression; | ||
| import liquidjava.rj_language.opt.derivation_node.BinaryDerivationNode; | ||
| import liquidjava.rj_language.opt.derivation_node.DerivationNode; | ||
| import liquidjava.rj_language.opt.derivation_node.UnaryDerivationNode; | ||
| import liquidjava.rj_language.opt.derivation_node.ValDerivationNode; | ||
|
|
||
| public class ConstantFolding { | ||
|
|
||
| public static ValDerivationNode fold(ValDerivationNode node) { | ||
| Expression exp = node.getValue(); | ||
| if (exp instanceof BinaryExpression) { | ||
| return foldBinary(node); | ||
| } | ||
| if (exp instanceof UnaryExpression) { | ||
| return foldUnary(node); | ||
| } | ||
| if (exp instanceof GroupExpression) { | ||
| GroupExpression group = (GroupExpression) exp; | ||
| if (group.getChildren().size() == 1) { | ||
| return fold(new ValDerivationNode(group.getChildren().get(0), node.getOrigin())); | ||
| } | ||
| } | ||
| return node; | ||
| } | ||
|
|
||
| private static ValDerivationNode foldBinary(ValDerivationNode node) { | ||
| BinaryExpression binExp = (BinaryExpression) node.getValue(); | ||
| DerivationNode parent = node.getOrigin(); | ||
|
|
||
| // fold child nodes | ||
| ValDerivationNode leftNode; | ||
| ValDerivationNode rightNode; | ||
| if (parent instanceof BinaryDerivationNode) { | ||
| // has origin (from constant propagation) | ||
| BinaryDerivationNode binaryOrigin = (BinaryDerivationNode) parent; | ||
| leftNode = fold(binaryOrigin.getLeft()); | ||
| rightNode = fold(binaryOrigin.getRight()); | ||
| } else { | ||
| // no origin | ||
| leftNode = fold(new ValDerivationNode(binExp.getFirstOperand(), null)); | ||
| rightNode = fold(new ValDerivationNode(binExp.getSecondOperand(), null)); | ||
| } | ||
|
|
||
| Expression left = leftNode.getValue(); | ||
| Expression right = rightNode.getValue(); | ||
| String op = binExp.getOperator(); | ||
| binExp.setChild(0, left); | ||
| binExp.setChild(1, right); | ||
|
|
||
| // int and int | ||
| if (left instanceof LiteralInt && right instanceof LiteralInt) { | ||
| int l = ((LiteralInt) left).getValue(); | ||
| int r = ((LiteralInt) right).getValue(); | ||
| Expression res = switch (op) { | ||
| case "+" -> new LiteralInt(l + r); | ||
| case "-" -> new LiteralInt(l - r); | ||
| case "*" -> new LiteralInt(l * r); | ||
| case "/" -> r != 0 ? new LiteralInt(l / r) : null; | ||
| case "%" -> r != 0 ? new LiteralInt(l % r) : null; | ||
| case "<" -> new LiteralBoolean(l < r); | ||
| case "<=" -> new LiteralBoolean(l <= r); | ||
| case ">" -> new LiteralBoolean(l > r); | ||
| case ">=" -> new LiteralBoolean(l >= r); | ||
| case "==" -> new LiteralBoolean(l == r); | ||
| case "!=" -> new LiteralBoolean(l != r); | ||
| default -> null; | ||
| }; | ||
| if (res != null) | ||
| return new ValDerivationNode(res, new BinaryDerivationNode(leftNode, rightNode, op)); | ||
| } | ||
| // real and real | ||
| else if (left instanceof LiteralReal && right instanceof LiteralReal) { | ||
| double l = ((LiteralReal) left).getValue(); | ||
| double r = ((LiteralReal) right).getValue(); | ||
| Expression res = switch (op) { | ||
| case "+" -> new LiteralReal(l + r); | ||
| case "-" -> new LiteralReal(l - r); | ||
| case "*" -> new LiteralReal(l * r); | ||
| case "/" -> r != 0.0 ? new LiteralReal(l / r) : null; | ||
| case "%" -> r != 0.0 ? new LiteralReal(l % r) : null; | ||
| case "<" -> new LiteralBoolean(l < r); | ||
| case "<=" -> new LiteralBoolean(l <= r); | ||
| case ">" -> new LiteralBoolean(l > r); | ||
| case ">=" -> new LiteralBoolean(l >= r); | ||
| case "==" -> new LiteralBoolean(l == r); | ||
| case "!=" -> new LiteralBoolean(l != r); | ||
| default -> null; | ||
| }; | ||
| if (res != null) | ||
| return new ValDerivationNode(res, new BinaryDerivationNode(leftNode, rightNode, op)); | ||
| } | ||
|
|
||
| // mixed int and real | ||
| else if ((left instanceof LiteralInt && right instanceof LiteralReal) | ||
| || (left instanceof LiteralReal && right instanceof LiteralInt)) { | ||
| double l = left instanceof LiteralInt ? ((LiteralInt) left).getValue() : ((LiteralReal) left).getValue(); | ||
| double r = right instanceof LiteralInt ? ((LiteralInt) right).getValue() : ((LiteralReal) right).getValue(); | ||
| Expression res = switch (op) { | ||
| case "+" -> new LiteralReal(l + r); | ||
| case "-" -> new LiteralReal(l - r); | ||
| case "*" -> new LiteralReal(l * r); | ||
| case "/" -> r != 0.0 ? new LiteralReal(l / r) : null; | ||
| case "%" -> r != 0.0 ? new LiteralReal(l % r) : null; | ||
| case "<" -> new LiteralBoolean(l < r); | ||
| case "<=" -> new LiteralBoolean(l <= r); | ||
| case ">" -> new LiteralBoolean(l > r); | ||
| case ">=" -> new LiteralBoolean(l >= r); | ||
| case "==" -> new LiteralBoolean(l == r); | ||
| case "!=" -> new LiteralBoolean(l != r); | ||
| default -> null; | ||
| }; | ||
| if (res != null) | ||
| return new ValDerivationNode(res, new BinaryDerivationNode(leftNode, rightNode, op)); | ||
| } | ||
| // bool and bool | ||
| else if (left instanceof LiteralBoolean && right instanceof LiteralBoolean) { | ||
| boolean l = ((LiteralBoolean) left).isBooleanTrue(); | ||
| boolean r = ((LiteralBoolean) right).isBooleanTrue(); | ||
| Expression res = switch (op) { | ||
| case "&&" -> new LiteralBoolean(l && r); | ||
| case "||" -> new LiteralBoolean(l || r); | ||
| case "-->" -> new LiteralBoolean(!l || r); | ||
| case "==" -> new LiteralBoolean(l == r); | ||
| case "!=" -> new LiteralBoolean(l != r); | ||
| default -> null; | ||
| }; | ||
| if (res != null) | ||
| return new ValDerivationNode(res, new BinaryDerivationNode(leftNode, rightNode, op)); | ||
| } | ||
|
|
||
| // no folding | ||
| DerivationNode origin = (leftNode.getOrigin() != null || rightNode.getOrigin() != null) | ||
| ? new BinaryDerivationNode(leftNode, rightNode, op) : null; | ||
| return new ValDerivationNode(binExp, origin); | ||
| } | ||
|
|
||
| private static ValDerivationNode foldUnary(ValDerivationNode node) { | ||
| UnaryExpression unaryExp = (UnaryExpression) node.getValue(); | ||
| DerivationNode parent = node.getOrigin(); | ||
|
|
||
| // fold child node | ||
| ValDerivationNode operandNode; | ||
| if (parent instanceof UnaryDerivationNode) { | ||
| // has origin (from constant propagation) | ||
| UnaryDerivationNode unaryOrigin = (UnaryDerivationNode) parent; | ||
| operandNode = fold(unaryOrigin.getOperand()); | ||
| } else { | ||
| // no origin | ||
| operandNode = fold(new ValDerivationNode(unaryExp.getChildren().get(0), null)); | ||
| } | ||
| Expression operand = operandNode.getValue(); | ||
| String operator = unaryExp.getOp(); | ||
| unaryExp.setChild(0, operand); | ||
|
|
||
| // unary not | ||
| if ("!".equals(operator) && operand instanceof LiteralBoolean) { | ||
| // !true => false, !false => true | ||
| boolean value = ((LiteralBoolean) operand).isBooleanTrue(); | ||
| Expression res = new LiteralBoolean(!value); | ||
| return new ValDerivationNode(res, new UnaryDerivationNode(operandNode, operator)); | ||
| } | ||
| // unary minus | ||
| if ("-".equals(operator)) { | ||
| // -(x) => -x | ||
| if (operand instanceof LiteralInt) { | ||
| Expression res = new LiteralInt(-((LiteralInt) operand).getValue()); | ||
| return new ValDerivationNode(res, new UnaryDerivationNode(operandNode, operator)); | ||
| } | ||
| if (operand instanceof LiteralReal) { | ||
| Expression res = new LiteralReal(-((LiteralReal) operand).getValue()); | ||
| return new ValDerivationNode(res, new UnaryDerivationNode(operandNode, operator)); | ||
| } | ||
| } | ||
|
|
||
| // no folding | ||
| DerivationNode origin = operandNode.getOrigin() != null ? new UnaryDerivationNode(operandNode, operator) : null; | ||
| return new ValDerivationNode(unaryExp, origin); | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
liquidjava-verifier/src/main/java/liquidjava/rj_language/opt/ConstantPropagation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package liquidjava.rj_language.opt; | ||
|
|
||
| import liquidjava.rj_language.ast.BinaryExpression; | ||
| import liquidjava.rj_language.ast.Expression; | ||
| import liquidjava.rj_language.ast.UnaryExpression; | ||
| import liquidjava.rj_language.ast.Var; | ||
| import liquidjava.rj_language.opt.derivation_node.BinaryDerivationNode; | ||
| import liquidjava.rj_language.opt.derivation_node.DerivationNode; | ||
| import liquidjava.rj_language.opt.derivation_node.UnaryDerivationNode; | ||
| import liquidjava.rj_language.opt.derivation_node.ValDerivationNode; | ||
| import liquidjava.rj_language.opt.derivation_node.VarDerivationNode; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class ConstantPropagation { | ||
|
|
||
| public static ValDerivationNode propagate(Expression exp) { | ||
| Map<String, Expression> substitutions = VariableResolver.resolve(exp); | ||
| return propagateRecursive(exp, substitutions); | ||
| } | ||
|
|
||
| private static ValDerivationNode propagateRecursive(Expression exp, Map<String, Expression> subs) { | ||
|
|
||
| // substitute variable | ||
| if (exp instanceof Var) { | ||
| Var var = (Var) exp; | ||
| String name = var.getName(); | ||
| Expression value = subs.get(name); | ||
| // substitution | ||
| if (value != null) | ||
| return new ValDerivationNode(value.clone(), new VarDerivationNode(name)); | ||
|
|
||
| // no substitution | ||
| return new ValDerivationNode(var, null); | ||
| } | ||
|
|
||
| // lift unary origin | ||
| if (exp instanceof UnaryExpression) { | ||
| UnaryExpression unary = (UnaryExpression) exp; | ||
| ValDerivationNode operand = propagateRecursive(unary.getChildren().get(0), subs); | ||
| unary.setChild(0, operand.getValue()); | ||
|
|
||
| DerivationNode origin = operand.getOrigin() != null ? new UnaryDerivationNode(operand, unary.getOp()) | ||
| : null; | ||
| return new ValDerivationNode(unary, origin); | ||
| } | ||
|
|
||
| // lift binary origin | ||
| if (exp instanceof BinaryExpression) { | ||
| BinaryExpression binary = (BinaryExpression) exp; | ||
| ValDerivationNode left = propagateRecursive(binary.getFirstOperand(), subs); | ||
| ValDerivationNode right = propagateRecursive(binary.getSecondOperand(), subs); | ||
| binary.setChild(0, left.getValue()); | ||
| binary.setChild(1, right.getValue()); | ||
|
|
||
| DerivationNode origin = (left.getOrigin() != null || right.getOrigin() != null) | ||
| ? new BinaryDerivationNode(left, right, binary.getOperator()) : null; | ||
| return new ValDerivationNode(binary, origin); | ||
| } | ||
|
|
||
| // recursively propagate children | ||
| if (exp.hasChildren()) { | ||
| Expression propagated = exp.clone(); | ||
| for (int i = 0; i < exp.getChildren().size(); i++) { | ||
| ValDerivationNode child = propagateRecursive(exp.getChildren().get(i), subs); | ||
| propagated.setChild(i, child.getValue()); | ||
| } | ||
| return new ValDerivationNode(propagated, null); | ||
| } | ||
|
|
||
| // no propagation | ||
| return new ValDerivationNode(exp, null); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.