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

import bjc.dicelang.dice.DieExpression;
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, DICESCALAR, DICEFUDGE, DICEGROUP, DICECONCAT, DICELIST, LET, BIND, COERCE, STRCAT, STRREP, 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
	// FLOAT_LIT (float value)
	public double floatValue;

	// This is used for the following token types
	// DICE_LIT (dice value)
	public DieExpression diceValue;

	// This is used for the following token types
	// TOKGROUP (the tokens in the group)
	// TAG* (the tagged construct)
	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, 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 TAGOP:
		case TAGOPR:
		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;
		}
	}
}