All: move some fns to core. Major cleanup.
[jackhill/mal.git] / js / step5_tco.js
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 eval_ast(ast, env) {
17 if (types._symbol_Q(ast)) {
18 return env.get(ast);
19 } else if (types._list_Q(ast)) {
20 return ast.map(function(a) { return EVAL(a, env); });
21 } else if (types._vector_Q(ast)) {
22 var v = ast.map(function(a) { return EVAL(a, env); });
23 v.__isvector__ = true;
24 return v;
25 } else if (types._hash_map_Q(ast)) {
26 var new_hm = {};
27 for (k in ast) {
28 new_hm[EVAL(k, env)] = EVAL(ast[k], env);
29 }
30 return new_hm;
31 } else {
32 return ast;
33 }
34 }
35
36 function _EVAL(ast, env) {
37 while (true) {
38
39 //printer.println("EVAL:", types._pr_str(ast, true));
40 if (!types._list_Q(ast)) {
41 return eval_ast(ast, env);
42 }
43
44 // apply list
45 var a0 = ast[0], a1 = ast[1], a2 = ast[2], a3 = ast[3];
46 switch (a0.value) {
47 case "def!":
48 var res = EVAL(a2, env);
49 return env.set(a1, res);
50 case "let*":
51 var let_env = new Env(env);
52 for (var i=0; i < a1.length; i+=2) {
53 let_env.set(a1[i].value, EVAL(a1[i+1], let_env));
54 }
55 return EVAL(a2, let_env);
56 case "do":
57 eval_ast(ast.slice(1, -1), env);
58 ast = ast[ast.length-1];
59 break;
60 case "if":
61 var cond = EVAL(a1, env);
62 if (cond === null || cond === false) {
63 ast = (typeof a3 !== "undefined") ? a3 : null;
64 } else {
65 ast = a2;
66 }
67 break;
68 case "fn*":
69 return types._function(EVAL, Env, a2, env, a1);
70 default:
71 var el = eval_ast(ast, env), f = el[0];
72 if (f.__ast__) {
73 ast = f.__ast__;
74 env = f.__gen_env__(el.slice(1));
75 } else {
76 return f.apply(f, el.slice(1));
77 }
78 }
79
80 }
81 }
82
83 function EVAL(ast, env) {
84 var result = _EVAL(ast, env);
85 return (typeof result !== "undefined") ? result : null;
86 }
87
88 // print
89 function PRINT(exp) {
90 return printer._pr_str(exp, true);
91 }
92
93 // repl
94 var repl_env = new Env();
95 var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); };
96
97 // core.js: defined using javascript
98 for (var n in core.ns) { repl_env.set(n, core.ns[n]); }
99 repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); });
100
101 // core.mal: defined using the language itself
102 rep("(def! not (fn* (a) (if a false true)))");
103
104 if (typeof require === 'undefined') {
105 // Asynchronous browser mode
106 readline.rlwrap(function(line) { return rep(line); },
107 function(exc) {
108 if (exc instanceof reader.BlankException) { return; }
109 if (exc.stack) { printer.println(exc.stack); }
110 else { printer.println(exc); }
111 });
112 } else if (require.main === module) {
113 // Synchronous node.js commandline mode
114 while (true) {
115 var line = readline.readline("user> ");
116 if (line === null) { break; }
117 try {
118 if (line) { printer.println(rep(line)); }
119 } catch (exc) {
120 if (exc instanceof reader.BlankException) { continue; }
121 if (exc.stack) { printer.println(exc.stack); }
122 else { printer.println(exc); }
123 }
124 }
125 } else {
126 exports.rep = rep;
127 }