DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / chuck / step8_macros.ck
1 // @import readline.ck
2 // @import types/boxed/*.ck
3 // @import types/MalObject.ck
4 // @import types/mal/MalAtom.ck
5 // @import types/mal/MalError.ck
6 // @import types/mal/MalNil.ck
7 // @import types/mal/MalFalse.ck
8 // @import types/mal/MalTrue.ck
9 // @import types/mal/MalInt.ck
10 // @import types/mal/MalString.ck
11 // @import types/mal/MalSymbol.ck
12 // @import types/mal/MalKeyword.ck
13 // @import types/mal/MalList.ck
14 // @import types/mal/MalVector.ck
15 // @import types/mal/MalHashMap.ck
16 // @import util/*.ck
17 // @import reader.ck
18 // @import printer.ck
19 // @import env.ck
20 // @import func.ck
21 // @import types/MalSubr.ck
22 // @import types/subr/*.ck
23 // @import core.ck
24
25 fun MalObject READ(string input)
26 {
27 return Reader.read_str(input);
28 }
29
30 fun int isPair(MalObject m)
31 {
32 if( (m.type == "list" || m.type == "vector") &&
33 Util.sequenceToMalObjectArray(m).size() > 0 )
34 {
35 return true;
36 }
37 else
38 {
39 return false;
40 }
41 }
42
43 fun MalObject quasiquote(MalObject ast)
44 {
45 if( !isPair(ast) )
46 {
47 return MalList.create([MalSymbol.create("quote"), ast]);
48 }
49
50 Util.sequenceToMalObjectArray(ast) @=> MalObject a[];
51 a[0] @=> MalObject a0;
52
53 if( a0.type == "symbol" && (a0$MalSymbol).value() == "unquote" )
54 {
55 return a[1];
56 }
57
58 if( isPair(a0) )
59 {
60 Util.sequenceToMalObjectArray(a0) @=> MalObject a0_[];
61 a0_[0] @=> MalObject a0_0;
62
63 if( a0_0.type == "symbol" && (a0_0$MalSymbol).value() == "splice-unquote" )
64 {
65 return MalList.create(
66 [MalSymbol.create("concat"), a0_[1],
67 quasiquote(MalList.create(MalObject.slice(a, 1)))]);
68 }
69 }
70
71 return MalList.create(
72 [MalSymbol.create("cons"), quasiquote(a[0]),
73 quasiquote(MalList.create(MalObject.slice(a, 1)))]);
74 }
75
76 fun int isMacroCall(MalObject ast, Env env)
77 {
78 if( ast.type == "list" )
79 {
80 (ast$MalList).value() @=> MalObject a[];
81
82 if( a[0].type == "symbol" )
83 {
84 (a[0]$MalSymbol).value() => string name;
85 env.find(name) @=> MalObject value;
86
87 if( value != null && value.type == "func" && (value$Func).isMacro )
88 {
89 return true;
90 }
91 }
92 }
93
94 return false;
95 }
96
97 fun MalObject macroexpand(MalObject ast, Env env)
98 {
99 while( isMacroCall(ast, env) )
100 {
101 Util.sequenceToMalObjectArray(ast) @=> MalObject list[];
102 (list[0]$MalSymbol).value() => string name;
103 env.get(name) @=> MalObject macro;
104 MalObject.slice(list, 1) @=> MalObject args[];
105
106 if( macro.type == "subr" )
107 {
108 (macro$MalSubr).call(args) @=> ast;
109 }
110 else // macro.type == "func"
111 {
112 macro$Func @=> Func func;
113 Env.create(func.env, func.args, args) @=> Env eval_env;
114 EVAL(func.ast, eval_env) @=> ast;;
115 }
116 }
117
118 return ast;
119 }
120
121 fun MalObject EVAL(MalObject m, Env env)
122 {
123 while( true )
124 {
125 if( m.type != "list" )
126 {
127 return eval_ast(m, env);
128 }
129
130 if( (m$MalList).value().size() == 0 )
131 {
132 return m;
133 }
134
135 macroexpand(m, env) @=> m;
136
137 if( m.type != "list" )
138 {
139 return eval_ast(m, env);
140 }
141
142 (m$MalList).value() @=> MalObject ast[];
143
144 if( ast[0].type == "symbol" )
145 {
146 (ast[0]$MalSymbol).value() => string a0;
147
148 if( a0 == "def!" )
149 {
150 (ast[1]$MalSymbol).value() => string a1;
151
152 EVAL(ast[2], env) @=> MalObject value;
153 if( value.type == "error" )
154 {
155 return value;
156 }
157
158 env.set(a1, value);
159 return value;
160 }
161 else if( a0 == "let*" )
162 {
163 Env.create(env) @=> Env let_env;
164 Util.sequenceToMalObjectArray(ast[1]) @=> MalObject bindings[];
165
166 for( 0 => int i; i < bindings.size(); 2 +=> i)
167 {
168 (bindings[i]$MalSymbol).value() => string symbol;
169 EVAL(bindings[i+1], let_env) @=> MalObject value;
170
171 if( value.type == "error" )
172 {
173 return value;
174 }
175
176 let_env.set(symbol, value);
177 }
178
179 let_env @=> env;
180 ast[2] @=> m;
181 continue; // TCO
182 }
183 else if( a0 == "quote" )
184 {
185 return ast[1];
186 }
187 else if( a0 == "quasiquote" )
188 {
189 quasiquote(ast[1]) @=> m;
190 continue; // TCO
191 }
192 else if( a0 == "defmacro!" )
193 {
194 (ast[1]$MalSymbol).value() => string a1;
195
196 EVAL(ast[2], env) @=> MalObject value;
197 if( value.type == "error" )
198 {
199 return value;
200 }
201
202 true => (value$Func).isMacro;
203
204 env.set(a1, value);
205 return value;
206 }
207 else if( a0 == "macroexpand" )
208 {
209 return macroexpand(ast[1], env);
210 }
211 else if( a0 == "do" )
212 {
213 MalObject.slice(ast, 1, ast.size()-1) @=> MalObject forms[];
214 eval_ast(MalList.create(forms), env) @=> MalObject value;
215
216 if( value.type == "error" )
217 {
218 return value;
219 }
220
221 // HACK: this assumes do gets at least one argument...
222 ast[ast.size()-1] @=> m;
223 continue; // TCO
224 }
225 else if( a0 == "if" )
226 {
227 EVAL(ast[1], env) @=> MalObject condition;
228
229 if( condition.type == "error" )
230 {
231 return condition;
232 }
233
234 if( !(condition.type == "nil") && !(condition.type == "false") )
235 {
236 ast[2] @=> m;
237 continue; // TCO
238 }
239 else
240 {
241 if( ast.size() < 4 )
242 {
243 return Constants.NIL;
244 }
245 else
246 {
247 ast[3] @=> m;
248 continue; // TCO
249 }
250 }
251 }
252 else if( a0 == "fn*" )
253 {
254 (ast[1]$MalList).value() @=> MalObject arg_values[];
255 string args[arg_values.size()];
256
257 for( 0 => int i; i < arg_values.size(); i++ )
258 {
259 (arg_values[i]$MalSymbol).value() => args[i];
260 }
261
262 ast[2] @=> MalObject _ast;
263
264 return Func.create(env, args, _ast);
265 }
266 }
267
268 eval_ast(m, env) @=> MalObject result;
269 if( result.type == "error" )
270 {
271 return result;
272 }
273
274 (result$MalList).value() @=> MalObject values[];
275 values[0].type => string type;
276 MalObject.slice(values, 1) @=> MalObject args[];
277
278 if( type == "subr" )
279 {
280 values[0]$MalSubr @=> MalSubr subr;
281 return subr.call(args);
282 }
283 else // type == "func"
284 {
285 values[0]$Func @=> Func func;
286 Env.create(func.env, func.args, args) @=> Env eval_env;
287 eval_env @=> env;
288 func.ast @=> m;
289 continue; // TCO
290 }
291 }
292 }
293
294 fun MalObject eval_ast(MalObject m, Env env)
295 {
296 m.type => string type;
297
298 if( type == "symbol" )
299 {
300 (m$MalSymbol).value() => string symbol;
301 return env.get(symbol);
302 }
303 else if( type == "list" || type == "vector" || type == "hashmap" )
304 {
305 (m$MalList).value() @=> MalObject values[];
306 MalObject results[values.size()];
307
308 if( type != "hashmap" )
309 {
310 for( 0 => int i; i < values.size(); i++ )
311 {
312 EVAL(values[i], env) @=> MalObject result;
313
314 if( result.type == "error" )
315 {
316 return result;
317 }
318
319 result @=> results[i];
320 }
321 }
322 else
323 {
324 for( 0 => int i; i < values.size(); i++ )
325 {
326 if( i % 2 == 0 )
327 {
328 values[i] @=> results[i];
329 }
330 else
331 {
332 EVAL(values[i], env) @=> results[i];
333 }
334 }
335 }
336
337 if( type == "list" )
338 {
339 return MalList.create(results);
340 }
341 else if( type == "vector" )
342 {
343 return MalVector.create(results);
344 }
345 else if( type == "hashmap" )
346 {
347 return MalHashMap.create(results);
348 }
349 }
350 else
351 {
352 return m;
353 }
354 }
355
356 fun string PRINT(MalObject m)
357 {
358 return Printer.pr_str(m, true);
359 }
360
361 Env.create(null) @=> Env repl_env;
362 for( 0 => int i; i < Core.names.size(); i++ )
363 {
364 Core.names[i] => string name;
365 repl_env.set(name, Core.ns[name]);
366 }
367
368 // HACK, HACK, HACK
369 class MalEval extends MalSubr
370 {
371 fun MalObject call(MalObject args[])
372 {
373 args[0] @=> MalObject m;
374 return EVAL(args[0], repl_env);
375 }
376
377 fun MalObject apply(MalObject f, MalObject args[])
378 {
379 if( f.type == "subr" )
380 {
381 return (f$MalSubr).call(args);
382 }
383 else // f.type == "func"
384 {
385 f$Func @=> Func func;
386 Env.create(func.env, func.args, args) @=> Env eval_env;
387 return EVAL(func.ast, eval_env);
388 }
389 }
390 }
391
392 new MalEval @=> MalEval eval;
393 repl_env.set("eval", new MalEval);
394 eval @=> (repl_env.get("swap!")$MalSubr).eval;
395
396 fun MalObject[] MalArgv(string args[])
397 {
398 MalObject values[0];
399
400 for( 1 => int i; i < args.size(); i++ )
401 {
402 values << MalString.create(args[i]);
403 }
404
405 return values;
406 }
407
408 // NOTE: normally I'd use \0, but strings are null-terminated...
409 String.split(Std.getenv("CHUCK_ARGS"), "\a") @=> string args[];
410 repl_env.set("*ARGV*", MalList.create(MalArgv(args)));
411
412 fun string errorMessage(MalObject m)
413 {
414 (m$MalError).value() @=> MalObject value;
415 return "exception: " + Printer.pr_str(value, true);
416 }
417
418 fun string rep(string input)
419 {
420 READ(input) @=> MalObject m;
421
422 if( m.type == "error" )
423 {
424 return errorMessage(m);
425 }
426
427 EVAL(m, repl_env) @=> MalObject result;
428 if( result.type == "error" )
429 {
430 return errorMessage(result);
431 }
432
433 return PRINT(result);
434 }
435
436 rep("(def! not (fn* (a) (if a false true)))");
437 rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))");
438 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)))))))");
439
440 fun void main()
441 {
442 int done;
443
444 while( !done )
445 {
446 Readline.readline("user> ") => string input;
447
448 if( input != null )
449 {
450 rep(input) => string output;
451
452 if( output == "empty input" )
453 {
454 // proceed immediately with prompt
455 }
456 else
457 {
458 Util.println(output);
459 }
460 }
461 else
462 {
463 true => done;
464 }
465 }
466 }
467
468 if( args.size() > 0 )
469 {
470 args[0] => string filename;
471 rep("(load-file \"" + filename + "\")");
472 }
473 else
474 {
475 main();
476 }