summaryrefslogtreecommitdiff
path: root/base/src/bjc/dicelang/tokens/Token.java
blob: 1a11ccdf7ee76ad870730901f771948fda0e872b (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package bjc.dicelang.tokens;

import bjc.utils.funcdata.IList;

/*
 * @TODO 10/09/17 Ben Culkin :TokenReorg
 * 
 * Split the class into subclasses based off of type.
 */
/**
 * Lexer token.
 */
@SuppressWarnings("javadoc")
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.
		 */
		/*
		 * Math tokens.
		 */
		ADD, SUBTRACT, MULTIPLY, DIVIDE, IDIVIDE,

		/*
		 * Literal tokens.
		 */
		INT_LIT, FLOAT_LIT, STRING_LIT, VREF, DICE_LIT,

		/*
		 * Dice operators.
		 */
		DICESCALAR, DICEFUDGE, DICEGROUP, DICECONCAT, DICELIST,

		/*
		 * Expression operators.
		 */
		LET, BIND, COERCE,

		/*
		 * String operators.
		 */
		STRCAT, STRREP,

		/*
		 * Grouping operators.
		 */
		OPAREN, CPAREN, OBRACKET, CBRACKET, OBRACE, CBRACE,

		/*
		 * Synthetic tokens
		 * 
		 * These are produced when needed.
		 */
		NIL, GROUPSEP, TOKGROUP, TAGOP, TAGOPR
	}

	public final Type type;

	/*
	 * This is used for the following token types
	 *
	 * - INT_LIT (int value)
	 * 
	 * - STRING_LIT (index into string table)
	 * 
	 * - VREF (index into sym table)
	 * 
	 * - O* and C* (sym-count of current token)
	 * 
	 */
	public long intValue;

	/*
	 * This is used for the following token types
	 *
	 * - TOKGROUP (the tokens in the group)
	 * 
	 * - TAG* (the tagged construct)
	 * 
	 */
	public IList<Token> tokenValues;

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

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

		intValue = val;
	}

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

		tokenValues = tkVals;
	}

	@Override
	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 TAGOP:
		case TAGOPR:
		case TOKGROUP:
			return type.toString() + "(" + tokenValues + ")";

		default:
			return type.toString();
		}
	}

	@Override
	public boolean equals(final Object other) {
		if(!(other instanceof Token)) {
			return false;
		}

		final 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;
		}
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return super.hashCode();
	}
}