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

/**
 * Represents a lexical token.
 *
 * @author Ben Culkin
 */
public class Token {
	/*
	 * The state for this token.
	 */
	private final Tokens tks;

	/**
	 * The type of the token.
	 *
	 * Determines which fields have a value.
	 */
	public final TokenType typ;

	/**
	 * The integer value attached to this token.
	 */
	public int intValue;

	/**
	 * The original string this token was part of.
	 */
	public String rawValue;

	/**
	 * Create a new token.
	 *
	 * @param type
	 *                The type of this token.
	 *
	 * @param raw
	 *                The string this token came from.
	 *
	 * @param toks
	 *                The state for this token
	 */
	public Token(final TokenType type, final String raw, final Tokens toks) {
		this.typ = type;

		rawValue = raw;

		tks = toks;
	}

	@Override
	public String toString() {
		String typeStr = typ.toString();
		typeStr += " (" + typ.name() + ")";

		if (typ == TokenType.VREF) {
			typeStr += " (ind. " + intValue;
			typeStr += ", sym. \"" + tks.symbolTable.get(intValue) + "\")";
		}

		return typeStr + " (originally from: " + rawValue + ")";
	}

	/**
	 * Convert this token into the string representation of it.
	 *
	 * @return The string representation of it.
	 */
	public String toExpr() {
		switch (typ) {
		case ADD:
			return "+";

		case SUBTRACT:
			return "-";

		case MULTIPLY:
			return "*";

		case DIVIDE:
			return "/";

		case VREF:
			return tks.symbolTable.get(intValue);

		case OPAREN:
			return "(";

		case CPAREN:
			return ")";

		default:
			return "???";
		}
	}
}