diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/Makefile | 2 | ||||
| -rw-r--r-- | python/step9_try.py (renamed from python/step9_interop.py) | 19 | ||||
| -rw-r--r-- | python/stepA_interop.py (renamed from python/stepA_more.py) | 0 |
3 files changed, 14 insertions, 7 deletions
diff --git a/python/Makefile b/python/Makefile index b461db3..6e51430 100644 --- a/python/Makefile +++ b/python/Makefile @@ -3,7 +3,7 @@ TESTS = SOURCES_BASE = mal_readline.py mal_types.py reader.py printer.py -SOURCES_LISP = env.py core.py stepA_more.py +SOURCES_LISP = env.py core.py stepA_interop.py SOURCES = $(SOURCES_BASE) $(SOURCES_LISP) diff --git a/python/step9_interop.py b/python/step9_try.py index 7cacf1f..65da08c 100644 --- a/python/step9_interop.py +++ b/python/step9_try.py @@ -97,12 +97,17 @@ def EVAL(ast, env): else: exec(compile(ast[1], '', 'single') in globals()) return None - elif "py*" == a0: - return eval(ast[1]) - elif "." == a0: - el = eval_ast(ast[2:], env) - f = eval(ast[1]) - return f(*el) + elif "try*" == a0: + a1, a2 = ast[1], ast[2] + if a2[0] == "catch*": + try: + return EVAL(a1, env); + except Exception as exc: + exc = exc.args[0] + catch_env = Env(env, [a2[1]], [exc]) + return EVAL(a2[2], catch_env) + else: + return EVAL(a1, env); elif "do" == a0: eval_ast(ast[1:-1], env) ast = ast[-1] @@ -143,6 +148,7 @@ repl_env.set('eval', lambda ast: EVAL(ast, repl_env)) repl_env.set('*ARGV*', types._list(*sys.argv[2:])) # core.mal: defined using the language itself +REP("(def! *host-language* \"python\")") REP("(def! not (fn* (a) (if a false true)))") REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))") REP("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))") @@ -153,6 +159,7 @@ if len(sys.argv) >= 2: sys.exit(0) # repl loop +REP("(println (str \"Mal [\" *host-language* \"]\"))") while True: try: line = mal_readline.readline("user> ") diff --git a/python/stepA_more.py b/python/stepA_interop.py index 723f0ed..723f0ed 100644 --- a/python/stepA_more.py +++ b/python/stepA_interop.py |
