-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.py
54 lines (44 loc) · 1.56 KB
/
scope.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
import itertools as itl
from typetree import TypeTree
class Scope:
def __init__(self, parent=None):
self.locals = []
self.parent = parent
self.children = []
self.index_at_parent = 0 if parent is None else len(parent.locals)
self.types = TypeTree()
def define_variable(self, vname):
vinfo = VariableInfo(vname)
self.locals.append(vinfo)
return vinfo
def create_child_scope(self):
child_scope = Scope(self)
self.children.append(child_scope)
return child_scope
def is_defined(self, vname):
return self.get_variable_info(vname) is not None
def get_variable_info(self, vname):
current = self
top = len(self.locals)
while current is not None:
vinfo = Scope.find_variable_info(vname, current, top)
if vinfo is not None:
return vinfo
top = current.index_at_parent
current = current.parent
return None
def is_local(self, vname):
return self.get_local_variable_info(vname) is not None
def get_local_variable_info(self, vname):
return Scope.find_variable_info(vname, self)
@staticmethod
def find_variable_info(vname, scope, top=None):
if top is None:
top = len(scope.locals)
candidates = (vinfo for vinfo in itl.islice(scope.locals, top) if vinfo.name == vname)
return next(candidates, None)
class VariableInfo:
def __init__(self, name):
self.name = name
self.type = None
self.vmholder = None