aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/ast/WhileNode.java
blob: 7c06facb3f1c6c93a97ea4eaee8ce52558805525 (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
package org.zwobble.couscous.ast;

import com.google.common.base.Objects;
import org.zwobble.couscous.ast.visitors.NodeTransformer;
import org.zwobble.couscous.util.ExtraIterables;

import java.util.List;

public class WhileNode implements StatementNode {
    public static WhileNode whileLoop(ExpressionNode condition, List<StatementNode> body) {
        return new WhileNode(condition, body);
    }

    private final ExpressionNode condition;
    private final List<StatementNode> body;

    public WhileNode(ExpressionNode condition, List<StatementNode> body) {
        this.condition = condition;
        this.body = body;
    }

    public ExpressionNode getCondition() {
        return condition;
    }

    public List<StatementNode> getBody() {
        return body;
    }

    @Override
    public int type() {
        return NodeTypes.WHILE;
    }

    @Override
    public Iterable<? extends Node> childNodes() {
        return ExtraIterables.lazyCons(condition, body);
    }

    @Override
    public StatementNode transformSubtree(NodeTransformer transformer) {
        return new WhileNode(
            transformer.transformExpression(condition),
            transformer.transformStatements(body));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        WhileNode whileNode = (WhileNode) o;
        return Objects.equal(condition, whileNode.condition) &&
            Objects.equal(body, whileNode.body);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(condition, body);
    }

    @Override
    public String toString() {
        return "WhileNode(" +
            "condition=" + condition +
            ", body=" + body +
            ')';
    }
}