Commit | Line | Data |
---|---|---|
31690700 | 1 | if (typeof module !== 'undefined') { |
31b44161 | 2 | var types = require('./types'); |
31690700 | 3 | var readline = require('./node_readline'); |
31b44161 JM |
4 | var reader = require('./reader'); |
5 | var printer = require('./printer'); | |
6 | var Env = require('./env').Env; | |
7 | var core = require('./core'); | |
31690700 JM |
8 | } |
9 | ||
10 | // read | |
11 | function READ(str) { | |
12 | return reader.read_str(str); | |
13 | } | |
14 | ||
15 | // eval | |
16 | function is_pair(x) { | |
ea81a808 | 17 | return types._sequential_Q(x) && x.length > 0; |
31690700 JM |
18 | } |
19 | ||
20 | function quasiquote(ast) { | |
21 | if (!is_pair(ast)) { | |
ea81a808 | 22 | return [types._symbol("quote"), ast]; |
3dcaa6ce | 23 | } else if (types._symbol_Q(ast[0]) && ast[0].value === 'unquote') { |
31690700 JM |
24 | return ast[1]; |
25 | } else if (is_pair(ast[0]) && ast[0][0].value === 'splice-unquote') { | |
8adb0827 JM |
26 | return [types._symbol("concat"), |
27 | ast[0][1], | |
28 | quasiquote(ast.slice(1))]; | |
31690700 | 29 | } else { |
8adb0827 JM |
30 | return [types._symbol("cons"), |
31 | quasiquote(ast[0]), | |
32 | quasiquote(ast.slice(1))]; | |
31690700 JM |
33 | } |
34 | } | |
35 | ||
36 | function is_macro_call(ast, env) { | |
ea81a808 JM |
37 | return types._list_Q(ast) && |
38 | types._symbol_Q(ast[0]) && | |
b8ee29b2 JM |
39 | env.find(ast[0]) && |
40 | env.get(ast[0])._ismacro_; | |
31690700 JM |
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) { | |
ea81a808 | 52 | if (types._symbol_Q(ast)) { |
31690700 | 53 | return env.get(ast); |
ea81a808 | 54 | } else if (types._list_Q(ast)) { |
31690700 | 55 | return ast.map(function(a) { return EVAL(a, env); }); |
ea81a808 | 56 | } else if (types._vector_Q(ast)) { |
31690700 JM |
57 | var v = ast.map(function(a) { return EVAL(a, env); }); |
58 | v.__isvector__ = true; | |
59 | return v; | |
ea81a808 | 60 | } else if (types._hash_map_Q(ast)) { |
31690700 JM |
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) { | |
ea81a808 | 73 | |
86b689f3 | 74 | //printer.println("EVAL:", printer._pr_str(ast, true)); |
ea81a808 JM |
75 | if (!types._list_Q(ast)) { |
76 | return eval_ast(ast, env); | |
77 | } | |
78 | ||
79 | // apply list | |
80 | ast = macroexpand(ast, env); | |
bfa3dd35 DM |
81 | if (!types._list_Q(ast)) { |
82 | return eval_ast(ast, env); | |
83 | } | |
daf52a0a DM |
84 | if (ast.length === 0) { |
85 | return ast; | |
86 | } | |
ea81a808 JM |
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) { | |
b8ee29b2 | 96 | let_env.set(a1[i], EVAL(a1[i+1], let_env)); |
31690700 | 97 | } |
6301e0b6 JM |
98 | ast = a2; |
99 | env = let_env; | |
100 | break; | |
ea81a808 JM |
101 | case "quote": |
102 | return a1; | |
103 | case "quasiquote": | |
6301e0b6 JM |
104 | ast = quasiquote(a1); |
105 | break; | |
ea81a808 | 106 | case 'defmacro!': |
c1da7517 | 107 | var func = types._clone(EVAL(a2, env)); |
ea81a808 JM |
108 | func._ismacro_ = true; |
109 | return env.set(a1, func); | |
110 | case 'macroexpand': | |
111 | return macroexpand(a1, env); | |
ea81a808 JM |
112 | case "try*": |
113 | try { | |
114 | return EVAL(a1, env); | |
115 | } catch (exc) { | |
116 | if (a2 && a2[0].value === "catch*") { | |
117 | if (exc instanceof Error) { exc = exc.message; } | |
118 | return EVAL(a2[2], new Env(env, [a2[1]], [exc])); | |
31690700 | 119 | } else { |
ea81a808 | 120 | throw exc; |
31690700 JM |
121 | } |
122 | } | |
ea81a808 JM |
123 | case "do": |
124 | eval_ast(ast.slice(1, -1), env); | |
125 | ast = ast[ast.length-1]; | |
126 | break; | |
127 | case "if": | |
128 | var cond = EVAL(a1, env); | |
129 | if (cond === null || cond === false) { | |
130 | ast = (typeof a3 !== "undefined") ? a3 : null; | |
131 | } else { | |
132 | ast = a2; | |
133 | } | |
134 | break; | |
135 | case "fn*": | |
136 | return types._function(EVAL, Env, a2, env, a1); | |
137 | default: | |
8adb0827 | 138 | var el = eval_ast(ast, env), f = el[0]; |
a34b0200 JM |
139 | if (f.__ast__) { |
140 | ast = f.__ast__; | |
141 | env = f.__gen_env__(el.slice(1)); | |
ea81a808 JM |
142 | } else { |
143 | return f.apply(f, el.slice(1)); | |
144 | } | |
145 | } | |
146 | ||
31690700 JM |
147 | } |
148 | } | |
149 | ||
150 | function EVAL(ast, env) { | |
151 | var result = _EVAL(ast, env); | |
152 | return (typeof result !== "undefined") ? result : null; | |
153 | } | |
154 | ||
155 | ||
156 | function PRINT(exp) { | |
ea81a808 | 157 | return printer._pr_str(exp, true); |
31690700 JM |
158 | } |
159 | ||
160 | // repl | |
ea81a808 | 161 | var repl_env = new Env(); |
31690700 | 162 | var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; |
31690700 | 163 | |
8cb5cda4 | 164 | // core.js: defined using javascript |
b8ee29b2 JM |
165 | for (var n in core.ns) { repl_env.set(types._symbol(n), core.ns[n]); } |
166 | repl_env.set(types._symbol('eval'), function(ast) { | |
167 | return EVAL(ast, repl_env); }); | |
168 | repl_env.set(types._symbol('*ARGV*'), []); | |
31690700 | 169 | |
8cb5cda4 | 170 | // core.mal: defined using the language itself |
db4c329a | 171 | rep("(def! *host-language* \"javascript\")") |
31690700 | 172 | rep("(def! not (fn* (a) (if a false true)))"); |
e6d41de4 | 173 | rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))"); |
31690700 | 174 | 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)))))))"); |
31690700 JM |
175 | |
176 | if (typeof process !== 'undefined' && process.argv.length > 2) { | |
b8ee29b2 | 177 | repl_env.set(types._symbol('*ARGV*'), process.argv.slice(3)); |
86b689f3 JM |
178 | rep('(load-file "' + process.argv[2] + '")'); |
179 | process.exit(0); | |
180 | } | |
181 | ||
182 | // repl loop | |
e4393504 | 183 | if (typeof require !== 'undefined' && require.main === module) { |
31690700 | 184 | // Synchronous node.js commandline mode |
e4393504 | 185 | rep("(println (str \"Mal [\" *host-language* \"]\"))"); |
31690700 JM |
186 | while (true) { |
187 | var line = readline.readline("user> "); | |
188 | if (line === null) { break; } | |
189 | try { | |
31b44161 | 190 | if (line) { printer.println(rep(line)); } |
31690700 | 191 | } catch (exc) { |
dd7a4f55 JM |
192 | if (exc instanceof reader.BlankException) { continue } |
193 | if (exc instanceof Error) { console.warn(exc.stack) } | |
194 | else { console.warn("Error: " + printer._pr_str(exc, true)) } | |
31690700 JM |
195 | } |
196 | } | |
31690700 | 197 | } |