-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTable.py
More file actions
45 lines (33 loc) · 1.19 KB
/
Copy pathSymbolTable.py
File metadata and controls
45 lines (33 loc) · 1.19 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
#!/usr/bin/python
from errors import UndeclaredVariableError
class VariableSymbol:
def __init__(self, name, type):
self.name = name
self.type = type # MatrixType, IntType, FloatType, String
class Scope:
def __init__(self, parent, level):
self.parent = parent
self.level = level
self.symbols = dict()
def put(self, name, type):
self.symbols[name] = VariableSymbol(name, type)
def get(self, name):
if name in self.symbols:
return self.symbols[name]
if self.parent == None:
raise UndeclaredVariableError()
return self.parent.get(name)
def get_parent(self):
return self.parent
class SymbolTable:
def __init__(self):
self.global_scope = Scope(None, 0)
self.current_scope = self.global_scope
def push_scope(self):
self.current_scope = Scope(self.current_scope, self.current_scope.level+1)
def pop_scope(self):
self.current_scope = self.current_scope.get_parent()
def get(self, name):
return self.current_scope.get(name)
def put(self, name, type):
self.current_scope.put(name, type)