aboutsummaryrefslogtreecommitdiff
path: root/python/stepA_more.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/stepA_more.py')
-rw-r--r--python/stepA_more.py63
1 files changed, 34 insertions, 29 deletions
diff --git a/python/stepA_more.py b/python/stepA_more.py
index 7c6a5da..723f0ed 100644
--- a/python/stepA_more.py
+++ b/python/stepA_more.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)
@@ -77,11 +77,14 @@ def EVAL(ast, env):
let_env = Env(env)
for i in range(0, len(a1), 2):
let_env.set(a1[i], EVAL(a1[i+1], let_env))
- return EVAL(a2, let_env)
+ ast = a2
+ env = let_env
+ # Continue loop (TCO)
elif "quote" == a0:
return ast[1]
elif "quasiquote" == a0:
- return EVAL(quasiquote(ast[1]), env)
+ ast = quasiquote(ast[1]);
+ # Continue loop (TCO)
elif 'defmacro!' == a0:
func = EVAL(ast[2], env)
func._ismacro_ = True
@@ -89,7 +92,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 +109,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:
@@ -127,10 +133,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:])
@@ -142,31 +147,31 @@ def PRINT(exp):
repl_env = Env()
def REP(str):
return PRINT(EVAL(READ(str), repl_env))
-def _ref(k,v): repl_env.set(k, v)
-
-# Import types functions
-for name, val in core.ns.items(): _ref(name, val)
-_ref('readline', lambda prompt: mal_readline.readline(prompt))
-_ref('read-string', reader.read_str)
-_ref('eval', lambda ast: EVAL(ast, repl_env))
-_ref('slurp', lambda file: open(file).read())
+# 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:]))
-# Defined using the language itself
+# 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)))))))")
REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))")
-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
+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())))