Merge pull request #145 from ivern/kotlin
[jackhill/mal.git] / d / step8_macros.d
CommitLineData
f82cb965
DM
1module main;
2
3import std.algorithm;
4import std.array;
5import std.range;
6import std.stdio;
7import std.string;
8import std.c.process;
9import env;
10import mal_core;
11import readline;
12import reader;
13import printer;
14import types;
15
16bool 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
23MalType 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
48bool 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
62MalType 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
76MalType READ(string str)
77{
78 return read_str(str);
79}
80
81MalType eval_ast(MalType ast, Env env)
82{
83 if (typeid(ast) == typeid(MalSymbol))
84 {
85 auto sym = verify_cast!MalSymbol(ast);
86 return env.get(sym);
87 }
88 else if (typeid(ast) == typeid(MalList))
89 {
90 auto lst = verify_cast!MalList(ast);
91 auto el = array(lst.elements.map!(e => EVAL(e, env)));
92 return new MalList(el);
93 }
94 else if (typeid(ast) == typeid(MalVector))
95 {
96 auto lst = verify_cast!MalVector(ast);
97 auto el = array(lst.elements.map!(e => EVAL(e, env)));
98 return new MalVector(el);
99 }
100 else if (typeid(ast) == typeid(MalHashmap))
101 {
102 auto hm = verify_cast!MalHashmap(ast);
103 typeof(hm.data) new_data;
104 foreach (string k, MalType v; hm.data)
105 {
106 new_data[k] = EVAL(v, env);
107 }
108 return new MalHashmap(new_data);
109 }
110 else
111 {
112 return ast;
113 }
114}
115
116MalType EVAL(MalType ast, Env env)
117{
118 for (;;)
119 {
120 MalList ast_list = cast(MalList) ast;
121 if (ast_list is null)
122 {
123 return eval_ast(ast, env);
124 }
125
126 ast = macroexpand(ast, env);
127 ast_list = cast(MalList) ast;
128 if (ast_list is null)
129 {
130 return ast;
131 }
132
133 auto aste = ast_list.elements;
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..$];
207 if (typeid(first) == typeid(MalFunc))
208 {
209 auto funcobj = verify_cast!MalFunc(first);
210 auto callenv = new Env(funcobj.def_env, funcobj.arg_names, rest);
211 ast = funcobj.func_body;
212 env = callenv;
213 continue; // TCO
214 }
215 else if (typeid(first) == typeid(MalBuiltinFunc))
216 {
217 auto builtinfuncobj = verify_cast!MalBuiltinFunc(first);
218 return builtinfuncobj.fn(rest);
219 }
220 else
221 {
222 throw new Exception("Expected a function");
223 }
224 }
225 }
226}
227
228string PRINT(MalType ast)
229{
230 return pr_str(ast);
231}
232
233MalType re(string str, Env env)
234{
235 return EVAL(READ(str), env);
236}
237
238string rep(string str, Env env)
239{
240 return PRINT(re(str, env));
241}
242
243static MalList create_argv_list(string[] args)
244{
245 if (args.length <= 2) return new MalList([]);
246 return new MalList(array(args[2..$].map!(s => cast(MalType)(new MalString(s)))));
247}
248
249void main(string[] args)
250{
251 Env repl_env = new Env(null);
252 foreach (string sym_name, BuiltinStaticFuncType f; core_ns)
253 {
254 repl_env.set(new MalSymbol(sym_name), new MalBuiltinFunc(f, sym_name));
255 }
256
257 BuiltinFuncType eval_func = (a ...) {
258 verify_args_count(a, 1);
259 return EVAL(a[0], repl_env);
260 };
261 repl_env.set(new MalSymbol("eval"), new MalBuiltinFunc(eval_func, "eval"));
262 repl_env.set(new MalSymbol("*ARGV*"), create_argv_list(args));
263
264 // core.mal: defined using the language itself
265 re("(def! not (fn* (a) (if a false true)))", repl_env);
266 re("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", repl_env);
267 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);
268 re("(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);
269
270 if (args.length > 1)
271 {
272 try
273 {
274 rep("(load-file \"" ~ args[1] ~ "\")", repl_env);
275 return;
276 }
277 catch (Exception e)
278 {
279 writeln("Error: ", e.msg);
280 std.c.process.exit(1);
281 }
282 }
283
284 for (;;)
285 {
286 string line = _readline("user> ");
287 if (line is null) break;
288 if (line.length == 0) continue;
289 try
290 {
291 writeln(rep(line, repl_env));
292 }
293 catch (Exception e)
294 {
295 writeln("Error: ", e.msg);
296 }
297 }
298 writeln("");
299}