| 1 | module main; |
| 2 | |
| 3 | import std.algorithm; |
| 4 | import std.array; |
| 5 | import std.range; |
| 6 | import std.stdio; |
| 7 | import std.string; |
| 8 | import std.c.process; |
| 9 | import env; |
| 10 | import mal_core; |
| 11 | import readline; |
| 12 | import reader; |
| 13 | import printer; |
| 14 | import types; |
| 15 | |
| 16 | bool 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 | |
| 23 | MalType 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 | |
| 48 | bool 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 | |
| 62 | MalType 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 | |
| 76 | MalType READ(string str) |
| 77 | { |
| 78 | return read_str(str); |
| 79 | } |
| 80 | |
| 81 | MalType 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 | |
| 116 | MalType 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 "try*": |
| 172 | MalType exc; |
| 173 | try |
| 174 | { |
| 175 | return EVAL(aste[1], env); |
| 176 | } |
| 177 | catch (MalException e) |
| 178 | { |
| 179 | exc = e.data; |
| 180 | } |
| 181 | catch (Exception e) |
| 182 | { |
| 183 | exc = new MalString(e.msg); |
| 184 | } |
| 185 | if (aste.length < 3) return mal_nil; |
| 186 | auto catch_clause = verify_cast!MalList(aste[2]); |
| 187 | auto catch_env = new Env(env, [catch_clause.elements[1]], [exc]); |
| 188 | return EVAL(catch_clause.elements[2], catch_env); |
| 189 | |
| 190 | case "do": |
| 191 | auto all_but_last = new MalList(aste[1..$-1]); |
| 192 | eval_ast(all_but_last, env); |
| 193 | ast = aste[$-1]; |
| 194 | continue; // TCO |
| 195 | |
| 196 | case "if": |
| 197 | auto cond = EVAL(aste[1], env); |
| 198 | if (cond.is_truthy()) |
| 199 | { |
| 200 | ast = aste[2]; |
| 201 | continue; // TCO |
| 202 | } |
| 203 | else |
| 204 | if (aste.length > 3) |
| 205 | { |
| 206 | ast = aste[3]; |
| 207 | continue; // TCO |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | return mal_nil; |
| 212 | } |
| 213 | |
| 214 | case "fn*": |
| 215 | auto args_list = verify_cast!MalSequential(aste[1]); |
| 216 | return new MalFunc(args_list.elements, aste[2], env); |
| 217 | |
| 218 | default: |
| 219 | auto el = verify_cast!MalList(eval_ast(ast, env)); |
| 220 | if (el.elements.length == 0) |
| 221 | { |
| 222 | throw new Exception("Expected a non-empty list"); |
| 223 | } |
| 224 | auto first = el.elements[0]; |
| 225 | auto rest = el.elements[1..$]; |
| 226 | if (typeid(first) == typeid(MalFunc)) |
| 227 | { |
| 228 | auto funcobj = verify_cast!MalFunc(first); |
| 229 | auto callenv = new Env(funcobj.def_env, funcobj.arg_names, rest); |
| 230 | ast = funcobj.func_body; |
| 231 | env = callenv; |
| 232 | continue; // TCO |
| 233 | } |
| 234 | else if (typeid(first) == typeid(MalBuiltinFunc)) |
| 235 | { |
| 236 | auto builtinfuncobj = verify_cast!MalBuiltinFunc(first); |
| 237 | return builtinfuncobj.fn(rest); |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | throw new Exception("Expected a function"); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | string PRINT(MalType ast) |
| 248 | { |
| 249 | return pr_str(ast); |
| 250 | } |
| 251 | |
| 252 | MalType re(string str, Env env) |
| 253 | { |
| 254 | return EVAL(READ(str), env); |
| 255 | } |
| 256 | |
| 257 | string rep(string str, Env env) |
| 258 | { |
| 259 | return PRINT(re(str, env)); |
| 260 | } |
| 261 | |
| 262 | static MalList create_argv_list(string[] args) |
| 263 | { |
| 264 | if (args.length <= 2) return new MalList([]); |
| 265 | return new MalList(array(args[2..$].map!(s => cast(MalType)(new MalString(s))))); |
| 266 | } |
| 267 | |
| 268 | void main(string[] args) |
| 269 | { |
| 270 | Env repl_env = new Env(null); |
| 271 | foreach (string sym_name, BuiltinStaticFuncType f; core_ns) |
| 272 | { |
| 273 | repl_env.set(new MalSymbol(sym_name), new MalBuiltinFunc(f, sym_name)); |
| 274 | } |
| 275 | |
| 276 | BuiltinFuncType eval_func = (a ...) { |
| 277 | verify_args_count(a, 1); |
| 278 | return EVAL(a[0], repl_env); |
| 279 | }; |
| 280 | repl_env.set(new MalSymbol("eval"), new MalBuiltinFunc(eval_func, "eval")); |
| 281 | repl_env.set(new MalSymbol("*ARGV*"), create_argv_list(args)); |
| 282 | |
| 283 | // core.mal: defined using the language itself |
| 284 | re("(def! *host-language* \"d\")", repl_env); |
| 285 | re("(def! not (fn* (a) (if a false true)))", repl_env); |
| 286 | re("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", repl_env); |
| 287 | 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); |
| 288 | 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); |
| 289 | |
| 290 | if (args.length > 1) |
| 291 | { |
| 292 | try |
| 293 | { |
| 294 | rep("(load-file \"" ~ args[1] ~ "\")", repl_env); |
| 295 | return; |
| 296 | } |
| 297 | catch (Exception e) |
| 298 | { |
| 299 | writeln("Error: ", e.msg); |
| 300 | std.c.process.exit(1); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | re("(println (str \"Mal [\" *host-language* \"]\"))", repl_env); |
| 305 | for (;;) |
| 306 | { |
| 307 | string line = _readline("user> "); |
| 308 | if (line is null) break; |
| 309 | if (line.length == 0) continue; |
| 310 | try |
| 311 | { |
| 312 | writeln(rep(line, repl_env)); |
| 313 | } |
| 314 | catch (Exception e) |
| 315 | { |
| 316 | writeln("Error: ", e.msg); |
| 317 | } |
| 318 | } |
| 319 | writeln(""); |
| 320 | } |