mirror of
https://github.com/kanaka/mal.git
synced 2024-11-10 12:47:45 +03:00
ea81a8087b
- types: low-level mapping to the implementation language. - core: functions on types that are exposed directly to mal. - printer: implementation called by pr-str, str, prn, println. - env: the environment implementation - Also, unindent all TCO while loops so that the diff of step4 and step5 are minimized.
29 lines
789 B
Python
29 lines
789 B
Python
# Environment
|
|
|
|
class Env():
|
|
def __init__(self, outer=None, binds=None, exprs=None):
|
|
self.data = {}
|
|
self.outer = outer or None
|
|
|
|
if binds:
|
|
for i in range(len(binds)):
|
|
if binds[i] == "&":
|
|
self.data[binds[i+1]] = exprs[i:]
|
|
break
|
|
else:
|
|
self.data[binds[i]] = exprs[i]
|
|
|
|
def find(self, key):
|
|
if key in self.data: return self
|
|
elif self.outer: return self.outer.find(key)
|
|
else: return None
|
|
|
|
def set(self, key, value):
|
|
self.data[key] = value
|
|
return value
|
|
|
|
def get(self, key):
|
|
env = self.find(key)
|
|
if not env: raise Exception("'" + key + "' not found")
|
|
return env.data[key]
|