-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.py
57 lines (40 loc) · 1.58 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
55
56
57
from interpreter.basic_value import BasicValue
class SymbolInfo:
def __init__(self, varname, decltype, value=None, allow_casting=False):
self.varname = varname
self.decltype = decltype
self.value_wrapper = BasicValue(value)
self.allow_casting = allow_casting
class Scope():
def __init__(self, parent=None):
self.variables = {}
self.parent = parent
def declare_variable(self, name, decltype, allow_casting=False):
self.variables[name] = SymbolInfo(name, decltype, allow_casting)
return self.variables[name].value_wrapper
def set_variable(self, name, value):
var = self.find_variable_info(name)
if var != None:
var.value_wrapper.assign_value(value)
def find_variable_info(self, name, limit=False):
# if name != "self":
# print(name)
if name in self.variables:
return self.variables[name]
if limit or self.parent is None:
return None
try:
return self.parent.find_variable_info(name)
except:
# TODO: Maybe throw an error.
return None
def find_variable_value(self, name, limit=False):
return self.find_variable_info(name, limit).value_wrapper
def find_variable_decltype(self, name, limit=False):
return self.find_variable_info(name, limit).decltype
def __str__(self):
return "Scope definitions: {}".format(self.variables)
__repr__ = __str__
class FunctionScope(Scope):
def __init__(self):
Scope.__init__(None)