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)
# apply list
ast = macroexpand(ast, env)
- if not types._list_Q(ast): return ast
+ if not types._list_Q(ast):
+ return eval_ast(ast, env)
if len(ast) == 0: return ast
a0 = ast[0]
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
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)
+# core.py: defined using python
+for k, v in core.ns.items(): repl_env.set(types._symbol(k), v)
+repl_env.set(types._symbol('eval'), lambda ast: EVAL(ast, repl_env))
+repl_env.set(types._symbol('*ARGV*'), types._list(*sys.argv[2:]))
-_ref('read-string', reader.read_str)
-_ref('eval', lambda ast: EVAL(ast, repl_env))
-_ref('slurp', lambda file: open(file).read())
-
-# Defined using the language itself
+# core.mal: defined using the language itself
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))))))))")
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())))