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 "do": | |
172 | auto all_but_last = new MalList(aste[1..$-1]); | |
173 | eval_ast(all_but_last, env); | |
174 | ast = aste[$-1]; | |
175 | continue; // TCO | |
176 | ||
177 | case "if": | |
178 | auto cond = EVAL(aste[1], env); | |
179 | if (cond.is_truthy()) | |
180 | { | |
181 | ast = aste[2]; | |
182 | continue; // TCO | |
183 | } | |
184 | else | |
185 | if (aste.length > 3) | |
186 | { | |
187 | ast = aste[3]; | |
188 | continue; // TCO | |
189 | } | |
190 | else | |
191 | { | |
192 | return mal_nil; | |
193 | } | |
194 | ||
195 | case "fn*": | |
196 | auto args_list = verify_cast!MalSequential(aste[1]); | |
197 | return new MalFunc(args_list.elements, aste[2], env); | |
198 | ||
199 | default: | |
200 | auto el = verify_cast!MalList(eval_ast(ast, env)); | |
201 | if (el.elements.length == 0) | |
202 | { | |
203 | throw new Exception("Expected a non-empty list"); | |
204 | } | |
205 | auto first = el.elements[0]; | |
206 | auto rest = el.elements[1..$]; | |
2eb69541 | 207 | if (auto funcobj = cast(MalFunc)first) |
f82cb965 | 208 | { |
f82cb965 DM |
209 | auto callenv = new Env(funcobj.def_env, funcobj.arg_names, rest); |
210 | ast = funcobj.func_body; | |
211 | env = callenv; | |
212 | continue; // TCO | |
213 | } | |
2eb69541 | 214 | else if (auto builtinfuncobj = cast(MalBuiltinFunc)first) |
f82cb965 | 215 | { |
f82cb965 DM |
216 | return builtinfuncobj.fn(rest); |
217 | } | |
218 | else | |
219 | { | |
220 | throw new Exception("Expected a function"); | |
221 | } | |
222 | } | |
223 | } | |
224 | } | |
225 | ||
226 | string PRINT(MalType ast) | |
227 | { | |
228 | return pr_str(ast); | |
229 | } | |
230 | ||
231 | MalType re(string str, Env env) | |
232 | { | |
233 | return EVAL(READ(str), env); | |
234 | } | |
235 | ||
236 | string rep(string str, Env env) | |
237 | { | |
238 | return PRINT(re(str, env)); | |
239 | } | |
240 | ||
241 | static MalList create_argv_list(string[] args) | |
242 | { | |
243 | if (args.length <= 2) return new MalList([]); | |
244 | return new MalList(array(args[2..$].map!(s => cast(MalType)(new MalString(s))))); | |
245 | } | |
246 | ||
247 | void main(string[] args) | |
248 | { | |
249 | Env repl_env = new Env(null); | |
250 | foreach (string sym_name, BuiltinStaticFuncType f; core_ns) | |
251 | { | |
252 | repl_env.set(new MalSymbol(sym_name), new MalBuiltinFunc(f, sym_name)); | |
253 | } | |
254 | ||
255 | BuiltinFuncType eval_func = (a ...) { | |
256 | verify_args_count(a, 1); | |
257 | return EVAL(a[0], repl_env); | |
258 | }; | |
259 | repl_env.set(new MalSymbol("eval"), new MalBuiltinFunc(eval_func, "eval")); | |
260 | repl_env.set(new MalSymbol("*ARGV*"), create_argv_list(args)); | |
261 | ||
262 | // core.mal: defined using the language itself | |
263 | re("(def! not (fn* (a) (if a false true)))", repl_env); | |
e6d41de4 | 264 | re("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))", repl_env); |
f82cb965 | 265 | 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 |
266 | |
267 | if (args.length > 1) | |
268 | { | |
269 | try | |
270 | { | |
271 | rep("(load-file \"" ~ args[1] ~ "\")", repl_env); | |
272 | return; | |
273 | } | |
274 | catch (Exception e) | |
275 | { | |
276 | writeln("Error: ", e.msg); | |
bc33c480 | 277 | exit(1); |
f82cb965 DM |
278 | } |
279 | } | |
280 | ||
281 | for (;;) | |
282 | { | |
283 | string line = _readline("user> "); | |
284 | if (line is null) break; | |
285 | if (line.length == 0) continue; | |
286 | try | |
287 | { | |
288 | writeln(rep(line, repl_env)); | |
289 | } | |
290 | catch (Exception e) | |
291 | { | |
292 | writeln("Error: ", e.msg); | |
293 | } | |
294 | } | |
295 | writeln(""); | |
296 | } |