aboutsummaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/core.py5
-rw-r--r--python/mal_types.py15
-rw-r--r--python/step5_tco.py7
-rw-r--r--python/step6_file.py7
-rw-r--r--python/step7_quote.py7
-rw-r--r--python/step8_macros.py7
-rw-r--r--python/step9_interop.py7
-rw-r--r--python/stepA_more.py7
8 files changed, 32 insertions, 30 deletions
diff --git a/python/core.py b/python/core.py
index 28d9c5e..5e2ef75 100644
--- a/python/core.py
+++ b/python/core.py
@@ -85,7 +85,10 @@ def mapf(f, lst): return List(map(f, lst))
# Metadata functions
def with_meta(obj, meta):
- new_obj = copy.copy(obj)
+ if type(obj) == type(lambda x:x):
+ new_obj = obj.__copy__()
+ else:
+ new_obj = copy.copy(obj)
new_obj.__meta__ = meta
return new_obj
diff --git a/python/mal_types.py b/python/mal_types.py
index 15e4b6b..74409c9 100644
--- a/python/mal_types.py
+++ b/python/mal_types.py
@@ -38,11 +38,16 @@ def _symbol(str): return Symbol(str)
def _symbol_Q(exp): return type(exp) == Symbol
# Functions
-def _function(Eval, Env, exp, env, params):
- def f(*args):
- return Eval(exp, Env(env, params, args))
- f.__meta__ = {"exp": exp, "env": env, "params": params}
- return f
+def _function(Eval, Env, ast, env, params):
+ def gen_fn():
+ def fn(*args):
+ return Eval(ast, Env(env, params, args))
+ fn.__meta__ = None
+ fn.__ast__ = ast
+ fn.__gen_env__ = lambda args: Env(env, params, args)
+ fn.__copy__ = gen_fn
+ return fn
+ return gen_fn()
def _function_Q(f): return type(f) == type(function_Q)
# lists
diff --git a/python/step5_tco.py b/python/step5_tco.py
index 4335a96..d9d34bc 100644
--- a/python/step5_tco.py
+++ b/python/step5_tco.py
@@ -65,10 +65,9 @@ def EVAL(ast, env):
else:
el = eval_ast(ast, env)
f = el[0]
- if hasattr(f, '__meta__') and f.__meta__.has_key("exp"):
- m = f.__meta__
- ast = m['exp']
- env = Env(m['env'], m['params'], el[1:])
+ if hasattr(f, '__ast__'):
+ ast = f.__ast__
+ env = f.__gen_env__(el[1:])
else:
return f(*el[1:])
diff --git a/python/step6_file.py b/python/step6_file.py
index 9e0b8cd..f3847d2 100644
--- a/python/step6_file.py
+++ b/python/step6_file.py
@@ -65,10 +65,9 @@ def EVAL(ast, env):
else:
el = eval_ast(ast, env)
f = el[0]
- if hasattr(f, '__meta__') and f.__meta__.has_key("exp"):
- m = f.__meta__
- ast = m['exp']
- env = Env(m['env'], m['params'], el[1:])
+ if hasattr(f, '__ast__'):
+ ast = f.__ast__
+ env = f.__gen_env__(el[1:])
else:
return f(*el[1:])
diff --git a/python/step7_quote.py b/python/step7_quote.py
index 3ed6965..f57dc0a 100644
--- a/python/step7_quote.py
+++ b/python/step7_quote.py
@@ -87,10 +87,9 @@ def EVAL(ast, env):
else:
el = eval_ast(ast, env)
f = el[0]
- if hasattr(f, '__meta__') and f.__meta__.has_key("exp"):
- m = f.__meta__
- ast = m['exp']
- env = Env(m['env'], m['params'], el[1:])
+ if hasattr(f, '__ast__'):
+ ast = f.__ast__
+ env = f.__gen_env__(el[1:])
else:
return f(*el[1:])
diff --git a/python/step8_macros.py b/python/step8_macros.py
index 6e2bd45..5d9151a 100644
--- a/python/step8_macros.py
+++ b/python/step8_macros.py
@@ -107,10 +107,9 @@ def EVAL(ast, env):
else:
el = eval_ast(ast, env)
f = el[0]
- if hasattr(f, '__meta__') and f.__meta__.has_key("exp"):
- m = f.__meta__
- ast = m['exp']
- env = Env(m['env'], m['params'], el[1:])
+ if hasattr(f, '__ast__'):
+ ast = f.__ast__
+ env = f.__gen_env__(el[1:])
else:
return f(*el[1:])
diff --git a/python/step9_interop.py b/python/step9_interop.py
index 66a8c50..521a3c2 100644
--- a/python/step9_interop.py
+++ b/python/step9_interop.py
@@ -116,10 +116,9 @@ def EVAL(ast, env):
else:
el = eval_ast(ast, env)
f = el[0]
- if hasattr(f, '__meta__') and f.__meta__.has_key("exp"):
- m = f.__meta__
- ast = m['exp']
- env = Env(m['env'], m['params'], el[1:])
+ if hasattr(f, '__ast__'):
+ ast = f.__ast__
+ env = f.__gen_env__(el[1:])
else:
return f(*el[1:])
diff --git a/python/stepA_more.py b/python/stepA_more.py
index 7c6a5da..21c1641 100644
--- a/python/stepA_more.py
+++ b/python/stepA_more.py
@@ -127,10 +127,9 @@ def EVAL(ast, env):
else:
el = eval_ast(ast, env)
f = el[0]
- if hasattr(f, '__meta__') and f.__meta__.has_key("exp"):
- m = f.__meta__
- ast = m['exp']
- env = Env(m['env'], m['params'], el[1:])
+ if hasattr(f, '__ast__'):
+ ast = f.__ast__
+ env = f.__gen_env__(el[1:])
else:
return f(*el[1:])