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