-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironment.py
More file actions
26 lines (21 loc) · 810 Bytes
/
Environment.py
File metadata and controls
26 lines (21 loc) · 810 Bytes
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
from typing import Dict, Any
class Environment:
def __init__(self, enclosing=None) -> None:
self.values: Dict[str, Any] = dict()
self.enclosing = enclosing
def define(self, name, value: object):
self.values[name.lexeme] = value
def get(self, name):
if name.lexeme in self.values:
return self.values[name.lexeme]
if self.enclosing:
return self.enclosing.get(name)
raise RuntimeError(name, f"Undefined Variable {name.lexeme} .")
def assign(self, name, value):
if name.lexeme in self.values:
self.values[name.lexeme] = value
return
if self.enclosing:
self.enclosing.assign(name,value)
return
raise Exception(f"Undefined variable {name.lexeme}.")