Merge pull request #273 from wasamasa/r7rs-implementation
[jackhill/mal.git] / dart / step9_try.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) \")\")))))");
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 rep("(defmacro! or "
31 " (fn* (& xs) (if (empty? xs) nil "
32 " (if (= 1 (count xs)) "
33 " (first xs) "
34 " `(let* (or_FIXME ~(first xs)) "
35 " (if or_FIXME or_FIXME (or ~@(rest xs))))))))");
36 }
37
38 /// Returns `true` if [ast] is a macro call.
39 ///
40 /// This checks that [ast] is a list whose first element is a symbol that refers
41 /// to a function in the current [env] that is a macro.
42 bool isMacroCall(MalType ast, Env env) {
43 if (ast is MalList) {
44 if (ast.isNotEmpty && ast.first is MalSymbol) {
45 try {
46 var value = env.get(ast.first);
47 if (value is MalCallable) {
48 return value.isMacro;
49 }
50 } on NotFoundException {
51 return false;
52 }
53 }
54 }
55 return false;
56 }
57
58 MalType macroexpand(MalType ast, Env env) {
59 while (isMacroCall(ast, env)) {
60 var macroSymbol = (ast as MalList).first;
61 var macro = env.get(macroSymbol) as MalCallable;
62 ast = macro((ast as MalList).sublist(1));
63 }
64 return ast;
65 }
66
67 MalType quasiquote(MalType ast) {
68 bool isPair(MalType ast) {
69 return ast is MalIterable && ast.isNotEmpty;
70 }
71
72 if (!isPair(ast)) {
73 return new MalList([new MalSymbol("quote"), ast]);
74 } else {
75 var list = ast as MalIterable;
76 if (list.first == new MalSymbol("unquote")) {
77 return list[1];
78 } else if (isPair(list.first) &&
79 (list.first as MalIterable).first == new MalSymbol("splice-unquote")) {
80 return new MalList([
81 new MalSymbol("concat"),
82 (list.first as MalIterable)[1],
83 quasiquote(new MalList(list.sublist(1)))
84 ]);
85 } else {
86 return new MalList([
87 new MalSymbol("cons"),
88 quasiquote(list[0]),
89 quasiquote(new MalList(list.sublist(1)))
90 ]);
91 }
92 }
93 }
94
95 MalType READ(String x) => reader.read_str(x);
96
97 MalType eval_ast(MalType ast, Env env) {
98 if (ast is MalSymbol) {
99 return env.get(ast);
100 } else if (ast is MalList) {
101 return new MalList(ast.elements.map((x) => EVAL(x, env)).toList());
102 } else if (ast is MalVector) {
103 return new MalVector(ast.elements.map((x) => EVAL(x, env)).toList());
104 } else if (ast is MalHashMap) {
105 var newMap = new Map<MalType, MalType>.from(ast.value);
106 for (var key in newMap.keys) {
107 newMap[key] = EVAL(newMap[key], env);
108 }
109 return new MalHashMap(newMap);
110 } else {
111 return ast;
112 }
113 }
114
115 MalType EVAL(MalType ast, Env env) {
116 while (true) {
117 if (ast is! MalList) {
118 return eval_ast(ast, env);
119 } else {
120 if ((ast as MalList).elements.isEmpty) {
121 return ast;
122 } else {
123 ast = macroexpand(ast, env);
124 if (ast is! MalList) return eval_ast(ast, env);
125 if ((ast as MalList).isEmpty) return ast;
126
127 var list = ast as MalList;
128
129 if (list.elements.first is MalSymbol) {
130 var symbol = list.elements.first as MalSymbol;
131 var args = list.elements.sublist(1);
132 if (symbol.value == "def!") {
133 MalSymbol key = args.first;
134 MalType value = EVAL(args[1], env);
135 env.set(key, value);
136 return value;
137 } else if (symbol.value == "defmacro!") {
138 MalSymbol key = args.first;
139 MalClosure macro = EVAL(args[1], env) as MalClosure;
140 macro.isMacro = true;
141 env.set(key, macro);
142 return macro;
143 } else if (symbol.value == "let*") {
144 // TODO(het): If elements.length is not even, give helpful error
145 Iterable<List<MalType>> pairs(List<MalType> elements) sync* {
146 for (var i = 0; i < elements.length; i += 2) {
147 yield [elements[i], elements[i + 1]];
148 }
149 }
150
151 var newEnv = new Env(env);
152 MalIterable bindings = args.first;
153 for (var pair in pairs(bindings.elements)) {
154 MalSymbol key = pair[0];
155 MalType value = EVAL(pair[1], newEnv);
156 newEnv.set(key, value);
157 }
158 ast = args[1];
159 env = newEnv;
160 continue;
161 } else if (symbol.value == "do") {
162 eval_ast(new MalList(args.sublist(0, args.length - 1)), env);
163 ast = args.last;
164 continue;
165 } else if (symbol.value == "if") {
166 var condition = EVAL(args[0], env);
167 if (condition is MalNil ||
168 condition is MalBool && condition.value == false) {
169 // False side of branch
170 if (args.length < 3) {
171 return new MalNil();
172 }
173 ast = args[2];
174 continue;
175 } else {
176 // True side of branch
177 ast = args[1];
178 continue;
179 }
180 } else if (symbol.value == "fn*") {
181 var params = (args[0] as MalIterable)
182 .elements
183 .map((e) => e as MalSymbol)
184 .toList();
185 return new MalClosure(
186 params,
187 args[1],
188 env,
189 (List<MalType> funcArgs) =>
190 EVAL(args[1], new Env(env, params, funcArgs)));
191 } else if (symbol.value == "quote") {
192 return args.single;
193 } else if (symbol.value == "quasiquote") {
194 ast = quasiquote(args.first);
195 continue;
196 } else if (symbol.value == 'macroexpand') {
197 ast = macroexpand(args.first, env);
198 continue;
199 } else if (symbol.value == 'try*') {
200 var body = args.first;
201 var catchClause = args[1] as MalList;
202 try {
203 ast = EVAL(body, env);
204 } catch (e) {
205 assert((catchClause.first as MalSymbol).value == 'catch*');
206 var exceptionSymbol = catchClause[1] as MalSymbol;
207 var catchBody = catchClause[2];
208 MalType exceptionValue;
209 if (e is MalException) {
210 exceptionValue = e.value;
211 } else {
212 exceptionValue = new MalString(e.toString());
213 }
214 var newEnv = new Env(env, [exceptionSymbol], [exceptionValue]);
215 ast = EVAL(catchBody, newEnv);
216 }
217 continue;
218 }
219 }
220 var newAst = eval_ast(ast, env) as MalList;
221 var f = newAst.elements.first;
222 var args = newAst.elements.sublist(1);
223 if (f is MalBuiltin) {
224 return f.call(args);
225 } else if (f is MalClosure) {
226 ast = f.ast;
227 env = new Env(f.env, f.params, args);
228 continue;
229 } else {
230 throw 'bad!';
231 }
232 }
233 }
234 }
235 }
236
237 String PRINT(MalType x) => printer.pr_str(x);
238
239 String rep(String x) {
240 var parsed;
241 try {
242 parsed = READ(x);
243 } on reader.ParseException catch (e) {
244 return e.message;
245 }
246
247 var evaledAst;
248 try {
249 evaledAst = EVAL(parsed, replEnv);
250 } on NotFoundException catch (e) {
251 return "'${e.value}' not found";
252 } on MalNativeException catch (e) {
253 return "${e.error}";
254 }
255 return PRINT(evaledAst);
256 }
257
258 const prompt = 'user> ';
259 main(List<String> args) {
260 setupEnv(args.isEmpty ? const <String>[] : args.sublist(1));
261 if (args.isNotEmpty) {
262 rep("(load-file \"${args.first}\")");
263 return;
264 }
265 while (true) {
266 stdout.write(prompt);
267 var input = stdin.readLineSync();
268 if (input == null) return;
269 var output;
270 try {
271 output = rep(input);
272 } on reader.NoInputException {
273 continue;
274 }
275 stdout.writeln(output);
276 }
277 }