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]; |
31690700 JM |
23 | } else if (ast[0].value === 'unquote') { |
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]) && | |
31690700 JM |
39 | env.find(ast[0].value) && |
40 | env.get(ast[0].value)._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) { | |
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); | |
81 | if (!types._list_Q(ast)) { return ast; } | |
82 | ||
83 | var a0 = ast[0], a1 = ast[1], a2 = ast[2], a3 = ast[3]; | |
84 | switch (a0.value) { | |
85 | case "def!": | |
86 | var res = EVAL(a2, env); | |
87 | return env.set(a1, res); | |
88 | case "let*": | |
89 | var let_env = new Env(env); | |
90 | for (var i=0; i < a1.length; i+=2) { | |
91 | let_env.set(a1[i].value, EVAL(a1[i+1], let_env)); | |
31690700 | 92 | } |
6301e0b6 JM |
93 | ast = a2; |
94 | env = let_env; | |
95 | break; | |
ea81a808 JM |
96 | case "quote": |
97 | return a1; | |
98 | case "quasiquote": | |
6301e0b6 JM |
99 | ast = quasiquote(a1); |
100 | break; | |
ea81a808 JM |
101 | case 'defmacro!': |
102 | var func = EVAL(a2, env); | |
103 | func._ismacro_ = true; | |
104 | return env.set(a1, func); | |
105 | case 'macroexpand': | |
106 | return macroexpand(a1, env); | |
107 | case "js*": | |
108 | return eval(a1.toString()); | |
109 | case ".": | |
110 | var el = eval_ast(ast.slice(2), env), | |
111 | f = eval(a1.toString()); | |
112 | return f.apply(f, el); | |
113 | case "try*": | |
114 | try { | |
115 | return EVAL(a1, env); | |
116 | } catch (exc) { | |
117 | if (a2 && a2[0].value === "catch*") { | |
118 | if (exc instanceof Error) { exc = exc.message; } | |
119 | return EVAL(a2[2], new Env(env, [a2[1]], [exc])); | |
31690700 | 120 | } else { |
ea81a808 | 121 | throw exc; |
31690700 JM |
122 | } |
123 | } | |
ea81a808 JM |
124 | case "do": |
125 | eval_ast(ast.slice(1, -1), env); | |
126 | ast = ast[ast.length-1]; | |
127 | break; | |
128 | case "if": | |
129 | var cond = EVAL(a1, env); | |
130 | if (cond === null || cond === false) { | |
131 | ast = (typeof a3 !== "undefined") ? a3 : null; | |
132 | } else { | |
133 | ast = a2; | |
134 | } | |
135 | break; | |
136 | case "fn*": | |
137 | return types._function(EVAL, Env, a2, env, a1); | |
138 | default: | |
8adb0827 | 139 | var el = eval_ast(ast, env), f = el[0]; |
a34b0200 JM |
140 | if (f.__ast__) { |
141 | ast = f.__ast__; | |
142 | env = f.__gen_env__(el.slice(1)); | |
ea81a808 JM |
143 | } else { |
144 | return f.apply(f, el.slice(1)); | |
145 | } | |
146 | } | |
147 | ||
31690700 JM |
148 | } |
149 | } | |
150 | ||
151 | function EVAL(ast, env) { | |
152 | var result = _EVAL(ast, env); | |
153 | return (typeof result !== "undefined") ? result : null; | |
154 | } | |
155 | ||
156 | ||
157 | function PRINT(exp) { | |
ea81a808 | 158 | return printer._pr_str(exp, true); |
31690700 JM |
159 | } |
160 | ||
161 | // repl | |
ea81a808 | 162 | var repl_env = new Env(); |
31690700 | 163 | var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; |
31690700 | 164 | |
8cb5cda4 | 165 | // core.js: defined using javascript |
ea81a808 | 166 | for (var n in core.ns) { repl_env.set(n, core.ns[n]); } |
8cb5cda4 | 167 | repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); |
86b689f3 | 168 | repl_env.set('*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)))"); |
8cb5cda4 | 173 | rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); |
31690700 JM |
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)))))))"); |
175 | 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))))))))"); | |
31690700 JM |
176 | |
177 | if (typeof process !== 'undefined' && process.argv.length > 2) { | |
86b689f3 JM |
178 | repl_env.set('*ARGV*', process.argv.slice(3)); |
179 | rep('(load-file "' + process.argv[2] + '")'); | |
180 | process.exit(0); | |
181 | } | |
182 | ||
183 | // repl loop | |
e4393504 | 184 | if (typeof require !== 'undefined' && require.main === module) { |
31690700 | 185 | // Synchronous node.js commandline mode |
e4393504 | 186 | rep("(println (str \"Mal [\" *host-language* \"]\"))"); |
31690700 JM |
187 | while (true) { |
188 | var line = readline.readline("user> "); | |
189 | if (line === null) { break; } | |
190 | try { | |
31b44161 | 191 | if (line) { printer.println(rep(line)); } |
31690700 JM |
192 | } catch (exc) { |
193 | if (exc instanceof reader.BlankException) { continue; } | |
31b44161 JM |
194 | if (exc.stack) { printer.println(exc.stack); } |
195 | else { printer.println(exc); } | |
31690700 JM |
196 | } |
197 | } | |
31690700 | 198 | } |