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
)
80 if len(ast
) == 0: return ast
83 ast
= macroexpand(ast
, env
)
84 if not types
._list
_Q
(ast
):
85 return eval_ast(ast
, env
)
86 if len(ast
) == 0: return ast
88 if isinstance(a0
, MalSym
):
94 a1
, a2
= ast
[1], ast
[2]
96 return env
.set(a1
, res
)
97 elif u
"let*" == a0sym
:
98 a1
, a2
= ast
[1], ast
[2]
100 for i
in range(0, len(a1
), 2):
101 let_env
.set(a1
[i
], EVAL(a1
[i
+1], let_env
))
103 env
= let_env
# Continue loop (TCO)
104 elif u
"quote" == a0sym
:
106 elif u
"quasiquote" == a0sym
:
107 ast
= quasiquote(ast
[1]) # Continue loop (TCO)
108 elif u
"defmacro!" == a0sym
:
109 func
= EVAL(ast
[2], env
)
111 return env
.set(ast
[1], func
)
112 elif u
"macroexpand" == a0sym
:
113 return macroexpand(ast
[1], env
)
114 elif u
"try*" == a0sym
:
116 return EVAL(ast
[1], env
);
117 a1
, a2
= ast
[1], ast
[2]
119 if isinstance(a20
, MalSym
):
120 if a20
.value
== u
"catch*":
122 return EVAL(a1
, env
);
123 except types
.MalException
as exc
:
125 catch_env
= Env(env
, _list(a2
[1]), _list(exc
))
126 return EVAL(a2
[2], catch_env
)
127 except Exception as exc
:
128 exc
= MalStr(unicode("%s" % exc
))
129 catch_env
= Env(env
, _list(a2
[1]), _list(exc
))
130 return EVAL(a2
[2], catch_env
)
131 return EVAL(a1
, env
);
136 eval_ast(ast
.slice2(1, len(ast
)-1), env
)
137 ast
= ast
[-1] # Continue loop (TCO)
139 a1
, a2
= ast
[1], ast
[2]
141 if cond
is nil
or cond
is false
:
142 if len(ast
) > 3: ast
= ast
[3] # Continue loop (TCO)
145 ast
= a2
# Continue loop (TCO)
146 elif u
"fn*" == a0sym
:
147 a1
, a2
= ast
[1], ast
[2]
148 return MalFunc(None, a2
, env
, a1
, EVAL
)
150 el
= eval_ast(ast
, env
)
152 if isinstance(f
, MalFunc
):
155 env
= f
.gen_env(el
.rest()) # Continue loop (TCO)
157 return f
.apply(el
.rest())
159 raise Exception("%s is not callable" % f
)
163 return printer
._pr
_str
(exp
)
166 class MalEval(MalFunc
):
167 def apply(self
, args
):
168 return self
.EvalFunc(args
[0], self
.env
)
170 def entry_point(argv
):
173 return PRINT(EVAL(READ(str), env
))
175 # core.py: defined using python
176 for k
, v
in core
.ns
.items():
177 repl_env
.set(_symbol(unicode(k
)), MalFunc(v
))
178 repl_env
.set(types
._symbol
(u
'eval'),
179 MalEval(None, env
=repl_env
, EvalFunc
=EVAL
))
182 for a
in argv
[2:]: mal_args
.append(MalStr(unicode(a
)))
183 repl_env
.set(_symbol(u
'*ARGV*'), MalList(mal_args
))
185 # core.mal: defined using the language itself
186 REP("(def! not (fn* (a) (if a false true)))", repl_env
)
187 REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))", repl_env
)
188 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
)
191 REP('(load-file "' + argv
[1] + '")', repl_env
)
196 line
= mal_readline
.readline("user> ")
197 if line
== "": continue
198 print(REP(line
, repl_env
))
199 except EOFError as e
:
203 except types
.MalException
as e
:
204 print(u
"Error: %s" % printer
._pr
_str
(e
.object, False))
205 except Exception as e
:
206 print("Error: %s" % e
)
207 #print("".join(traceback.format_exception(*sys.exc_info())))
210 # _____ Define and setup target ___
214 # Just run entry_point if not RPython compilation
216 if not sys
.argv
[0].endswith('rpython'):
217 entry_point(sys
.argv
)