-
Notifications
You must be signed in to change notification settings - Fork 0
/
Token.cs
67 lines (61 loc) · 2.06 KB
/
Token.cs
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
namespace JackCompiler
{
public enum TokenType { KEYWORD, SYMBOL, IDENTIFIER, INT_CONST, STRING_CONST };
public enum Keyword { CLASS, METHOD, FUNCTION, CONSTRUCTOR, INT, BOOLEAN, CHAR, VOID, VAR,
STATIC, FIELD, LET, DO, IF, ELSE, WHILE, RETURN, TRUE, FALSE, NULL, THIS };
public class Token
{
public TokenType Type { get; private set; }
public Keyword Keyword { get; private set; }
public char Symbol { get; private set; }
public string Identifier { get; private set; }
public string StringVal { get; private set; }
public int IntVal { get; private set; }
public Token(TokenType type, char symbol)
{
this.Type = type;
this.Symbol = symbol;
}
public Token(TokenType type, int intVal)
{
this.Type = type;
this.IntVal = intVal;
}
public Token(TokenType type, Keyword keyword)
{
this.Type = type;
this.Keyword = keyword;
}
public Token(TokenType type, string identifierOrString)
{
if(type == TokenType.IDENTIFIER)
{
this.Type = type;
this.Identifier = identifierOrString;
}
else if(type == TokenType.STRING_CONST)
{
this.Type = TokenType.STRING_CONST;
this.StringVal = identifierOrString;
}
}
public override string ToString()
{
switch(Type)
{
case TokenType.IDENTIFIER:
return "IDENTIFIER: " + Identifier;
case TokenType.INT_CONST:
return "INT: " + IntVal;
case TokenType.KEYWORD:
return "KEYWORD: " + Keyword;
case TokenType.STRING_CONST:
return "STRING: " + StringVal;
case TokenType.SYMBOL:
return "SYMBOL: " + Symbol;
default:
return Identifier;
}
}
}
}