From 3a3f549c3ff0e59bcefef29e1b870686aebec532 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 24 Jan 2016 15:48:29 +0000 Subject: Introduce separate OperationNode that can be desugarred --- .../org/zwobble/couscous/ast/OperationNode.java | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/main/java/org/zwobble/couscous/ast/OperationNode.java (limited to 'src/main/java/org/zwobble/couscous/ast/OperationNode.java') 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 arguments, TypeName type) { + return new OperationNode(operator, arguments, type); + } + + private final Operator operator; + private final List arguments; + private TypeName type; + + OperationNode(Operator operator, List 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 accept(ExpressionNodeMapper 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; + } +} -- cgit v1.2.3