diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/core.py | 11 | ||||
| -rw-r--r-- | python/mal_readline.py | 8 | ||||
| -rw-r--r-- | python/mal_types.py | 38 | ||||
| -rw-r--r-- | python/printer.py | 2 | ||||
| -rw-r--r-- | python/step1_read_print.py | 2 | ||||
| -rw-r--r-- | python/step2_eval.py | 4 | ||||
| -rw-r--r-- | python/step3_env.py | 4 | ||||
| -rw-r--r-- | python/step4_if_fn_do.py | 2 | ||||
| -rw-r--r-- | python/step5_tco.py | 2 | ||||
| -rw-r--r-- | python/step6_file.py | 2 | ||||
| -rw-r--r-- | python/step7_quote.py | 2 | ||||
| -rw-r--r-- | python/step8_macros.py | 2 | ||||
| -rw-r--r-- | python/step9_interop.py | 2 | ||||
| -rw-r--r-- | python/stepA_more.py | 9 |
14 files changed, 56 insertions, 34 deletions
diff --git a/python/core.py b/python/core.py index 8b01b69..20ac793 100644 --- a/python/core.py +++ b/python/core.py @@ -17,12 +17,12 @@ def do_str(*args): return "".join(map(lambda exp: printer._pr_str(exp, False), args)) def prn(*args): - print " ".join(map(lambda exp: printer._pr_str(exp, True), args)) + print(" ".join(map(lambda exp: printer._pr_str(exp, True), args))) return None def println(*args): line = " ".join(map(lambda exp: printer._pr_str(exp, False), args)) - print line.replace('\\n', '\n') + print(line.replace('\\n', '\n')) return None @@ -85,10 +85,7 @@ def mapf(f, lst): return List(map(f, lst)) # Metadata functions def with_meta(obj, meta): - if type(obj) == type(lambda x:x): - new_obj = obj.__copy__() - else: - new_obj = copy.copy(obj) + new_obj = types._clone(obj) new_obj.__meta__ = meta return new_obj @@ -126,7 +123,7 @@ ns = { '+': lambda a,b: a+b, '-': lambda a,b: a-b, '*': lambda a,b: a*b, - '/': lambda a,b: a/b, + '/': lambda a,b: int(a/b), 'list': types._list, 'list?': types._list_Q, diff --git a/python/mal_readline.py b/python/mal_readline.py index e8cf957..2add6b5 100644 --- a/python/mal_readline.py +++ b/python/mal_readline.py @@ -1,7 +1,11 @@ -import os, readline as pyreadline +import os, sys, readline as pyreadline history_loaded = False histfile = os.path.expanduser("~/.mal-history") +if sys.version_info[0] >= 3: + rl = input +else: + rl = raw_input def readline(prompt="user> "): if not history_loaded: @@ -15,7 +19,7 @@ def readline(prompt="user> "): pass try: - line = raw_input(prompt) + line = rl(prompt) pyreadline.add_history(line) with open(histfile, "a") as hf: hf.write(line + "\n") diff --git a/python/mal_types.py b/python/mal_types.py index 74409c9..e6bd70d 100644 --- a/python/mal_types.py +++ b/python/mal_types.py @@ -1,3 +1,9 @@ +import sys, copy, types as pytypes +if sys.version_info[0] >= 3: + str_types = [str] +else: + str_types = [str, unicode] + # General functions def _equal_Q(a, b): @@ -26,11 +32,26 @@ def _equal_Q(a, b): def _sequential_Q(seq): return _list_Q(seq) or _vector_Q(seq) +def _clone(obj): + #if type(obj) == type(lambda x:x): + if type(obj) == pytypes.FunctionType: + if obj.__code__: + return pytypes.FunctionType( + obj.__code__, obj.__globals__, name = obj.__name__, + argdefs = obj.__defaults__, closure = obj.__closure__) + else: + return pytypes.FunctionType( + obj.func_code, obj.func_globals, name = obj.func_name, + argdefs = obj.func_defaults, closure = obj.func_closure) + else: + return copy.copy(obj) + + # Scalars def _nil_Q(exp): return exp is None def _true_Q(exp): return exp is True def _false_Q(exp): return exp is False -def _string_Q(exp): return type(exp) in [str, unicode] +def _string_Q(exp): return type(exp) in str_types # Symbols class Symbol(str): pass @@ -39,15 +60,12 @@ def _symbol_Q(exp): return type(exp) == Symbol # Functions 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 fn(*args): + return Eval(ast, Env(env, params, args)) + fn.__meta__ = None + fn.__ast__ = ast + fn.__gen_env__ = lambda args: Env(env, params, args) + return fn def _function_Q(f): return type(f) == type(function_Q) # lists diff --git a/python/printer.py b/python/printer.py index d501d60..65bf256 100644 --- a/python/printer.py +++ b/python/printer.py @@ -13,7 +13,7 @@ def _pr_str(obj, print_readably=True): return "{" + " ".join(ret) + "}" elif types._string_Q(obj): if print_readably: - return '"' + obj.encode('unicode_escape').replace('"', '\\"') + '"' + return '"' + obj.encode('unicode_escape').decode('latin1').replace('"', '\\"') + '"' else: return obj elif types._nil_Q(obj): diff --git a/python/step1_read_print.py b/python/step1_read_print.py index 0315cf0..7c5b7d8 100644 --- a/python/step1_read_print.py +++ b/python/step1_read_print.py @@ -27,4 +27,4 @@ while True: print(REP(line)) except reader.Blank: continue except Exception as e: - print "".join(traceback.format_exception(*sys.exc_info())) + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/step2_eval.py b/python/step2_eval.py index e50d231..a5061dc 100644 --- a/python/step2_eval.py +++ b/python/step2_eval.py @@ -48,7 +48,7 @@ def REP(str): repl_env['+'] = lambda a,b: a+b repl_env['-'] = lambda a,b: a-b repl_env['*'] = lambda a,b: a*b -repl_env['/'] = lambda a,b: a/b +repl_env['/'] = lambda a,b: int(a/b) while True: try: @@ -58,4 +58,4 @@ while True: print(REP(line)) except reader.Blank: continue except Exception as e: - print "".join(traceback.format_exception(*sys.exc_info())) + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/step3_env.py b/python/step3_env.py index 21879d6..f684274 100644 --- a/python/step3_env.py +++ b/python/step3_env.py @@ -62,7 +62,7 @@ def _ref(k,v): repl_env.set(k, v) _ref('+', lambda a,b: a+b) _ref('-', lambda a,b: a-b) _ref('*', lambda a,b: a*b) -_ref('/', lambda a,b: a/b) +_ref('/', lambda a,b: int(a/b)) while True: try: @@ -72,4 +72,4 @@ while True: print(REP(line)) except reader.Blank: continue except Exception as e: - print "".join(traceback.format_exception(*sys.exc_info())) + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/step4_if_fn_do.py b/python/step4_if_fn_do.py index d0a7fc3..d9910f6 100644 --- a/python/step4_if_fn_do.py +++ b/python/step4_if_fn_do.py @@ -88,4 +88,4 @@ while True: print(REP(line)) except reader.Blank: continue except Exception as e: - print "".join(traceback.format_exception(*sys.exc_info())) + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/step5_tco.py b/python/step5_tco.py index d9d34bc..633cbd8 100644 --- a/python/step5_tco.py +++ b/python/step5_tco.py @@ -95,4 +95,4 @@ while True: print(REP(line)) except reader.Blank: continue except Exception as e: - print "".join(traceback.format_exception(*sys.exc_info())) + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/step6_file.py b/python/step6_file.py index f3847d2..1a992cc 100644 --- a/python/step6_file.py +++ b/python/step6_file.py @@ -103,4 +103,4 @@ else: print(REP(line)) except reader.Blank: continue except Exception as e: - print "".join(traceback.format_exception(*sys.exc_info())) + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/step7_quote.py b/python/step7_quote.py index f57dc0a..cae3c99 100644 --- a/python/step7_quote.py +++ b/python/step7_quote.py @@ -125,4 +125,4 @@ else: print(REP(line)) except reader.Blank: continue except Exception as e: - print "".join(traceback.format_exception(*sys.exc_info())) + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/step8_macros.py b/python/step8_macros.py index 5d9151a..adb4546 100644 --- a/python/step8_macros.py +++ b/python/step8_macros.py @@ -145,4 +145,4 @@ else: print(REP(line)) except reader.Blank: continue except Exception as e: - print "".join(traceback.format_exception(*sys.exc_info())) + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/step9_interop.py b/python/step9_interop.py index 521a3c2..1d8e55a 100644 --- a/python/step9_interop.py +++ b/python/step9_interop.py @@ -154,4 +154,4 @@ else: print(REP(line)) except reader.Blank: continue except Exception as e: - print "".join(traceback.format_exception(*sys.exc_info())) + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/stepA_more.py b/python/stepA_more.py index 21c1641..dccce8e 100644 --- a/python/stepA_more.py +++ b/python/stepA_more.py @@ -89,7 +89,10 @@ def EVAL(ast, env): elif 'macroexpand' == a0: return macroexpand(ast[1], env) elif "py!*" == a0: - exec compile(ast[1], '', 'single') in globals() + if sys.version_info[0] >= 3: + exec(compile(ast[1], '', 'single'), globals()) + else: + exec(compile(ast[1], '', 'single') in globals()) return None elif "py*" == a0: return eval(ast[1]) @@ -103,7 +106,7 @@ def EVAL(ast, env): try: return EVAL(a1, env); except Exception as exc: - exc = exc.message + exc = exc.args[0] catch_env = Env(env, [a2[1]], [exc]) return EVAL(a2[2], catch_env) else: @@ -168,4 +171,4 @@ else: print(REP(line)) except reader.Blank: continue except Exception as e: - print "".join(traceback.format_exception(*sys.exc_info())) + print("".join(traceback.format_exception(*sys.exc_info()))) |
