summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v2/Node.java
blob: 334a9f39325fede10645b32737ac2a0329e452d4 (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
package bjc.dicelang.v2;

public class Node {
	public static enum Type {
		 ROOT,    TOKREF,
		 UNARYOP, BINOP,
		 GROUP,   OGROUP,
		 RESULT
	}

	public static enum GroupType {
		ARRAY, CODE
	}

	public final Type type;

	// These can have or not have values based of the node type
	public Token      		tokenVal;
	public Token.Type 		operatorType;
	public GroupType 		groupType;
	public EvaluatorResult resultVal;

	public Node(Type typ) {
		type = typ;
	}

	public Node(Type typ, Token tokenVl) {
		this(typ);

		tokenVal = tokenVl;
	}

	public Node(Type typ, Token.Type opType) {
		this(typ);

		operatorType = opType;
	}

	public Node(Type typ, GroupType grupType) {
		this(typ);

		groupType = grupType;
	}

	public Node(Type typ, EvaluatorResult res) {
		this(typ);

		resultVal = res;
	}

	public String toString() {
		switch(type) {
			case UNARYOP:
			case BINOP:
				return "(" + type.name() + " : " + operatorType + ")";
			case OGROUP:
			case TOKREF:
				return "(" + type.name() + " : " + tokenVal     + ")";
			case GROUP:
				return "(" + type.name() + " : " + groupType    + ")";
			case RESULT:
				return "(" + type.name() + " : " + resultVal    + ")";
			default:
				return "Unknown node type " + type;
		}
	}

	public boolean equals(Object other) {
		if(!(other instanceof Node)) return false;

		Node otk = (Node)other;

		if(otk.type != type) return false;

		switch(type) {
			case OGROUP:
				return tokenVal.equals(otk.tokenVal);
			default:
				return true;
		}
	}
}