-
Notifications
You must be signed in to change notification settings - Fork 1
/
environment.py
33 lines (27 loc) · 1.14 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
from typing import Dict, Optional
class Environment:
def __init__(self) -> None:
self._store: Dict[str, "monkey_object.MonkeyObject"] = {}
self.outer: Optional[Environment] = None
@staticmethod
def new_enclosed_environment(outer: "Environment") -> "Environment":
env = Environment()
env.outer = outer
return env
def get(self, name: str) -> "Optional[monkey_object.MonkeyObject]":
found = name in self._store
obj = None
if found:
obj = self._store[name]
elif self.outer is not None:
# If current environment doesn't have a value associated with a
# name, we recursively call get on enclosing environment (which the
# current environment is extending) until either name is found or
# caller can issue a "unknown identifier" error.
obj = self.outer.get(name)
return obj
def set(self, name: str, value: "monkey_object.MonkeyObject") -> "monkey_object.MonkeyObject":
self._store[name] = value
return value
# pylint: disable=unused-import, wrong-import-position
import monkey_object