Perl: add vector, hash-map, metadata, atom support. TCO let*
[jackhill/mal.git] / js / step8_macros.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];
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
36function 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
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);
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 }
ea81a808
JM
93 return EVAL(a2, let_env);
94 case "quote":
95 return a1;
96 case "quasiquote":
97 return EVAL(quasiquote(a1), env);
98 case 'defmacro!':
99 var func = EVAL(a2, env);
100 func._ismacro_ = true;
101 return env.set(a1, func);
102 case 'macroexpand':
103 return macroexpand(a1, env);
104 case "do":
105 eval_ast(ast.slice(1, -1), env);
106 ast = ast[ast.length-1];
107 break;
108 case "if":
109 var cond = EVAL(a1, env);
110 if (cond === null || cond === false) {
111 ast = (typeof a3 !== "undefined") ? a3 : null;
112 } else {
113 ast = a2;
31690700 114 }
ea81a808
JM
115 break;
116 case "fn*":
117 return types._function(EVAL, Env, a2, env, a1);
118 default:
8adb0827 119 var el = eval_ast(ast, env), f = el[0];
a34b0200
JM
120 if (f.__ast__) {
121 ast = f.__ast__;
122 env = f.__gen_env__(el.slice(1));
ea81a808
JM
123 } else {
124 return f.apply(f, el.slice(1));
125 }
126 }
127
31690700
JM
128 }
129}
130
131function EVAL(ast, env) {
132 var result = _EVAL(ast, env);
133 return (typeof result !== "undefined") ? result : null;
134}
135
136// print
137function PRINT(exp) {
ea81a808 138 return printer._pr_str(exp, true);
31690700
JM
139}
140
141// repl
ea81a808 142var repl_env = new Env();
31690700 143var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); };
31690700 144
8cb5cda4 145// core.js: defined using javascript
ea81a808 146for (var n in core.ns) { repl_env.set(n, core.ns[n]); }
8cb5cda4 147repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); });
86b689f3 148repl_env.set('*ARGV*', []);
31690700 149
8cb5cda4 150// core.mal: defined using the language itself
31690700 151rep("(def! not (fn* (a) (if a false true)))");
1617910a 152rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
8cb5cda4
JM
153rep("(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)))))))");
154rep("(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
155
156if (typeof process !== 'undefined' && process.argv.length > 2) {
86b689f3
JM
157 repl_env.set('*ARGV*', process.argv.slice(3));
158 rep('(load-file "' + process.argv[2] + '")');
159 process.exit(0);
160}
161
162// repl loop
163if (typeof require === 'undefined') {
31690700
JM
164 // Asynchronous browser mode
165 readline.rlwrap(function(line) { return rep(line); },
166 function(exc) {
167 if (exc instanceof reader.BlankException) { return; }
31b44161
JM
168 if (exc.stack) { printer.println(exc.stack); }
169 else { printer.println(exc); }
31690700
JM
170 });
171} else if (require.main === module) {
172 // Synchronous node.js commandline mode
173 while (true) {
174 var line = readline.readline("user> ");
175 if (line === null) { break; }
176 try {
31b44161 177 if (line) { printer.println(rep(line)); }
31690700
JM
178 } catch (exc) {
179 if (exc instanceof reader.BlankException) { continue; }
31b44161
JM
180 if (exc.stack) { printer.println(exc.stack); }
181 else { printer.println(exc); }
31690700
JM
182 }
183 }
184} else {
185 exports.rep = rep;
186}