-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnv.py
30 lines (23 loc) · 991 Bytes
/
Env.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
""" In scheme, "environment" can be viewed as a lookup table where
key is the variable name and value the variable's content.
The outermost environment is call "global" scope; with each function
call new yet smaller scope environment will be created, and possibly
override the outer environment. """
class Env(dict):
def __init__(self, parent = None):
self.parent = parent
def spawn(self, params, args):
""" returns a new env with current env as the parent """
env = Env(self)
for index in xrange(len(params)):
env[params[index].val] = args[index]
return env
def __getitem__(self, key):
if dict.__contains__(self, key):
return dict.__getitem__(self, key)
if self.parent != None:
return self.parent[key]
return None
def __contains__(self, key):
return dict.__contains__(self, key) or (
self.parent != None and key in self.parent)