-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymbolTable.cs
131 lines (123 loc) · 4.03 KB
/
SymbolTable.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
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
using System.Collections.Generic;
namespace JackCompiler
{
class SymbolTable
{
Dictionary<string, Symbol> classSymbols = new Dictionary<string, Symbol>();
Dictionary<string, Symbol> subroutineSymbols;
Dictionary<SymbolKind, int> indices = new Dictionary<SymbolKind, int>()
{
{ SymbolKind.ARG, 0 },
{ SymbolKind.VAR, 0 },
{ SymbolKind.STATIC, 0 },
{ SymbolKind.FIELD, 0 }
};
/// <summary>
/// Starts a new subroutine scope.
/// </summary>
public void StartSubroutine()
{
subroutineSymbols = new Dictionary<string, Symbol>();
indices[SymbolKind.ARG] = 0;
indices[SymbolKind.VAR] = 0;
}
/// <summary>
/// Defines a new identifier of the given name, type and kind,
/// and assigns it a running index. STATIC and FIELD identifiers
/// have a class scope, while ARG and VAR identifiers have a
/// subroutine scope.
/// </summary>
public void Define(string name, string type, SymbolKind kind)
{
if(kind == SymbolKind.STATIC || kind == SymbolKind.FIELD)
{
int index = indices[kind];
classSymbols.Add(name, new Symbol(type, kind, index));
indices[kind] = index + 1;
}
else if(kind == SymbolKind.ARG || kind == SymbolKind.VAR)
{
int index = indices[kind];
subroutineSymbols.Add(name, new Symbol(type, kind, index));
indices[kind] = index + 1;
}
}
/// <summary>
/// Returns the number of variables of the given kind already
/// defined in the current scope.
/// </summary>
public int VarCount(SymbolKind kind)
{
return indices[kind];
}
/// <summary>
/// Returns the kind of the named identifier in the current scope.
/// If the identifier is unknown in the current scope return NONE.
/// </summary>
public SymbolKind KindOf(string name)
{
Symbol symbol;
if(classSymbols.TryGetValue(name, out symbol))
{
return symbol.Kind;
}
else if(subroutineSymbols.TryGetValue(name, out symbol))
{
return symbol.Kind;
}
return SymbolKind.NONE;
}
/// <summary>
/// Returns the segment of the named identifier.
/// </summary>
public Segment SegmentOf(string name)
{
SymbolKind kind = KindOf(name);
switch(kind)
{
case SymbolKind.VAR:
return Segment.LOCAL;
case SymbolKind.ARG:
return Segment.ARG;
case SymbolKind.FIELD:
return Segment.THIS;
case SymbolKind.STATIC:
return Segment.STATIC;
default:
return Segment.ARG;
}
}
/// <summary>
/// Returns the type of the named identifier in the current scope.
/// </summary>
public string TypeOf(string name)
{
Symbol symbol;
if(classSymbols.TryGetValue(name, out symbol))
{
return symbol.Type;
}
else if(subroutineSymbols.TryGetValue(name, out symbol))
{
return symbol.Type;
}
return null;
}
/// <summary>
/// Returns the index assigned to the named identifier.
/// </summary>
public int IndexOf(string name)
{
Symbol symbol;
if(classSymbols.TryGetValue(name, out symbol))
{
return symbol.Index;
}
else if(subroutineSymbols.TryGetValue(name, out symbol))
{
return symbol.Index;
}
return -1;
}
}
}