Commit | Line | Data |
---|---|---|
f82cb965 DM |
1 | module main; |
2 | ||
3 | import std.algorithm; | |
4 | import std.array; | |
5 | import std.range; | |
6 | import std.stdio; | |
7 | import std.string; | |
bc33c480 | 8 | import core.stdc.stdlib; |
f82cb965 DM |
9 | import env; |
10 | import mal_core; | |
11 | import readline; | |
12 | import reader; | |
13 | import printer; | |
14 | import types; | |
15 | ||
16 | bool is_pair(MalType ast) | |
17 | { | |
18 | auto lst = cast(MalSequential) ast; | |
19 | if (lst is null) return false; | |
20 | return lst.elements.length > 0; | |
21 | } | |
22 | ||
23 | MalType quasiquote(MalType ast) | |
24 | { | |
25 | if (!is_pair(ast)) | |
26 | { | |
27 | return new MalList([sym_quote, ast]); | |
28 | } | |
29 | auto ast_seq = verify_cast!MalSequential(ast); | |
30 | auto aste = ast_seq.elements; | |
31 | if (aste[0] == sym_unquote) | |
32 | { | |
33 | return aste[1]; | |
34 | } | |
35 | ||
36 | if (is_pair(aste[0])) | |
37 | { | |
38 | auto ast0_seq = verify_cast!MalSequential(aste[0]); | |
39 | if (ast0_seq.elements[0] == sym_splice_unquote) | |
40 | { | |
41 | return new MalList([new MalSymbol("concat"), ast0_seq.elements[1], quasiquote(new MalList(aste[1..$]))]); | |
42 | } | |
43 | } | |
44 | ||
45 | return new MalList([new MalSymbol("cons"), quasiquote(aste[0]), quasiquote(new MalList(aste[1..$]))]); | |
46 | } | |
47 | ||
48 | bool is_macro_call(MalType ast, Env env) | |
49 | { | |
50 | auto lst = cast(MalList) ast; | |
51 | if (lst is null) return false; | |
52 | if (lst.elements.length == 0) return false; | |
53 | auto sym0 = cast(MalSymbol) lst.elements[0]; | |
54 | if (sym0 is null) return false; | |
55 | if (env.find(sym0) is null) return false; | |
56 | auto val = env.get(sym0); | |
57 | auto val_func = cast(MalFunc) val; | |
58 | if (val_func is null) return false; | |
59 | return val_func.is_macro; | |
60 | } | |
61 | ||
62 | MalType macroexpand(MalType ast, Env env) | |
63 | { | |
64 | while (is_macro_call(ast, env)) | |
65 | { | |
66 | auto ast_list = verify_cast!MalList(ast); | |
67 | auto sym0 = verify_cast!MalSymbol(ast_list.elements[0]); | |
68 | auto macrofunc = verify_cast!MalFunc(env.get(sym0)); | |
69 | auto rest = ast_list.elements[1..$]; | |
70 | auto callenv = new Env(macrofunc.def_env, macrofunc.arg_names, rest); | |
71 | ast = EVAL(macrofunc.func_body, callenv); | |
72 | } | |
73 | return ast; | |
74 | } | |
75 | ||
76 | MalType READ(string str) | |
77 | { | |
78 | return read_str(str); | |
79 | } | |
80 | ||
81 | MalType eval_ast(MalType ast, Env env) | |
82 | { | |
2eb69541 | 83 | if (auto sym = cast(MalSymbol)ast) |
f82cb965 | 84 | { |
f82cb965 DM |
85 | return env.get(sym); |
86 | } | |
2eb69541 | 87 | else if (auto lst = cast(MalList)ast) |
f82cb965 | 88 | { |
f82cb965 DM |
89 | auto el = array(lst.elements.map!(e => EVAL(e, env))); |
90 | return new MalList(el); | |
91 | } | |
2eb69541 | 92 | else if (auto lst = cast(MalVector)ast) |
f82cb965 | 93 | { |
f82cb965 DM |
94 | auto el = array(lst.elements.map!(e => EVAL(e, env))); |
95 | return new MalVector(el); | |
96 | } | |
2eb69541 | 97 | else if (auto hm = cast(MalHashmap)ast) |
f82cb965 | 98 | { |
f82cb965 DM |
99 | typeof(hm.data) new_data; |
100 | foreach (string k, MalType v; hm.data) | |
101 | { | |
102 | new_data[k] = EVAL(v, env); | |
103 | } | |
104 | return new MalHashmap(new_data); | |
105 | } | |
106 | else | |
107 | { | |
108 | return ast; | |
109 | } | |
110 | } | |
111 | ||
112 | MalType EVAL(MalType ast, Env env) | |
113 | { | |
114 | for (;;) | |
115 | { | |
116 | MalList ast_list = cast(MalList) ast; | |
117 | if (ast_list is null) | |
118 | { | |
119 | return eval_ast(ast, env); | |
120 | } | |
121 | ||
122 | ast = macroexpand(ast, env); | |
123 | ast_list = cast(MalList) ast; | |
124 | if (ast_list is null) | |
125 | { | |
6c94cd3e | 126 | return eval_ast(ast, env); |
f82cb965 DM |
127 | } |
128 | ||
129 | auto aste = ast_list.elements; | |
ece4cad1 DM |
130 | if (aste.length == 0) |
131 | { | |
132 | return ast; | |
133 | } | |
f82cb965 DM |
134 | auto a0_sym = cast(MalSymbol) aste[0]; |
135 | auto sym_name = a0_sym is null ? "" : a0_sym.name; | |
136 | switch (sym_name) | |
137 | { | |
138 | case "def!": | |
139 | auto a1 = verify_cast!MalSymbol(aste[1]); | |
140 | return env.set(a1, EVAL(aste[2], env)); | |
141 | ||
142 | case "let*": | |
143 | auto a1 = verify_cast!MalSequential(aste[1]); | |
144 | auto let_env = new Env(env); | |
145 | foreach (kv; chunks(a1.elements, 2)) | |
146 | { | |
147 | if (kv.length < 2) throw new Exception("let* requires even number of elements"); | |
148 | auto var_name = verify_cast!MalSymbol(kv[0]); | |
149 | let_env.set(var_name, EVAL(kv[1], let_env)); | |
150 | } | |
151 | ast = aste[2]; | |
152 | env = let_env; | |
153 | continue; // TCO | |
154 | ||
155 | case "quote": | |
156 | return aste[1]; | |
157 | ||
158 | case "quasiquote": | |
159 | ast = quasiquote(aste[1]); | |
160 | continue; // TCO | |
161 | ||
162 | case "defmacro!": | |
163 | auto a1 = verify_cast!MalSymbol(aste[1]); | |
164 | auto mac = verify_cast!MalFunc(EVAL(aste[2], env)); | |
165 | mac.is_macro = true; | |
166 | return env.set(a1, mac); | |
167 | ||
168 | case "macroexpand": | |
169 | return macroexpand(aste[1], env); | |
170 | ||
171 | case "try*": | |
2eb69541 | 172 | if (aste.length < 2) return mal_nil; |
2b2ba8fc DM |
173 | if (aste.length < 3) |
174 | { | |
2eb69541 UK |
175 | ast = aste[1]; |
176 | continue; // TCO | |
2b2ba8fc | 177 | } |
f82cb965 DM |
178 | MalType exc; |
179 | try | |
180 | { | |
2eb69541 UK |
181 | // d seems to do erroneous tco all by itself without this |
182 | // little distraction | |
183 | pr_str(aste[1]); | |
f82cb965 DM |
184 | return EVAL(aste[1], env); |
185 | } | |
186 | catch (MalException e) | |
187 | { | |
188 | exc = e.data; | |
189 | } | |
190 | catch (Exception e) | |
191 | { | |
192 | exc = new MalString(e.msg); | |
193 | } | |
f82cb965 DM |
194 | auto catch_clause = verify_cast!MalList(aste[2]); |
195 | auto catch_env = new Env(env, [catch_clause.elements[1]], [exc]); | |
2eb69541 UK |
196 | ast = catch_clause.elements[2]; |
197 | env = catch_env; | |
198 | continue; // TCO | |
f82cb965 DM |
199 | |
200 | case "do": | |
201 | auto all_but_last = new MalList(aste[1..$-1]); | |
202 | eval_ast(all_but_last, env); | |
203 | ast = aste[$-1]; | |
204 | continue; // TCO | |
205 | ||
206 | case "if": | |
207 | auto cond = EVAL(aste[1], env); | |
208 | if (cond.is_truthy()) | |
209 | { | |
210 | ast = aste[2]; | |
211 | continue; // TCO | |
212 | } | |
213 | else | |
214 | if (aste.length > 3) | |
215 | { | |
216 | ast = aste[3]; | |
217 | continue; // TCO | |
218 | } | |
219 | else | |
220 | { | |
221 | return mal_nil; | |
222 | } | |
223 | ||
224 | case "fn*": | |
225 | auto args_list = verify_cast!MalSequential(aste[1]); | |
226 | return new MalFunc(args_list.elements, aste[2], env); | |
227 | ||
228 | default: | |
229 | auto el = verify_cast!MalList(eval_ast(ast, env)); | |
230 | if (el.elements.length == 0) | |
231 | { | |
232 | throw new Exception("Expected a non-empty list"); | |
233 | } | |
234 | auto first = el.elements[0]; | |
235 | auto rest = el.elements[1..$]; | |
2eb69541 | 236 | if (auto funcobj = cast(MalFunc)first) |
f82cb965 | 237 | { |
f82cb965 DM |
238 | auto callenv = new Env(funcobj.def_env, funcobj.arg_names, rest); |
239 | ast = funcobj.func_body; | |
240 | env = callenv; | |
241 | continue; // TCO | |
242 | } | |
2eb69541 | 243 | else if (auto builtinfuncobj = cast(MalBuiltinFunc)first) |
f82cb965 | 244 | { |
f82cb965 DM |
245 | return builtinfuncobj.fn(rest); |
246 | } | |
247 | else | |
248 | { | |
249 | throw new Exception("Expected a function"); | |
250 | } | |
251 | } | |
252 | } | |
253 | } | |
254 | ||
255 | string PRINT(MalType ast) | |
256 | { | |
257 | return pr_str(ast); | |
258 | } | |
259 | ||
260 | MalType re(string str, Env env) | |
261 | { | |
262 | return EVAL(READ(str), env); | |
263 | } | |
264 | ||
265 | string rep(string str, Env env) | |
266 | { | |
267 | return PRINT(re(str, env)); | |
268 | } | |
269 | ||
270 | static MalList create_argv_list(string[] args) | |
271 | { | |
272 | if (args.length <= 2) return new MalList([]); | |
273 | return new MalList(array(args[2..$].map!(s => cast(MalType)(new MalString(s))))); | |
274 | } | |
275 | ||
276 | void main(string[] args) | |
277 | { | |
278 | Env repl_env = new Env(null); | |
279 | foreach (string sym_name, BuiltinStaticFuncType f; core_ns) | |
280 | { | |
281 | repl_env.set(new MalSymbol(sym_name), new MalBuiltinFunc(f, sym_name)); | |
282 | } | |
283 | ||
284 | BuiltinFuncType eval_func = (a ...) { | |
285 | verify_args_count(a, 1); | |
286 | return EVAL(a[0], repl_env); | |
287 | }; | |
288 | repl_env.set(new MalSymbol("eval"), new MalBuiltinFunc(eval_func, "eval")); | |
289 | repl_env.set(new MalSymbol("*ARGV*"), create_argv_list(args)); | |
290 | ||
291 | // core.mal: defined using the language itself | |
292 | re("(def! not (fn* (a) (if a false true)))", repl_env); | |
293 | re("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", repl_env); | |
294 | re("(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); | |
f82cb965 DM |
295 | |
296 | if (args.length > 1) | |
297 | { | |
298 | try | |
299 | { | |
300 | rep("(load-file \"" ~ args[1] ~ "\")", repl_env); | |
301 | return; | |
302 | } | |
303 | catch (Exception e) | |
304 | { | |
305 | writeln("Error: ", e.msg); | |
bc33c480 | 306 | exit(1); |
f82cb965 DM |
307 | } |
308 | } | |
309 | ||
310 | for (;;) | |
311 | { | |
312 | string line = _readline("user> "); | |
313 | if (line is null) break; | |
314 | if (line.length == 0) continue; | |
315 | try | |
316 | { | |
317 | writeln(rep(line, repl_env)); | |
318 | } | |
dd7a4f55 JM |
319 | catch (MalException e) |
320 | { | |
321 | writeln("Error: ", pr_str(e.data)); | |
322 | } | |
f82cb965 DM |
323 | catch (Exception e) |
324 | { | |
325 | writeln("Error: ", e.msg); | |
326 | } | |
327 | } | |
328 | writeln(""); | |
329 | } |