All: split types into types, env, printer, core.
[jackhill/mal.git] / js / step0_repl.js
1 if (typeof module !== 'undefined') {
2 var readline = require('./node_readline');
3 }
4
5 // read
6 function READ(str) {
7 return str;
8 }
9
10 // eval
11 function EVAL(ast, env) {
12 return eval(ast);
13 }
14
15 // print
16 function PRINT(exp) {
17 return exp;
18 }
19
20 // repl
21 var rep = function(str) { return PRINT(EVAL(READ(str), {})); };
22
23 if (typeof require === 'undefined') {
24 // Asynchronous browser mode
25 readline.rlwrap(function(line) { return rep(line); },
26 function(exc) {
27 if (exc.stack) { console.log(exc.stack); } else { console.log(exc); }
28 });
29 } else if (require.main === module) {
30 // Synchronous node.js commandline mode
31 while (true) {
32 var line = readline.readline("user> ");
33 if (line === null) { break; }
34 try {
35 if (line) { console.log(rep(line)); }
36 } catch (exc) {
37 if (exc.stack) { console.log(exc.stack); } else { console.log(exc); }
38 }
39 }
40 } else {
41 exports.rep = rep;
42 }