diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-01-24 15:48:29 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-01-24 15:48:29 +0000 |
| commit | 3a3f549c3ff0e59bcefef29e1b870686aebec532 (patch) | |
| tree | c0f0644a90ff65a22bd21ac2271f4db9894a0738 /src/main/java/org/zwobble/couscous/ast | |
| parent | 5941365d936f73e48381db5eb444345e5f358e75 (diff) | |
Introduce separate OperationNode that can be desugarred
Diffstat (limited to 'src/main/java/org/zwobble/couscous/ast')
| -rw-r--r-- | src/main/java/org/zwobble/couscous/ast/OperationNode.java | 75 | ||||
| -rw-r--r-- | src/main/java/org/zwobble/couscous/ast/Operations.java | 34 |
2 files changed, 92 insertions, 17 deletions
diff --git a/src/main/java/org/zwobble/couscous/ast/OperationNode.java b/src/main/java/org/zwobble/couscous/ast/OperationNode.java new file mode 100644 index 0000000..c3447ef --- /dev/null +++ b/src/main/java/org/zwobble/couscous/ast/OperationNode.java @@ -0,0 +1,75 @@ +package org.zwobble.couscous.ast; + +import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; +import org.zwobble.couscous.ast.visitors.NodeTransformer; + +import java.util.List; + +public class OperationNode implements ExpressionNode { + public static OperationNode operation(Operator operator, List<ExpressionNode> arguments, TypeName type) { + return new OperationNode(operator, arguments, type); + } + + private final Operator operator; + private final List<ExpressionNode> arguments; + private TypeName type; + + OperationNode(Operator operator, List<ExpressionNode> arguments, TypeName type) { + this.operator = operator; + this.arguments = arguments; + this.type = type; + } + + public ExpressionNode desugar() { + return MethodCallNode.methodCall( + arguments.get(0), + operator.getMethodName(), + arguments.subList(1, arguments.size()), + type); + } + + @Override + public TypeName getType() { + return type; + } + + @Override + public <T> T accept(ExpressionNodeMapper<T> visitor) { + return desugar().accept(visitor); + } + + @Override + public ExpressionNode transform(NodeTransformer transformer) { + return desugar().transform(transformer); + } + + @Override + public String toString() { + return "OperationNode(" + + "operator=" + operator + + ", arguments=" + arguments + + ", type=" + type + + ')'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + OperationNode that = (OperationNode) o; + + if (operator != that.operator) return false; + if (!arguments.equals(that.arguments)) return false; + return type.equals(that.type); + + } + + @Override + public int hashCode() { + int result = operator.hashCode(); + result = 31 * result + arguments.hashCode(); + result = 31 * result + type.hashCode(); + return result; + } +} diff --git a/src/main/java/org/zwobble/couscous/ast/Operations.java b/src/main/java/org/zwobble/couscous/ast/Operations.java index 40d9672..5af2f8b 100644 --- a/src/main/java/org/zwobble/couscous/ast/Operations.java +++ b/src/main/java/org/zwobble/couscous/ast/Operations.java @@ -26,63 +26,63 @@ public class Operations { } public static ExpressionNode booleanAnd(ExpressionNode left, ExpressionNode right) { - return booleanOperation(Operator.BOOLEAN_AND.getMethodName(), left, right); + return booleanOperation(Operator.BOOLEAN_AND, left, right); } public static ExpressionNode booleanOr(ExpressionNode left, ExpressionNode right) { - return booleanOperation(Operator.BOOLEAN_OR.getMethodName(), left, right); + return booleanOperation(Operator.BOOLEAN_OR, left, right); } public static ExpressionNode integerAdd(ExpressionNode left, ExpressionNode right) { - return integerOperation(Operator.ADD.getMethodName(), left, right); + return integerOperation(Operator.ADD, left, right); } public static ExpressionNode integerSubtract(ExpressionNode left, ExpressionNode right) { - return integerOperation(Operator.SUBTRACT.getMethodName(), left, right); + return integerOperation(Operator.SUBTRACT, left, right); } public static ExpressionNode integerMultiply(ExpressionNode left, ExpressionNode right) { - return integerOperation(Operator.MULTIPLY.getMethodName(), left, right); + return integerOperation(Operator.MULTIPLY, left, right); } public static ExpressionNode integerDivide(ExpressionNode left, ExpressionNode right) { - return integerOperation(Operator.DIVIDE.getMethodName(), left, right); + return integerOperation(Operator.DIVIDE, left, right); } public static ExpressionNode integerMod(ExpressionNode left, ExpressionNode right) { - return integerOperation(Operator.MOD.getMethodName(), left, right); + return integerOperation(Operator.MOD, left, right); } - public static MethodCallNode integerOperation(String methodName, ExpressionNode left, ExpressionNode right) { - return methodCall(left, methodName, list(right), IntegerValue.REF); + public static ExpressionNode integerOperation(Operator operator, ExpressionNode left, ExpressionNode right) { + return new OperationNode(operator, list(left, right), IntegerValue.REF); } public static ExpressionNode equal(ExpressionNode left, ExpressionNode right) { - return booleanOperation(Operator.EQUALS.getMethodName(), left, right); + return booleanOperation(Operator.EQUALS, left, right); } public static ExpressionNode notEqual(ExpressionNode left, ExpressionNode right) { - return booleanOperation(Operator.NOT_EQUALS.getMethodName(), left, right); + return booleanOperation(Operator.NOT_EQUALS, left, right); } public static ExpressionNode greaterThan(ExpressionNode left, ExpressionNode right) { - return booleanOperation(Operator.GREATER_THAN.getMethodName(), left, right); + return booleanOperation(Operator.GREATER_THAN, left, right); } public static ExpressionNode greaterThanOrEqual(ExpressionNode left, ExpressionNode right) { - return booleanOperation(Operator.GREATER_THAN_OR_EQUAL.getMethodName(), left, right); + return booleanOperation(Operator.GREATER_THAN_OR_EQUAL, left, right); } public static ExpressionNode lessThan(ExpressionNode left, ExpressionNode right) { - return booleanOperation(Operator.LESS_THAN.getMethodName(), left, right); + return booleanOperation(Operator.LESS_THAN, left, right); } public static ExpressionNode lessThanOrEqual(ExpressionNode left, ExpressionNode right) { - return booleanOperation(Operator.LESS_THAN_OR_EQUAL.getMethodName(), left, right); + return booleanOperation(Operator.LESS_THAN_OR_EQUAL, left, right); } - private static ExpressionNode booleanOperation(String methodName, ExpressionNode left, ExpressionNode right) { - return methodCall(left, methodName, list(right), BooleanValue.REF); + private static ExpressionNode booleanOperation(Operator operator, ExpressionNode left, ExpressionNode right) { + return new OperationNode(operator, list(left, right), BooleanValue.REF); } public static ExpressionNode boxInt(ExpressionNode value) { |
