| 1 | if (typeof module !== 'undefined') { |
| 2 | var types = require('./types'); |
| 3 | var readline = require('./node_readline'); |
| 4 | var reader = require('./reader'); |
| 5 | var printer = require('./printer'); |
| 6 | var Env = require('./env').Env; |
| 7 | var core = require('./core'); |
| 8 | } |
| 9 | |
| 10 | // read |
| 11 | function READ(str) { |
| 12 | return reader.read_str(str); |
| 13 | } |
| 14 | |
| 15 | // eval |
| 16 | function is_pair(x) { |
| 17 | return types._sequential_Q(x) && x.length > 0; |
| 18 | } |
| 19 | |
| 20 | function quasiquote(ast) { |
| 21 | if (!is_pair(ast)) { |
| 22 | return [types._symbol("quote"), ast]; |
| 23 | } else if (types._symbol_Q(ast[0]) && ast[0].value === 'unquote') { |
| 24 | return ast[1]; |
| 25 | } else if (is_pair(ast[0]) && ast[0][0].value === 'splice-unquote') { |
| 26 | return [types._symbol("concat"), |
| 27 | ast[0][1], |
| 28 | quasiquote(ast.slice(1))]; |
| 29 | } else { |
| 30 | return [types._symbol("cons"), |
| 31 | quasiquote(ast[0]), |
| 32 | quasiquote(ast.slice(1))]; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | function is_macro_call(ast, env) { |
| 37 | return types._list_Q(ast) && |
| 38 | types._symbol_Q(ast[0]) && |
| 39 | env.find(ast[0]) && |
| 40 | env.get(ast[0])._ismacro_; |
| 41 | } |
| 42 | |
| 43 | function macroexpand(ast, env) { |
| 44 | while (is_macro_call(ast, env)) { |
| 45 | var mac = env.get(ast[0]); |
| 46 | ast = mac.apply(mac, ast.slice(1)); |
| 47 | } |
| 48 | return ast; |
| 49 | } |
| 50 | |
| 51 | function eval_ast(ast, env) { |
| 52 | if (types._symbol_Q(ast)) { |
| 53 | return env.get(ast); |
| 54 | } else if (types._list_Q(ast)) { |
| 55 | return ast.map(function(a) { return EVAL(a, env); }); |
| 56 | } else if (types._vector_Q(ast)) { |
| 57 | var v = ast.map(function(a) { return EVAL(a, env); }); |
| 58 | v.__isvector__ = true; |
| 59 | return v; |
| 60 | } else if (types._hash_map_Q(ast)) { |
| 61 | var new_hm = {}; |
| 62 | for (k in ast) { |
| 63 | new_hm[EVAL(k, env)] = EVAL(ast[k], env); |
| 64 | } |
| 65 | return new_hm; |
| 66 | } else { |
| 67 | return ast; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | function _EVAL(ast, env) { |
| 72 | while (true) { |
| 73 | |
| 74 | //printer.println("EVAL:", printer._pr_str(ast, true)); |
| 75 | if (!types._list_Q(ast)) { |
| 76 | return eval_ast(ast, env); |
| 77 | } |
| 78 | |
| 79 | // apply list |
| 80 | ast = macroexpand(ast, env); |
| 81 | if (!types._list_Q(ast)) { |
| 82 | return eval_ast(ast, env); |
| 83 | } |
| 84 | if (ast.length === 0) { |
| 85 | return ast; |
| 86 | } |
| 87 | |
| 88 | var a0 = ast[0], a1 = ast[1], a2 = ast[2], a3 = ast[3]; |
| 89 | switch (a0.value) { |
| 90 | case "def!": |
| 91 | var res = EVAL(a2, env); |
| 92 | return env.set(a1, res); |
| 93 | case "let*": |
| 94 | var let_env = new Env(env); |
| 95 | for (var i=0; i < a1.length; i+=2) { |
| 96 | let_env.set(a1[i], EVAL(a1[i+1], let_env)); |
| 97 | } |
| 98 | ast = a2; |
| 99 | env = let_env; |
| 100 | break; |
| 101 | case "quote": |
| 102 | return a1; |
| 103 | case "quasiquote": |
| 104 | ast = quasiquote(a1); |
| 105 | break; |
| 106 | case 'defmacro!': |
| 107 | var func = EVAL(a2, env); |
| 108 | func._ismacro_ = true; |
| 109 | return env.set(a1, func); |
| 110 | case 'macroexpand': |
| 111 | return macroexpand(a1, env); |
| 112 | case "do": |
| 113 | eval_ast(ast.slice(1, -1), env); |
| 114 | ast = ast[ast.length-1]; |
| 115 | break; |
| 116 | case "if": |
| 117 | var cond = EVAL(a1, env); |
| 118 | if (cond === null || cond === false) { |
| 119 | ast = (typeof a3 !== "undefined") ? a3 : null; |
| 120 | } else { |
| 121 | ast = a2; |
| 122 | } |
| 123 | break; |
| 124 | case "fn*": |
| 125 | return types._function(EVAL, Env, a2, env, a1); |
| 126 | default: |
| 127 | var el = eval_ast(ast, env), f = el[0]; |
| 128 | if (f.__ast__) { |
| 129 | ast = f.__ast__; |
| 130 | env = f.__gen_env__(el.slice(1)); |
| 131 | } else { |
| 132 | return f.apply(f, el.slice(1)); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | function EVAL(ast, env) { |
| 140 | var result = _EVAL(ast, env); |
| 141 | return (typeof result !== "undefined") ? result : null; |
| 142 | } |
| 143 | |
| 144 | // print |
| 145 | function PRINT(exp) { |
| 146 | return printer._pr_str(exp, true); |
| 147 | } |
| 148 | |
| 149 | // repl |
| 150 | var repl_env = new Env(); |
| 151 | var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; |
| 152 | |
| 153 | // core.js: defined using javascript |
| 154 | for (var n in core.ns) { repl_env.set(types._symbol(n), core.ns[n]); } |
| 155 | repl_env.set(types._symbol('eval'), function(ast) { |
| 156 | return EVAL(ast, repl_env); }); |
| 157 | repl_env.set(types._symbol('*ARGV*'), []); |
| 158 | |
| 159 | // core.mal: defined using the language itself |
| 160 | rep("(def! not (fn* (a) (if a false true)))"); |
| 161 | rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); |
| 162 | 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)))))))"); |
| 163 | rep("(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))))))))"); |
| 164 | |
| 165 | if (typeof process !== 'undefined' && process.argv.length > 2) { |
| 166 | repl_env.set(types._symbol('*ARGV*'), process.argv.slice(3)); |
| 167 | rep('(load-file "' + process.argv[2] + '")'); |
| 168 | process.exit(0); |
| 169 | } |
| 170 | |
| 171 | // repl loop |
| 172 | if (typeof require !== 'undefined' && require.main === module) { |
| 173 | // Synchronous node.js commandline mode |
| 174 | while (true) { |
| 175 | var line = readline.readline("user> "); |
| 176 | if (line === null) { break; } |
| 177 | try { |
| 178 | if (line) { printer.println(rep(line)); } |
| 179 | } catch (exc) { |
| 180 | if (exc instanceof reader.BlankException) { continue; } |
| 181 | if (exc.stack) { printer.println(exc.stack); } |
| 182 | else { printer.println(exc); } |
| 183 | } |
| 184 | } |
| 185 | } |