-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTable.cs
More file actions
42 lines (36 loc) · 1.06 KB
/
Copy pathSymbolTable.cs
File metadata and controls
42 lines (36 loc) · 1.06 KB
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
namespace JackCompiler;
class SymbolTable
{
public Dictionary<string, Symbol> Table = [];
// Indexers!
public Symbol this[string i] => Table[i];
public bool TryGetEntry(string name, out Symbol entry)
{
if (Table.TryGetValue(name, out _))
{
entry = Table[name];
return true;
}
entry = new();
return false;
}
public readonly List<int> _indexes = [0,0,0,0,0,0];
public void Reset() {
Table.Clear();
for (int i = 0; i < _indexes.Count; i++)
{
_indexes[i] = 0;
}
}
public void Define(string name, string type, Kind kind)
{
int indexKind = (int)kind;
Symbol symbol = new Symbol(type, kind, _indexes[indexKind]);
_indexes[indexKind]++;
Table.Add(name, symbol);
}
public int VarCount(Kind kind) => _indexes[(int)kind];
public int IndexOf(string name) => Table[name].Index;
public Kind KindOf(string name) => Table[name].Kind;
public string TypeOf(string name) => Table[name].Type;
}