OCaml: put macro flag in metadata rather than special type field
[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:", printer._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], EVAL(a1[i+1], let_env));
54 }
55 ast = a2;
56 env = let_env;
57 break;
58 case "do":
59 eval_ast(ast.slice(1, -1), env);
60 ast = ast[ast.length-1];
61 break;
62 case "if":
63 var cond = EVAL(a1, env);
64 if (cond === null || cond === false) {
65 ast = (typeof a3 !== "undefined") ? a3 : null;
66 } else {
67 ast = a2;
68 }
69 break;
70 case "fn*":
71 return types._function(EVAL, Env, a2, env, a1);
72 default:
73 var el = eval_ast(ast, env), f = el[0];
74 if (f.__ast__) {
75 ast = f.__ast__;
76 env = f.__gen_env__(el.slice(1));
77 } else {
78 return f.apply(f, el.slice(1));
79 }
80 }
81
82 }
83 }
84
85 function EVAL(ast, env) {
86 var result = _EVAL(ast, env);
87 return (typeof result !== "undefined") ? result : null;
88 }
89
90 // print
91 function PRINT(exp) {
92 return printer._pr_str(exp, true);
93 }
94
95 // repl
96 var repl_env = new Env();
97 var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); };
98
99 // core.js: defined using javascript
100 for (var n in core.ns) { repl_env.set(types._symbol(n), core.ns[n]); }
101
102 // core.mal: defined using the language itself
103 rep("(def! not (fn* (a) (if a false true)))");
104
105 // repl loop
106 if (typeof require !== 'undefined' && require.main === module) {
107 // Synchronous node.js commandline mode
108 while (true) {
109 var line = readline.readline("user> ");
110 if (line === null) { break; }
111 try {
112 if (line) { printer.println(rep(line)); }
113 } catch (exc) {
114 if (exc instanceof reader.BlankException) { continue; }
115 if (exc.stack) { printer.println(exc.stack); }
116 else { printer.println(exc); }
117 }
118 }
119 }