5 import 'printer.dart' as printer;
6 import 'reader.dart' as reader;
9 final Env replEnv = new Env();
11 void setupEnv(List<String> argv) {
12 ns.forEach((sym, fun) => replEnv.set(sym, fun));
14 replEnv.set(new MalSymbol('eval'),
15 new MalBuiltin((List<MalType> args) => EVAL(args.single, replEnv)));
17 replEnv.set(new MalSymbol('*ARGV*'),
18 new MalList(argv.map((s) => new MalString(s)).toList()));
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) "
28 " (throw \"odd number of forms to cond\")) "
29 " (cons 'cond (rest (rest xs)))))))");
32 /// Returns `true` if [ast] is a macro call.
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) {
38 if (ast.isNotEmpty && ast.first is MalSymbol) {
40 var value = env.get(ast.first);
41 if (value is MalCallable) {
44 } on NotFoundException {
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));
61 MalType quasiquote(MalType ast) {
62 bool isPair(MalType ast) {
63 return ast is MalIterable && ast.isNotEmpty;
67 return new MalList([new MalSymbol("quote"), ast]);
69 var list = ast as MalIterable;
70 if (list.first == new MalSymbol("unquote")) {
72 } else if (isPair(list.first) &&
73 (list.first as MalIterable).first == new MalSymbol("splice-unquote")) {
75 new MalSymbol("concat"),
76 (list.first as MalIterable)[1],
77 quasiquote(new MalList(list.sublist(1)))
81 new MalSymbol("cons"),
83 quasiquote(new MalList(list.sublist(1)))
89 MalType READ(String x) => reader.read_str(x);
91 MalType eval_ast(MalType ast, Env env) {
92 if (ast is MalSymbol) {
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);
103 return new MalHashMap(newMap);
109 MalType EVAL(MalType ast, Env env) {
111 if (ast is! MalList) {
112 return eval_ast(ast, env);
114 if ((ast as MalList).elements.isEmpty) {
117 ast = macroexpand(ast, env);
118 if (ast is! MalList) return eval_ast(ast, env);
119 if ((ast as MalList).isEmpty) return ast;
121 var list = ast as MalList;
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);
131 } else if (symbol.value == "defmacro!") {
132 MalSymbol key = args.first;
133 MalClosure macro = EVAL(args[1], env) as MalClosure;
134 macro.isMacro = true;
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]];
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);
155 } else if (symbol.value == "do") {
156 eval_ast(new MalList(args.sublist(0, args.length - 1)), env);
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) {
170 // True side of branch
174 } else if (symbol.value == "fn*") {
175 var params = (args[0] as MalIterable)
177 .map((e) => e as MalSymbol)
179 return new MalClosure(
183 (List<MalType> funcArgs) =>
184 EVAL(args[1], new Env(env, params, funcArgs)));
185 } else if (symbol.value == "quote") {
187 } else if (symbol.value == "quasiquote") {
188 ast = quasiquote(args.first);
190 } else if (symbol.value == 'macroexpand') {
191 return macroexpand(args.first, env);
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) {
199 } else if (f is MalClosure) {
201 env = new Env(f.env, f.params, args);
211 String PRINT(MalType x) => printer.pr_str(x);
213 String rep(String x) {
214 return PRINT(EVAL(READ(x), replEnv));
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}\")");
225 stdout.write(prompt);
226 var input = stdin.readLineSync();
227 if (input == null) return;
231 } on reader.ParseException catch (e) {
232 stdout.writeln("Error: '${e.message}'");
234 } on NotFoundException catch (e) {
235 stdout.writeln("Error: '${e.value}' not found");
237 } on MalException catch (e) {
238 stdout.writeln("Error: ${printer.pr_str(e.value)}");
240 } on reader.NoInputException {
243 stdout.writeln(output);