Commit | Line | Data |
---|---|---|
31690700 | 1 | if (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 | |
11 | function READ(str) { | |
12 | return reader.read_str(str); | |
13 | } | |
14 | ||
15 | // eval | |
16 | function eval_ast(ast, env) { | |
ea81a808 | 17 | if (types._symbol_Q(ast)) { |
31690700 | 18 | return env.get(ast); |
ea81a808 | 19 | } else if (types._list_Q(ast)) { |
31690700 | 20 | return ast.map(function(a) { return EVAL(a, env); }); |
ea81a808 | 21 | } else if (types._vector_Q(ast)) { |
31690700 JM |
22 | var v = ast.map(function(a) { return EVAL(a, env); }); |
23 | v.__isvector__ = true; | |
24 | return v; | |
ea81a808 | 25 | } else if (types._hash_map_Q(ast)) { |
31690700 JM |
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) { | |
ea81a808 | 38 | |
86b689f3 | 39 | //printer.println("EVAL:", printer._pr_str(ast, true)); |
ea81a808 JM |
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)); | |
31690700 | 54 | } |
ea81a808 JM |
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; | |
31690700 | 66 | } |
ea81a808 JM |
67 | break; |
68 | case "fn*": | |
69 | return types._function(EVAL, Env, a2, env, a1); | |
70 | default: | |
8adb0827 | 71 | var el = eval_ast(ast, env), f = el[0]; |
a34b0200 JM |
72 | if (f.__ast__) { |
73 | ast = f.__ast__; | |
74 | env = f.__gen_env__(el.slice(1)); | |
ea81a808 JM |
75 | } else { |
76 | return f.apply(f, el.slice(1)); | |
77 | } | |
78 | } | |
79 | ||
31690700 JM |
80 | } |
81 | } | |
82 | ||
83 | function EVAL(ast, env) { | |
84 | var result = _EVAL(ast, env); | |
85 | return (typeof result !== "undefined") ? result : null; | |
86 | } | |
87 | ||
88 | ||
89 | function PRINT(exp) { | |
ea81a808 | 90 | return printer._pr_str(exp, true); |
31690700 JM |
91 | } |
92 | ||
93 | // repl | |
ea81a808 | 94 | var repl_env = new Env(); |
31690700 | 95 | var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; |
31690700 | 96 | |
8cb5cda4 | 97 | // core.js: defined using javascript |
ea81a808 | 98 | for (var n in core.ns) { repl_env.set(n, core.ns[n]); } |
8cb5cda4 | 99 | repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); |
86b689f3 | 100 | repl_env.set('*ARGV*', []); |
31690700 | 101 | |
8cb5cda4 | 102 | // core.mal: defined using the language itself |
31690700 | 103 | rep("(def! not (fn* (a) (if a false true)))"); |
1617910a | 104 | rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); |
31690700 JM |
105 | |
106 | if (typeof process !== 'undefined' && process.argv.length > 2) { | |
86b689f3 JM |
107 | repl_env.set('*ARGV*', process.argv.slice(3)); |
108 | rep('(load-file "' + process.argv[2] + '")'); | |
109 | process.exit(0); | |
110 | } | |
111 | ||
112 | // repl loop | |
113 | if (typeof require === 'undefined') { | |
31690700 JM |
114 | // Asynchronous browser mode |
115 | readline.rlwrap(function(line) { return rep(line); }, | |
116 | function(exc) { | |
117 | if (exc instanceof reader.BlankException) { return; } | |
31b44161 JM |
118 | if (exc.stack) { printer.println(exc.stack); } |
119 | else { printer.println(exc); } | |
31690700 JM |
120 | }); |
121 | } else if (require.main === module) { | |
122 | // Synchronous node.js commandline mode | |
123 | while (true) { | |
124 | var line = readline.readline("user> "); | |
125 | if (line === null) { break; } | |
126 | try { | |
31b44161 | 127 | if (line) { printer.println(rep(line)); } |
31690700 JM |
128 | } catch (exc) { |
129 | if (exc instanceof reader.BlankException) { continue; } | |
31b44161 JM |
130 | if (exc.stack) { printer.println(exc.stack); } |
131 | else { printer.println(exc); } | |
31690700 JM |
132 | } |
133 | } | |
134 | } else { | |
135 | exports.rep = rep; | |
136 | } |