3 import mal_types
as types
4 from mal_types
import (MalSym
, MalInt
, MalStr
,
5 nil
, true
, false
, _symbol
, _keywordu
,
6 MalList
, _list
, MalVector
, MalHashMap
, MalFunc
)
13 return reader
.read_str(str)
17 return types
._sequential
_Q
(x
) and len(x
) > 0
21 return _list(_symbol(u
"quote"), ast
)
24 if isinstance(a0
, MalSym
):
25 if a0
.value
== u
'unquote':
27 if is_pair(a0
) and isinstance(a0
[0], MalSym
):
29 if (isinstance(a00
, MalSym
) and
30 a00
.value
== u
'splice-unquote'):
31 return _list(_symbol(u
"concat"),
33 quasiquote(ast
.rest()))
34 return _list(_symbol(u
"cons"),
36 quasiquote(ast
.rest()))
38 def is_macro_call(ast
, env
):
39 if types
._list
_Q
(ast
):
41 if isinstance(a0
, MalSym
):
42 if not env
.find(a0
) is None:
43 return env
.get(a0
).ismacro
46 def macroexpand(ast
, env
):
47 while is_macro_call(ast
, env
):
48 assert isinstance(ast
[0], MalSym
)
50 ast
= macroexpand(mac
.apply(ast
.rest()), env
)
53 def eval_ast(ast
, env
):
54 if types
._symbol
_Q
(ast
):
55 assert isinstance(ast
, MalSym
)
57 elif types
._list
_Q
(ast
):
60 res
.append(EVAL(a
, env
))
62 elif types
._vector
_Q
(ast
):
65 res
.append(EVAL(a
, env
))
67 elif types
._hash
_map
_Q
(ast
):
69 for k
in ast
.dct
.keys():
70 new_dct
[k
] = EVAL(ast
.dct
[k
], env
)
71 return MalHashMap(new_dct
)
73 return ast
# primitive value, return unchanged
77 #print("EVAL %s" % printer._pr_str(ast))
78 if not types
._list
_Q
(ast
):
79 return eval_ast(ast
, env
)
82 ast
= macroexpand(ast
, env
)
83 if not types
._list
_Q
(ast
):
84 return eval_ast(ast
, env
)
85 if len(ast
) == 0: return ast
87 if isinstance(a0
, MalSym
):
93 a1
, a2
= ast
[1], ast
[2]
95 return env
.set(a1
, res
)
96 elif u
"let*" == a0sym
:
97 a1
, a2
= ast
[1], ast
[2]
99 for i
in range(0, len(a1
), 2):
100 let_env
.set(a1
[i
], EVAL(a1
[i
+1], let_env
))
102 env
= let_env
# Continue loop (TCO)
103 elif u
"quote" == a0sym
:
105 elif u
"quasiquote" == a0sym
:
106 ast
= quasiquote(ast
[1]) # Continue loop (TCO)
107 elif u
"defmacro!" == a0sym
:
108 func
= EVAL(ast
[2], env
)
110 return env
.set(ast
[1], func
)
111 elif u
"macroexpand" == a0sym
:
112 return macroexpand(ast
[1], env
)
113 elif u
"try*" == a0sym
:
114 a1
, a2
= ast
[1], ast
[2]
116 if isinstance(a20
, MalSym
):
117 if a20
.value
== u
"catch*":
119 return EVAL(a1
, env
);
120 except types
.MalException
as exc
:
122 catch_env
= Env(env
, _list(a2
[1]), _list(exc
))
123 return EVAL(a2
[2], catch_env
)
124 except Exception as exc
:
125 exc
= MalStr(unicode("%s" % exc
))
126 catch_env
= Env(env
, _list(a2
[1]), _list(exc
))
127 return EVAL(a2
[2], catch_env
)
128 return EVAL(a1
, env
);
133 eval_ast(ast
.slice2(1, len(ast
)-1), env
)
134 ast
= ast
[-1] # Continue loop (TCO)
136 a1
, a2
= ast
[1], ast
[2]
138 if cond
is nil
or cond
is false
:
139 if len(ast
) > 3: ast
= ast
[3] # Continue loop (TCO)
142 ast
= a2
# Continue loop (TCO)
143 elif u
"fn*" == a0sym
:
144 a1
, a2
= ast
[1], ast
[2]
145 return MalFunc(None, a2
, env
, a1
, EVAL
)
147 el
= eval_ast(ast
, env
)
149 if isinstance(f
, MalFunc
):
152 env
= f
.gen_env(el
.rest()) # Continue loop (TCO)
154 return f
.apply(el
.rest())
156 raise Exception("%s is not callable" % f
)
160 return printer
._pr
_str
(exp
)
163 class MalEval(MalFunc
):
164 def apply(self
, args
):
165 return self
.EvalFunc(args
[0], self
.env
)
167 def entry_point(argv
):
170 return PRINT(EVAL(READ(str), env
))
172 # core.py: defined using python
173 for k
, v
in core
.ns
.items():
174 repl_env
.set(_symbol(unicode(k
)), MalFunc(v
))
175 repl_env
.set(types
._symbol
(u
'eval'),
176 MalEval(None, env
=repl_env
, EvalFunc
=EVAL
))
179 for a
in argv
[2:]: mal_args
.append(MalStr(unicode(a
)))
180 repl_env
.set(_symbol(u
'*ARGV*'), MalList(mal_args
))
182 # core.mal: defined using the language itself
183 REP("(def! not (fn* (a) (if a false true)))", repl_env
)
184 REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", repl_env
)
185 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)))))))", repl_env
)
186 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))))))))", repl_env
)
189 REP('(load-file "' + argv
[1] + '")', repl_env
)
194 line
= mal_readline
.readline("user> ")
195 if line
== "": continue
196 print(REP(line
, repl_env
))
197 except EOFError as e
:
201 except types
.MalException
as e
:
202 print(u
"Error: %s" % printer
._pr
_str
(e
.object, False))
203 except Exception as e
:
204 print("Error: %s" % e
)
205 #print("".join(traceback.format_exception(*sys.exc_info())))
208 # _____ Define and setup target ___
212 # Just run entry_point if not RPython compilation
214 if not sys
.argv
[0].endswith('rpython'):
215 entry_point(sys
.argv
)