aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/ast/AssignmentNode.java
blob: a2f9f2d54b8c1ac6bb2979bb51034afe43589d00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package org.zwobble.couscous.ast;

import org.zwobble.couscous.types.Type;
import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper;
import org.zwobble.couscous.ast.visitors.NodeTransformer;
import org.zwobble.couscous.util.ExtraIterables;

import static org.zwobble.couscous.ast.ExpressionStatementNode.expressionStatement;
import static org.zwobble.couscous.ast.VariableReferenceNode.reference;

public class AssignmentNode implements ExpressionNode {
    public static StatementNode assignStatement(AssignableExpressionNode target, ExpressionNode value) {
        return expressionStatement(assign(target, value));
    }
    
    public static AssignmentNode assign(AssignableExpressionNode target, ExpressionNode value) {
        return new AssignmentNode(target, value);
    }
    
    public static StatementNode assignStatement(VariableNode target, ExpressionNode value) {
        return expressionStatement(assign(target, value));
    }
    
    public static AssignmentNode assign(VariableNode target, ExpressionNode value) {
        return new AssignmentNode(reference(target), value);
    }
    
    private final AssignableExpressionNode target;
    private final ExpressionNode value;
    
    private AssignmentNode(AssignableExpressionNode target, ExpressionNode value) {
        this.target = target;
        this.value = value;
    }
    
    public AssignableExpressionNode getTarget() {
        return target;
    }
    
    public ExpressionNode getValue() {
        return value;
    }
    
    @Override
    public <T> T accept(ExpressionNodeMapper<T> visitor) {
        return visitor.visit(this);
    }

    @Override
    public Iterable<Node> childNodes() {
        return ExtraIterables.of(value, target);
    }

    @Override
    public ExpressionNode transform(NodeTransformer transformer) {
        return new AssignmentNode(
            (AssignableExpressionNode) transformer.transformExpression(target),
            transformer.transformExpression(value));
    }

    @Override
    public Type getType() {
        return value.getType();
    }

    @Override
    public String toString() {
        return "AssignmentNode(target=" + target + ", value=" + value + ")";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((target == null) ? 0 : target.hashCode());
        result = prime * result + ((value == null) ? 0 : value.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;
        AssignmentNode other = (AssignmentNode) obj;
        if (target == null) {
            if (other.target != null)
                return false;
        } else if (!target.equals(other.target))
            return false;
        if (value == null) {
            if (other.value != null)
                return false;
        } else if (!value.equals(other.value))
            return false;
        return true;
    }
}