summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/expr/Token.java
diff options
context:
space:
mode:
Diffstat (limited to 'dice-lang/src/bjc/dicelang/expr/Token.java')
-rw-r--r--dice-lang/src/bjc/dicelang/expr/Token.java87
1 files changed, 87 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/expr/Token.java b/dice-lang/src/bjc/dicelang/expr/Token.java
new file mode 100644
index 0000000..b02f6c9
--- /dev/null
+++ b/dice-lang/src/bjc/dicelang/expr/Token.java
@@ -0,0 +1,87 @@
+package bjc.dicelang.expr;
+
+/**
+ * Represents a lexical token.
+ *
+ * @author Ben Culkin
+ */
+public class Token {
+ /*
+ * The state for this token.
+ */
+ private Tokens tks;
+
+ /**
+ * The type of the token.
+ *
+ * Determines which fields have a value.
+ */
+ public final TokenType type;
+
+ /**
+ * 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(TokenType type, String raw, Tokens toks) {
+ this.type = type;
+
+ rawValue = raw;
+
+ tks = toks;
+ }
+
+ @Override
+ public String toString() {
+ String typeStr = type.toString();
+ typeStr += " (" + type.name() + ")";
+
+ if(type == 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(type) {
+ 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 "???";
+ }
+ }
+}