summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v2/Token.java
blob: e6f22a55bf4d20de03c7e02d40548f5c4b20c7d8 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package bjc.dicelang.v2;

import bjc.utils.funcdata.IList;

/**
 * Lexer token
 */
public class Token {
	public final static Token NIL_TOKEN = new Token(Type.NIL);

	/**
	 * Possible token types
	 */
	public static enum Type {
		// Natural tokens
		// These are produced from lexemes
		ADD,      SUBTRACT,
		MULTIPLY,
		DIVIDE,   IDIVIDE,
		INT_LIT,  FLOAT_LIT, STRING_LIT,
		VREF,
		DICE_LIT, DICEGROUP, DICECONCAT, DICELIST,
		LET,      BIND,
		OPAREN,   CPAREN,
		OBRACKET, CBRACKET,
		OBRACE,   CBRACE,
		// Synthetic tokens
		// These are produced when needed
		NIL,      PRESHUNT,  GROUPSEP,
		TOKGROUP
	}

	public final Type type;

	// At most one of these is valid
	// based on the token type
	public long 				 intValue;
	public double 				 floatValue;
	public String 				 stringValue;
	public DiceBox.DieExpression diceValue;
	public IList<Token>          tokenValues;

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

	public Token(Type typ, long val) {
		this(typ);

		intValue = val;
	}

	public Token(Type typ, double val) {
		this(typ);

		floatValue = val;
	}

	public Token(Type typ, String val) {
		this(typ);

		stringValue = val;
	}

	public Token(Type typ, DiceBox.DieExpression val) {
		this(typ);

		diceValue = val;
	}

	public Token(Type typ, IList<Token> tkVals) {
		this(typ);

		tokenValues = tkVals;
	}

	public String toString() {
		switch(type) {
			case INT_LIT:
			case STRING_LIT:
			case VREF:
			case OPAREN:
			case CPAREN:
			case OBRACKET:
			case CBRACKET:
			case OBRACE:
			case CBRACE:
				return type.toString() + "("
					+ intValue + ")";
			case FLOAT_LIT:
				return type.toString() + "("
					+ floatValue + ")";
			case DICE_LIT:
				return type.toString() + "("
					+ diceValue + ")";
			case TOKGROUP:
				return type.toString() + "("
					+ tokenValues + ")";
			default:
				return type.toString();
		}
	}

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

		Token otk = (Token)other;

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

		switch(type) {
			case OBRACE:
			case OBRACKET:
				return intValue == otk.intValue;
			default:
				return true;
		}
	}

	public boolean isGrouper() {
		switch(type) {
			case OPAREN:
			case OBRACE:
			case OBRACKET:
				return true;
			default:
				return false;
		}
	}
}