diff options
| author | Joel Martin <github@martintribe.org> | 2014-04-09 23:58:27 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-04-09 23:58:27 -0500 |
| commit | a34b02006527d28db5df5c6553be804770b81f79 (patch) | |
| tree | 526fe400e5a8c34b32a3d93439197db09c7f84ad /python | |
| parent | edc3b0640f1b773e185550448e2086279f7b1a7f (diff) | |
| download | mal-a34b02006527d28db5df5c6553be804770b81f79.tar.gz mal-a34b02006527d28db5df5c6553be804770b81f79.zip | |
Fix metadata on functions.
- Don't use metadata to store ast, env, params data.
- In Clojure, store metadata on the :meta key of the real metadata.
This also allows using any datatype as metadata.
Diffstat (limited to 'python')
| -rw-r--r-- | python/core.py | 5 | ||||
| -rw-r--r-- | python/mal_types.py | 15 | ||||
| -rw-r--r-- | python/step5_tco.py | 7 | ||||
| -rw-r--r-- | python/step6_file.py | 7 | ||||
| -rw-r--r-- | python/step7_quote.py | 7 | ||||
| -rw-r--r-- | python/step8_macros.py | 7 | ||||
| -rw-r--r-- | python/step9_interop.py | 7 | ||||
| -rw-r--r-- | python/stepA_more.py | 7 |
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:]) |
