aboutsummaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/step0_repl.py3
-rw-r--r--python/step1_read_print.py3
-rw-r--r--python/step2_eval.py3
-rw-r--r--python/step3_env.py3
-rw-r--r--python/step4_if_fn_do.py3
-rw-r--r--python/step5_tco.py3
-rw-r--r--python/step6_file.py25
-rw-r--r--python/step7_quote.py25
-rw-r--r--python/step8_macros.py25
-rw-r--r--python/step9_interop.py30
-rw-r--r--python/stepA_more.py24
11 files changed, 86 insertions, 61 deletions
diff --git a/python/step0_repl.py b/python/step0_repl.py
index 8d42c33..bb4d6bf 100644
--- a/python/step0_repl.py
+++ b/python/step0_repl.py
@@ -22,6 +22,7 @@ def PRINT(exp):
def REP(str):
return PRINT(EVAL(READ(str), {}))
+# repl loop
while True:
try:
line = mal_readline.readline("user> ")
@@ -29,4 +30,4 @@ while True:
if line == "": continue
print(REP(line))
except Exception as e:
- print "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
+ print("".join(traceback.format_exception(*sys.exc_info())))
diff --git a/python/step1_read_print.py b/python/step1_read_print.py
index 7c5b7d8..3c2e4ac 100644
--- a/python/step1_read_print.py
+++ b/python/step1_read_print.py
@@ -9,7 +9,7 @@ def READ(str):
# eval
def EVAL(ast, env):
- #print("EVAL %s" % ast)
+ #print("EVAL %s" % printer._pr_str(ast))
return ast
def PRINT(exp):
@@ -19,6 +19,7 @@ def PRINT(exp):
def REP(str):
return PRINT(EVAL(READ(str), {}))
+# repl loop
while True:
try:
line = mal_readline.readline("user> ")
diff --git a/python/step2_eval.py b/python/step2_eval.py
index a5061dc..8af09b1 100644
--- a/python/step2_eval.py
+++ b/python/step2_eval.py
@@ -28,7 +28,7 @@ def eval_ast(ast, env):
return ast # primitive value, return unchanged
def EVAL(ast, env):
- #print("EVAL %s" % ast)
+ #print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)
@@ -50,6 +50,7 @@ repl_env['-'] = lambda a,b: a-b
repl_env['*'] = lambda a,b: a*b
repl_env['/'] = lambda a,b: int(a/b)
+# repl loop
while True:
try:
line = mal_readline.readline("user> ")
diff --git a/python/step3_env.py b/python/step3_env.py
index 372adc4..4565011 100644
--- a/python/step3_env.py
+++ b/python/step3_env.py
@@ -26,7 +26,7 @@ def eval_ast(ast, env):
return ast # primitive value, return unchanged
def EVAL(ast, env):
- #print("EVAL %s" % ast)
+ #print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)
@@ -63,6 +63,7 @@ repl_env.set('-', lambda a,b: a-b)
repl_env.set('*', lambda a,b: a*b)
repl_env.set('/', lambda a,b: int(a/b))
+# repl loop
while True:
try:
line = mal_readline.readline("user> ")
diff --git a/python/step4_if_fn_do.py b/python/step4_if_fn_do.py
index a06d10b..e99cfcf 100644
--- a/python/step4_if_fn_do.py
+++ b/python/step4_if_fn_do.py
@@ -27,7 +27,7 @@ def eval_ast(ast, env):
return ast # primitive value, return unchanged
def EVAL(ast, env):
- #print("EVAL %s" % ast)
+ #print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)
@@ -79,6 +79,7 @@ for k, v in core.ns.items(): repl_env.set(k, v)
# core.mal: defined using the language itself
REP("(def! not (fn* (a) (if a false true)))")
+# repl loop
while True:
try:
line = mal_readline.readline("user> ")
diff --git a/python/step5_tco.py b/python/step5_tco.py
index 7982073..72457e9 100644
--- a/python/step5_tco.py
+++ b/python/step5_tco.py
@@ -28,7 +28,7 @@ def eval_ast(ast, env):
def EVAL(ast, env):
while True:
- #print("EVAL %s" % ast)
+ #print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)
@@ -86,6 +86,7 @@ for k, v in core.ns.items(): repl_env.set(k, v)
# core.mal: defined using the language itself
REP("(def! not (fn* (a) (if a false true)))")
+# repl loop
while True:
try:
line = mal_readline.readline("user> ")
diff --git a/python/step6_file.py b/python/step6_file.py
index ba2d355..764eb53 100644
--- a/python/step6_file.py
+++ b/python/step6_file.py
@@ -28,7 +28,7 @@ def eval_ast(ast, env):
def EVAL(ast, env):
while True:
- #print("EVAL %s" % ast)
+ #print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)
@@ -83,6 +83,7 @@ def REP(str):
# core.py: defined using python
for k, v in core.ns.items(): repl_env.set(k, v)
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! not (fn* (a) (if a false true)))")
@@ -90,13 +91,15 @@ REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\"))
if len(sys.argv) >= 2:
REP('(load-file "' + sys.argv[1] + '")')
-else:
- while True:
- try:
- line = mal_readline.readline("user> ")
- if line == None: break
- if line == "": continue
- print(REP(line))
- except reader.Blank: continue
- except Exception as e:
- print("".join(traceback.format_exception(*sys.exc_info())))
+ sys.exit(0)
+
+# repl loop
+while True:
+ try:
+ line = mal_readline.readline("user> ")
+ if line == None: break
+ if line == "": continue
+ print(REP(line))
+ except reader.Blank: continue
+ except Exception as e:
+ print("".join(traceback.format_exception(*sys.exc_info())))
diff --git a/python/step7_quote.py b/python/step7_quote.py
index aefa421..1002613 100644
--- a/python/step7_quote.py
+++ b/python/step7_quote.py
@@ -46,7 +46,7 @@ def eval_ast(ast, env):
def EVAL(ast, env):
while True:
- #print("EVAL %s" % ast)
+ #print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)
@@ -105,6 +105,7 @@ def REP(str):
# core.py: defined using python
for k, v in core.ns.items(): repl_env.set(k, v)
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! not (fn* (a) (if a false true)))")
@@ -112,13 +113,15 @@ REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\"))
if len(sys.argv) >= 2:
REP('(load-file "' + sys.argv[1] + '")')
-else:
- while True:
- try:
- line = mal_readline.readline("user> ")
- if line == None: break
- if line == "": continue
- print(REP(line))
- except reader.Blank: continue
- except Exception as e:
- print("".join(traceback.format_exception(*sys.exc_info())))
+ sys.exit(0)
+
+# repl loop
+while True:
+ try:
+ line = mal_readline.readline("user> ")
+ if line == None: break
+ if line == "": continue
+ print(REP(line))
+ except reader.Blank: continue
+ except Exception as e:
+ print("".join(traceback.format_exception(*sys.exc_info())))
diff --git a/python/step8_macros.py b/python/step8_macros.py
index 90aedf3..28b68fb 100644
--- a/python/step8_macros.py
+++ b/python/step8_macros.py
@@ -58,7 +58,7 @@ def eval_ast(ast, env):
def EVAL(ast, env):
while True:
- #print("EVAL %s" % ast)
+ #print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)
@@ -125,6 +125,7 @@ def REP(str):
# core.py: defined using python
for k, v in core.ns.items(): repl_env.set(k, v)
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! not (fn* (a) (if a false true)))")
@@ -134,13 +135,15 @@ REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first x
if len(sys.argv) >= 2:
REP('(load-file "' + sys.argv[1] + '")')
-else:
- while True:
- try:
- line = mal_readline.readline("user> ")
- if line == None: break
- if line == "": continue
- print(REP(line))
- except reader.Blank: continue
- except Exception as e:
- print("".join(traceback.format_exception(*sys.exc_info())))
+ sys.exit(0)
+
+# repl loop
+while True:
+ try:
+ line = mal_readline.readline("user> ")
+ if line == None: break
+ if line == "": continue
+ print(REP(line))
+ except reader.Blank: continue
+ except Exception as e:
+ print("".join(traceback.format_exception(*sys.exc_info())))
diff --git a/python/step9_interop.py b/python/step9_interop.py
index eae7837..2f7c5e0 100644
--- a/python/step9_interop.py
+++ b/python/step9_interop.py
@@ -58,7 +58,7 @@ def eval_ast(ast, env):
def EVAL(ast, env):
while True:
- #print("EVAL %s" % ast)
+ #print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)
@@ -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])
@@ -134,6 +137,7 @@ def REP(str):
# core.py: defined using python
for k, v in core.ns.items(): repl_env.set(k, v)
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! not (fn* (a) (if a false true)))")
@@ -143,13 +147,15 @@ REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first x
if len(sys.argv) >= 2:
REP('(load-file "' + sys.argv[1] + '")')
-else:
- while True:
- try:
- line = mal_readline.readline("user> ")
- if line == None: break
- if line == "": continue
- print(REP(line))
- except reader.Blank: continue
- except Exception as e:
- print("".join(traceback.format_exception(*sys.exc_info())))
+ sys.exit(0)
+
+# repl loop
+while True:
+ try:
+ line = mal_readline.readline("user> ")
+ if line == None: break
+ if line == "": continue
+ print(REP(line))
+ except reader.Blank: continue
+ except Exception as e:
+ print("".join(traceback.format_exception(*sys.exc_info())))
diff --git a/python/stepA_more.py b/python/stepA_more.py
index e4bc768..b9d8152 100644
--- a/python/stepA_more.py
+++ b/python/stepA_more.py
@@ -148,6 +148,7 @@ def REP(str):
# core.py: defined using python
for k, v in core.ns.items(): repl_env.set(k, v)
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\")")
@@ -158,13 +159,16 @@ REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first x
if len(sys.argv) >= 2:
REP('(load-file "' + sys.argv[1] + '")')
-else:
- while True:
- try:
- line = mal_readline.readline("user> ")
- if line == None: break
- if line == "": continue
- print(REP(line))
- except reader.Blank: continue
- except Exception as e:
- print("".join(traceback.format_exception(*sys.exc_info())))
+ sys.exit(0)
+
+# repl loop
+REP("(println (str \"Mal [\" *host-language* \"]\"))")
+while True:
+ try:
+ line = mal_readline.readline("user> ")
+ if line == None: break
+ if line == "": continue
+ print(REP(line))
+ except reader.Blank: continue
+ except Exception as e:
+ print("".join(traceback.format_exception(*sys.exc_info())))