coffee, dart, elixir, elm: detect unclosed strings.
[jackhill/mal.git] / dart / step8_macros.dart
1 import 'dart:io';
2
3 import 'core.dart';
4 import 'env.dart';
5 import 'printer.dart' as printer;
6 import 'reader.dart' as reader;
7 import 'types.dart';
8
9 final Env replEnv = new Env();
10
11 void setupEnv(List<String> argv) {
12 ns.forEach((sym, fun) => replEnv.set(sym, fun));
13
14 replEnv.set(new MalSymbol('eval'),
15 new MalBuiltin((List<MalType> args) => EVAL(args.single, replEnv)));
16
17 replEnv.set(new MalSymbol('*ARGV*'),
18 new MalList(argv.map((s) => new MalString(s)).toList()));
19
20 rep('(def! not (fn* (a) (if a false true)))');
21 rep("(def! load-file "
22 " (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))");
23 rep("(defmacro! cond "
24 " (fn* (& xs) (if (> (count xs) 0) "
25 " (list 'if (first xs) "
26 " (if (> (count xs) 1) "
27 " (nth xs 1) "
28 " (throw \"odd number of forms to cond\")) "
29 " (cons 'cond (rest (rest xs)))))))");
30 }
31
32 /// Returns `true` if [ast] is a macro call.
33 ///
34 /// This checks that [ast] is a list whose first element is a symbol that refers
35 /// to a function in the current [env] that is a macro.
36 bool isMacroCall(MalType ast, Env env) {
37 if (ast is MalList) {
38 if (ast.isNotEmpty && ast.first is MalSymbol) {
39 try {
40 var value = env.get(ast.first);
41 if (value is MalCallable) {
42 return value.isMacro;
43 }
44 } on NotFoundException {
45 return false;
46 }
47 }
48 }
49 return false;
50 }
51
52 MalType macroexpand(MalType ast, Env env) {
53 while (isMacroCall(ast, env)) {
54 var macroSymbol = (ast as MalList).first;
55 var macro = env.get(macroSymbol) as MalCallable;
56 ast = macro((ast as MalList).sublist(1));
57 }
58 return ast;
59 }
60
61 MalType quasiquote(MalType ast) {
62 bool isPair(MalType ast) {
63 return ast is MalIterable && ast.isNotEmpty;
64 }
65
66 if (!isPair(ast)) {
67 return new MalList([new MalSymbol("quote"), ast]);
68 } else {
69 var list = ast as MalIterable;
70 if (list.first == new MalSymbol("unquote")) {
71 return list[1];
72 } else if (isPair(list.first) &&
73 (list.first as MalIterable).first == new MalSymbol("splice-unquote")) {
74 return new MalList([
75 new MalSymbol("concat"),
76 (list.first as MalIterable)[1],
77 quasiquote(new MalList(list.sublist(1)))
78 ]);
79 } else {
80 return new MalList([
81 new MalSymbol("cons"),
82 quasiquote(list[0]),
83 quasiquote(new MalList(list.sublist(1)))
84 ]);
85 }
86 }
87 }
88
89 MalType READ(String x) => reader.read_str(x);
90
91 MalType eval_ast(MalType ast, Env env) {
92 if (ast is MalSymbol) {
93 return env.get(ast);
94 } else if (ast is MalList) {
95 return new MalList(ast.elements.map((x) => EVAL(x, env)).toList());
96 } else if (ast is MalVector) {
97 return new MalVector(ast.elements.map((x) => EVAL(x, env)).toList());
98 } else if (ast is MalHashMap) {
99 var newMap = new Map<MalType, MalType>.from(ast.value);
100 for (var key in newMap.keys) {
101 newMap[key] = EVAL(newMap[key], env);
102 }
103 return new MalHashMap(newMap);
104 } else {
105 return ast;
106 }
107 }
108
109 MalType EVAL(MalType ast, Env env) {
110 while (true) {
111 if (ast is! MalList) {
112 return eval_ast(ast, env);
113 } else {
114 if ((ast as MalList).elements.isEmpty) {
115 return ast;
116 } else {
117 ast = macroexpand(ast, env);
118 if (ast is! MalList) return eval_ast(ast, env);
119 if ((ast as MalList).isEmpty) return ast;
120
121 var list = ast as MalList;
122
123 if (list.elements.first is MalSymbol) {
124 var symbol = list.elements.first as MalSymbol;
125 var args = list.elements.sublist(1);
126 if (symbol.value == "def!") {
127 MalSymbol key = args.first;
128 MalType value = EVAL(args[1], env);
129 env.set(key, value);
130 return value;
131 } else if (symbol.value == "defmacro!") {
132 MalSymbol key = args.first;
133 MalClosure macro = EVAL(args[1], env) as MalClosure;
134 macro.isMacro = true;
135 env.set(key, macro);
136 return macro;
137 } else if (symbol.value == "let*") {
138 // TODO(het): If elements.length is not even, give helpful error
139 Iterable<List<MalType>> pairs(List<MalType> elements) sync* {
140 for (var i = 0; i < elements.length; i += 2) {
141 yield [elements[i], elements[i + 1]];
142 }
143 }
144
145 var newEnv = new Env(env);
146 MalIterable bindings = args.first;
147 for (var pair in pairs(bindings.elements)) {
148 MalSymbol key = pair[0];
149 MalType value = EVAL(pair[1], newEnv);
150 newEnv.set(key, value);
151 }
152 ast = args[1];
153 env = newEnv;
154 continue;
155 } else if (symbol.value == "do") {
156 eval_ast(new MalList(args.sublist(0, args.length - 1)), env);
157 ast = args.last;
158 continue;
159 } else if (symbol.value == "if") {
160 var condition = EVAL(args[0], env);
161 if (condition is MalNil ||
162 condition is MalBool && condition.value == false) {
163 // False side of branch
164 if (args.length < 3) {
165 return new MalNil();
166 }
167 ast = args[2];
168 continue;
169 } else {
170 // True side of branch
171 ast = args[1];
172 continue;
173 }
174 } else if (symbol.value == "fn*") {
175 var params = (args[0] as MalIterable)
176 .elements
177 .map((e) => e as MalSymbol)
178 .toList();
179 return new MalClosure(
180 params,
181 args[1],
182 env,
183 (List<MalType> funcArgs) =>
184 EVAL(args[1], new Env(env, params, funcArgs)));
185 } else if (symbol.value == "quote") {
186 return args.single;
187 } else if (symbol.value == "quasiquote") {
188 ast = quasiquote(args.first);
189 continue;
190 } else if (symbol.value == 'macroexpand') {
191 return macroexpand(args.first, env);
192 }
193 }
194 var newAst = eval_ast(ast, env) as MalList;
195 var f = newAst.elements.first;
196 var args = newAst.elements.sublist(1);
197 if (f is MalBuiltin) {
198 return f.call(args);
199 } else if (f is MalClosure) {
200 ast = f.ast;
201 env = new Env(f.env, f.params, args);
202 continue;
203 } else {
204 throw 'bad!';
205 }
206 }
207 }
208 }
209 }
210
211 String PRINT(MalType x) => printer.pr_str(x);
212
213 String rep(String x) {
214 return PRINT(EVAL(READ(x), replEnv));
215 }
216
217 const prompt = 'user> ';
218 main(List<String> args) {
219 setupEnv(args.isEmpty ? const <String>[] : args.sublist(1));
220 if (args.isNotEmpty) {
221 rep("(load-file \"${args.first}\")");
222 return;
223 }
224 while (true) {
225 stdout.write(prompt);
226 var input = stdin.readLineSync();
227 if (input == null) return;
228 var output;
229 try {
230 output = rep(input);
231 } on reader.ParseException catch (e) {
232 stdout.writeln("Error: '${e.message}'");
233 continue;
234 } on NotFoundException catch (e) {
235 stdout.writeln("Error: '${e.value}' not found");
236 continue;
237 } on MalException catch (e) {
238 stdout.writeln("Error: ${printer.pr_str(e.value)}");
239 continue;
240 } on reader.NoInputException {
241 continue;
242 }
243 stdout.writeln(output);
244 }
245 }