-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTable.py
More file actions
51 lines (41 loc) · 1.24 KB
/
SymbolTable.py
File metadata and controls
51 lines (41 loc) · 1.24 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
43
44
45
46
47
48
49
50
51
class SymbolTable(dict):
"""
TO DO
"""
def __init__(self, scope):
if scope == 'class':
self.kind_count = {'STATIC': 0, 'FIELD': 0}
elif scope == 'subroutine':
self.kind_count = {'ARG': 0, 'VAR': 0}
self.symbols = {}
def define(self, name, type, kind):
kind_idx = self.kind_count[kind]
self.symbols[name] = (type, kind, kind_idx)
self.kind_count[kind] += 1
# To Do - handle errors
def type_of(self, name):
return self.symbols[name][0]
def kind_of(self, name):
return self.symbols[name][1]
def index_of(self, name):
return self.symbols[name][2]
def kind_count_of(self, kind):
return self.kind_count[kind]
# TEST
if __name__ == '__main__':
st_class = SymbolTable('class')
st_class.define("Point", "int", "STATIC")
print(st_class.kind_count)
print(st_class.symbols)
print(st_class.kind_count)
print(st_class.type_of('Point'))
print(st_class.kind_of('Point'))
print(st_class.index_of('Point'))
print(st_class.symbols.get("asdf"))
print(st_class.symbols.get("Point"))
print()
st_subroutine = SymbolTable('subroutine')
st_subroutine.define("Thing", "char", "VAR")
print(st_subroutine.kind_count)
print(st_subroutine.symbols)
print(st_subroutine.kind_count)