3 import mal_types
as types
10 return reader
.read_str(str)
14 return types
._sequential
_Q
(x
) and len(x
) > 0
18 return types
._list
(types
._symbol
("quote"),
20 elif ast
[0] == 'unquote':
22 elif is_pair(ast
[0]) and ast
[0][0] == 'splice-unquote':
23 return types
._list
(types
._symbol
("concat"),
27 return types
._list
(types
._symbol
("cons"),
31 def is_macro_call(ast
, env
):
32 return (types
._list
_Q
(ast
) and
33 types
._symbol
_Q
(ast
[0]) and
35 hasattr(env
.get(ast
[0]), '_ismacro_'))
37 def macroexpand(ast
, env
):
38 while is_macro_call(ast
, env
):
40 ast
= macroexpand(mac(*ast
[1:]), env
)
43 def eval_ast(ast
, env
):
44 if types
._symbol
_Q
(ast
):
46 elif types
._list
_Q
(ast
):
47 return types
._list
(*map(lambda a
: EVAL(a
, env
), ast
))
48 elif types
._vector
_Q
(ast
):
49 return types
._vector
(*map(lambda a
: EVAL(a
, env
), ast
))
50 elif types
._hash
_map
_Q
(ast
):
53 keyvals
.append(EVAL(k
, env
))
54 keyvals
.append(EVAL(ast
[k
], env
))
55 return types
._hash
_map
(*keyvals
)
57 return ast
# primitive value, return unchanged
61 #print("EVAL %s" % printer._pr_str(ast))
62 if not types
._list
_Q
(ast
):
63 return eval_ast(ast
, env
)
66 ast
= macroexpand(ast
, env
)
67 if not types
._list
_Q
(ast
):
68 return eval_ast(ast
, env
)
69 if len(ast
) == 0: return ast
73 a1
, a2
= ast
[1], ast
[2]
75 return env
.set(a1
, res
)
77 a1
, a2
= ast
[1], ast
[2]
79 for i
in range(0, len(a1
), 2):
80 let_env
.set(a1
[i
], EVAL(a1
[i
+1], let_env
))
86 elif "quasiquote" == a0
:
87 ast
= quasiquote(ast
[1]);
89 elif 'defmacro!' == a0
:
90 func
= EVAL(ast
[2], env
)
92 return env
.set(ast
[1], func
)
93 elif 'macroexpand' == a0
:
94 return macroexpand(ast
[1], env
)
96 if sys
.version_info
[0] >= 3:
97 exec(compile(ast
[1], '', 'single'), globals())
99 exec(compile(ast
[1], '', 'single') in globals())
104 el
= eval_ast(ast
[2:], env
)
108 a1
, a2
= ast
[1], ast
[2]
109 if a2
[0] == "catch*":
111 return EVAL(a1
, env
);
112 except Exception as exc
:
114 catch_env
= Env(env
, [a2
[1]], [exc
])
115 return EVAL(a2
[2], catch_env
)
117 return EVAL(a1
, env
);
119 eval_ast(ast
[1:-1], env
)
121 # Continue loop (TCO)
123 a1
, a2
= ast
[1], ast
[2]
125 if cond
is None or cond
is False:
126 if len(ast
) > 3: ast
= ast
[3]
130 # Continue loop (TCO)
132 a1
, a2
= ast
[1], ast
[2]
133 return types
._function
(EVAL
, Env
, a2
, env
, a1
)
135 el
= eval_ast(ast
, env
)
137 if hasattr(f
, '__ast__'):
139 env
= f
.__gen
_env
__(el
[1:])
145 return printer
._pr
_str
(exp
)
150 return PRINT(EVAL(READ(str), repl_env
))
152 # core.py: defined using python
153 for k
, v
in core
.ns
.items(): repl_env
.set(types
._symbol
(k
), v
)
154 repl_env
.set(types
._symbol
('eval'), lambda ast
: EVAL(ast
, repl_env
))
155 repl_env
.set(types
._symbol
('*ARGV*'), types
._list
(*sys
.argv
[2:]))
157 # core.mal: defined using the language itself
158 REP("(def! *host-language* \"python\")")
159 REP("(def! not (fn* (a) (if a false true)))")
160 REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
161 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)))))))")
162 REP("(def! *gensym-counter* (atom 0))")
163 REP("(def! gensym (fn* [] (symbol (str \"G__\" (swap! *gensym-counter* (fn* [x] (+ 1 x)))))))")
164 REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))")
166 if len(sys
.argv
) >= 2:
167 REP('(load-file "' + sys
.argv
[1] + '")')
171 REP("(println (str \"Mal [\" *host-language* \"]\"))")
174 line
= mal_readline
.readline("user> ")
175 if line
== None: break
176 if line
== "": continue
178 except reader
.Blank
: continue
179 except Exception as e
:
180 print("".join(traceback
.format_exception(*sys
.exc_info())))