forked from jahnvi12/Nand2Tetris-Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymbolTable.py
65 lines (59 loc) · 2.05 KB
/
SymbolTable.py
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
class SymbolTable:
def __init__(self):
self.field_count = 0
self.static_count = 0
self.arg_count = 0
self.var_count = 0
self.subroutine_scope = {}
self.class_scope = {}
def startSubroutine(self):
self.subroutine_scope = {}
self.arg_count = 0
self.var_count = 0
def Define(self, name, type, kind):
if kind == 'static':
self.class_scope[name] = (type, 'static' , self.static_count)
self.static_count += 1
elif kind == 'field':
self.class_scope[name] = (type, 'field' , self.field_count)
self.field_count += 1
elif kind == 'argument':
self.subroutine_scope[name] = (type, 'argument' , self.arg_count)
self.arg_count += 1
elif kind == 'local':
self.subroutine_scope[name] = (type, 'local' , self.var_count)
self.var_count += 1
else:
self.class_scope[name] = (type, kind, 0)
def VarCount(self, kind):
if kind == 'argument':
return self.arg_count
elif kind == 'local':
return self.var_count
elif kind == 'static':
return self.static_count
elif kind == 'field':
return self.field_count
else:
return None
def KindOf(self, name):
if name in self.subroutine_scope.keys():
return self.subroutine_scope[name][1]
elif name in self.class_scope.keys():
return self.class_scope[name][1]
else:
return None
def TypeOf(self, name):
if name in self.subroutine_scope.keys():
return self.subroutine_scope[name][0]
elif name in self.class_scope.keys():
return self.class_scope[name][0]
else:
return None
def IndexOf(self, name):
if name in self.subroutine_scope.keys():
return self.subroutine_scope[name][2]
elif name in self.class_scope.keys():
return self.class_scope[name][2]
else:
return None