DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / js / step6_file.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 if (ast.length === 0) {
44 return ast;
45 }
46
47 // apply list
48 var a0 = ast[0], a1 = ast[1], a2 = ast[2], a3 = ast[3];
49 switch (a0.value) {
50 case "def!":
51 var res = EVAL(a2, env);
52 return env.set(a1, res);
53 case "let*":
54 var let_env = new Env(env);
55 for (var i=0; i < a1.length; i+=2) {
56 let_env.set(a1[i], EVAL(a1[i+1], let_env));
57 }
58 ast = a2;
59 env = let_env;
60 break;
61 case "do":
62 eval_ast(ast.slice(1, -1), env);
63 ast = ast[ast.length-1];
64 break;
65 case "if":
66 var cond = EVAL(a1, env);
67 if (cond === null || cond === false) {
68 ast = (typeof a3 !== "undefined") ? a3 : null;
69 } else {
70 ast = a2;
71 }
72 break;
73 case "fn*":
74 return types._function(EVAL, Env, a2, env, a1);
75 default:
76 var el = eval_ast(ast, env), f = el[0];
77 if (f.__ast__) {
78 ast = f.__ast__;
79 env = f.__gen_env__(el.slice(1));
80 } else {
81 return f.apply(f, el.slice(1));
82 }
83 }
84
85 }
86 }
87
88 function EVAL(ast, env) {
89 var result = _EVAL(ast, env);
90 return (typeof result !== "undefined") ? result : null;
91 }
92
93 // print
94 function PRINT(exp) {
95 return printer._pr_str(exp, true);
96 }
97
98 // repl
99 var repl_env = new Env();
100 var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); };
101
102 // core.js: defined using javascript
103 for (var n in core.ns) { repl_env.set(types._symbol(n), core.ns[n]); }
104 repl_env.set(types._symbol('eval'), function(ast) {
105 return EVAL(ast, repl_env); });
106 repl_env.set(types._symbol('*ARGV*'), []);
107
108 // core.mal: defined using the language itself
109 rep("(def! not (fn* (a) (if a false true)))");
110 rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))");
111
112 if (typeof process !== 'undefined' && process.argv.length > 2) {
113 repl_env.set(types._symbol('*ARGV*'), process.argv.slice(3));
114 rep('(load-file "' + process.argv[2] + '")');
115 process.exit(0);
116 }
117
118 // repl loop
119 if (typeof require !== 'undefined' && require.main === module) {
120 // Synchronous node.js commandline mode
121 while (true) {
122 var line = readline.readline("user> ");
123 if (line === null) { break; }
124 try {
125 if (line) { printer.println(rep(line)); }
126 } catch (exc) {
127 if (exc instanceof reader.BlankException) { continue }
128 if (exc instanceof Error) { console.warn(exc.stack) }
129 else { console.warn("Error: " + printer._pr_str(exc, true)) }
130 }
131 }
132 }