1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-10 12:47:45 +03:00
mal/python/env.py
Joel Martin ea81a8087b All: split types into types, env, printer, core.
- 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.
2014-04-02 22:23:37 -05:00

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]