1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-19 09:38:28 +03:00

Merge pull request #476 from rectalogic/python.2-env

Fix python.2 Env.find implementation.
This commit is contained in:
Joel Martin 2019-12-12 12:40:33 -06:00 committed by GitHub
commit 0279f85f35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions

View File

@ -12,10 +12,8 @@ class Env(object):
binds: Optional[List[MalExpression]] = None,
exprs: Optional[List[MalExpression]] = None,
) -> None:
if outer:
self._data: Dict[str, MalExpression] = outer._data.copy()
else:
self._data = {}
self._outer = outer
self._data: Dict[str, MalExpression] = {}
if binds is not None and exprs is not None:
for x in range(0, len(binds)):
assert isinstance(binds[x], MalSymbol)
@ -32,6 +30,8 @@ class Env(object):
def find(self, key: MalExpression) -> Optional["Env"]:
if str(key) in self._data:
return self
if self._outer is not None:
return self._outer.find(key)
return None
def get(self, key: MalExpression) -> MalExpression:
@ -49,4 +49,4 @@ class Env(object):
for d in self._data:
env_str += str(d) + ": " + str(self._data[d]) + ", "
env_str += "}"
return "environment: (data: " + env_str + ")"
return f"environment: (data: {env_str} outer: {repr(self._outer) if self._outer is not None else 'None'})"

View File

@ -126,6 +126,11 @@
*ARGV*
;=>()
;;
;; Testing that eval sets aa in root scope, and that it is found in nested scope
(let* (b 12) (do (eval (read-string "(def! aa 7)")) aa ))
;=>7
;>>> soft=True
;>>> optional=True
;;
@ -175,3 +180,4 @@ mymap
;;; Hopefully less problematic characters can be checked together
(read-string "1; &()*+,-./:;<=>?@[]^_{|}~")
;=>1