From f7a7bfd4764f2787e022fc0a9dd8eb57c151e90d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 7 Nov 2015 15:54:07 +0000 Subject: Add if-statement node --- .../org/zwobble/couscous/ast/IfStatementNode.java | 90 ++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/main/java/org/zwobble/couscous/ast/IfStatementNode.java (limited to 'src/main/java/org/zwobble/couscous/ast/IfStatementNode.java') diff --git a/src/main/java/org/zwobble/couscous/ast/IfStatementNode.java b/src/main/java/org/zwobble/couscous/ast/IfStatementNode.java new file mode 100644 index 0000000..ae3e29b --- /dev/null +++ b/src/main/java/org/zwobble/couscous/ast/IfStatementNode.java @@ -0,0 +1,90 @@ +package org.zwobble.couscous.ast; + +import java.util.List; + +import org.zwobble.couscous.ast.visitors.StatementNodeMapper; + +public class IfStatementNode implements StatementNode { + public static IfStatementNode ifStatement( + ExpressionNode condition, + List trueBranch, + List falseBranch) { + return new IfStatementNode(condition, trueBranch, falseBranch); + } + + private final ExpressionNode condition; + private final List trueBranch; + private final List falseBranch; + + private IfStatementNode( + ExpressionNode condition, + List trueBranch, + List falseBranch) { + this.condition = condition; + this.trueBranch = trueBranch; + this.falseBranch = falseBranch; + } + + public ExpressionNode getCondition() { + return condition; + } + + public List getTrueBranch() { + return trueBranch; + } + + public List getFalseBranch() { + return falseBranch; + } + + @Override + public T accept(StatementNodeMapper visitor) { + return visitor.visit(this); + } + + @Override + public String toString() { + return "IfStatementNode(condition=" + condition + ", trueBranch=" + + trueBranch + ", falseBranch=" + falseBranch + ")"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((condition == null) ? 0 : condition.hashCode()); + result = prime * result + + ((falseBranch == null) ? 0 : falseBranch.hashCode()); + result = prime * result + + ((trueBranch == null) ? 0 : trueBranch.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + IfStatementNode other = (IfStatementNode) obj; + if (condition == null) { + if (other.condition != null) + return false; + } else if (!condition.equals(other.condition)) + return false; + if (falseBranch == null) { + if (other.falseBranch != null) + return false; + } else if (!falseBranch.equals(other.falseBranch)) + return false; + if (trueBranch == null) { + if (other.trueBranch != null) + return false; + } else if (!trueBranch.equals(other.trueBranch)) + return false; + return true; + } +} -- cgit v1.2.3