-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvironment.py
41 lines (31 loc) · 1.23 KB
/
Environment.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
from JesseRuntimeError import JesseRuntimeError
from Token import Token
class Environment():
def __init__(self,enclosing=None):
self.values = {}
self.enclosing = enclosing
def get(self,name:Token):
if(name.lexeme in self.values):
return self.values[name.lexeme]
if self.enclosing is not None:
return self.enclosing.get(name)
raise JesseRuntimeError(name.pos,"i don't remember defining this yo")
def assign(self,name:Token,value:object):
if(name.lexeme in self.values):
self.values[name.lexeme] = value
return
if self.enclosing is not None:
self.enclosing.assign(name,value)
return
raise JesseRuntimeError(name.pos,"i don't remember defining this yo")
def define(self,name:str,value:object):
self.values[name] = value
def ancestor(self,distance:int):
env = self
for i in range(distance):
env = env.enclosing
return env
def get_at(self,distance:int,name:str):
return self.ancestor(distance).values[name]
def assign_at(self,distance:int,name:Token,value:object):
self.ancestor(distance).values[name.lexeme] = value