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 is_pair(x) { | |
ea81a808 | 17 | return types._sequential_Q(x) && x.length > 0; |
31690700 JM |
18 | } |
19 | ||
20 | function quasiquote(ast) { | |
21 | if (!is_pair(ast)) { | |
ea81a808 | 22 | return [types._symbol("quote"), ast]; |
31690700 JM |
23 | } else if (ast[0].value === 'unquote') { |
24 | return ast[1]; | |
25 | } else if (is_pair(ast[0]) && ast[0][0].value === 'splice-unquote') { | |
8adb0827 JM |
26 | return [types._symbol("concat"), |
27 | ast[0][1], | |
28 | quasiquote(ast.slice(1))]; | |
31690700 | 29 | } else { |
8adb0827 JM |
30 | return [types._symbol("cons"), |
31 | quasiquote(ast[0]), | |
32 | quasiquote(ast.slice(1))]; | |
31690700 JM |
33 | } |
34 | } | |
35 | ||
36 | function eval_ast(ast, env) { | |
ea81a808 | 37 | if (types._symbol_Q(ast)) { |
31690700 | 38 | return env.get(ast); |
ea81a808 | 39 | } else if (types._list_Q(ast)) { |
31690700 | 40 | return ast.map(function(a) { return EVAL(a, env); }); |
ea81a808 | 41 | } else if (types._vector_Q(ast)) { |
31690700 JM |
42 | var v = ast.map(function(a) { return EVAL(a, env); }); |
43 | v.__isvector__ = true; | |
44 | return v; | |
ea81a808 | 45 | } else if (types._hash_map_Q(ast)) { |
31690700 JM |
46 | var new_hm = {}; |
47 | for (k in ast) { | |
48 | new_hm[EVAL(k, env)] = EVAL(ast[k], env); | |
49 | } | |
50 | return new_hm; | |
51 | } else { | |
52 | return ast; | |
53 | } | |
54 | } | |
55 | ||
56 | function _EVAL(ast, env) { | |
57 | while (true) { | |
ea81a808 | 58 | |
86b689f3 | 59 | //printer.println("EVAL:", printer._pr_str(ast, true)); |
ea81a808 JM |
60 | if (!types._list_Q(ast)) { |
61 | return eval_ast(ast, env); | |
62 | } | |
63 | ||
64 | // apply list | |
65 | var a0 = ast[0], a1 = ast[1], a2 = ast[2], a3 = ast[3]; | |
66 | switch (a0.value) { | |
67 | case "def!": | |
68 | var res = EVAL(a2, env); | |
69 | return env.set(a1, res); | |
70 | case "let*": | |
71 | var let_env = new Env(env); | |
72 | for (var i=0; i < a1.length; i+=2) { | |
b8ee29b2 | 73 | let_env.set(a1[i], EVAL(a1[i+1], let_env)); |
31690700 | 74 | } |
6301e0b6 JM |
75 | ast = a2; |
76 | env = let_env; | |
77 | break; | |
ea81a808 JM |
78 | case "quote": |
79 | return a1; | |
80 | case "quasiquote": | |
6301e0b6 JM |
81 | ast = quasiquote(a1); |
82 | break; | |
ea81a808 JM |
83 | case "do": |
84 | eval_ast(ast.slice(1, -1), env); | |
85 | ast = ast[ast.length-1]; | |
86 | break; | |
87 | case "if": | |
88 | var cond = EVAL(a1, env); | |
89 | if (cond === null || cond === false) { | |
90 | ast = (typeof a3 !== "undefined") ? a3 : null; | |
91 | } else { | |
92 | ast = a2; | |
31690700 | 93 | } |
ea81a808 JM |
94 | break; |
95 | case "fn*": | |
96 | return types._function(EVAL, Env, a2, env, a1); | |
97 | default: | |
8adb0827 | 98 | var el = eval_ast(ast, env), f = el[0]; |
a34b0200 JM |
99 | if (f.__ast__) { |
100 | ast = f.__ast__; | |
101 | env = f.__gen_env__(el.slice(1)); | |
ea81a808 JM |
102 | } else { |
103 | return f.apply(f, el.slice(1)); | |
104 | } | |
105 | } | |
106 | ||
31690700 JM |
107 | } |
108 | } | |
109 | ||
110 | function EVAL(ast, env) { | |
111 | var result = _EVAL(ast, env); | |
112 | return (typeof result !== "undefined") ? result : null; | |
113 | } | |
114 | ||
115 | ||
116 | function PRINT(exp) { | |
ea81a808 | 117 | return printer._pr_str(exp, true); |
31690700 JM |
118 | } |
119 | ||
120 | // repl | |
ea81a808 | 121 | var repl_env = new Env(); |
31690700 | 122 | var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; |
31690700 | 123 | |
8cb5cda4 | 124 | // core.js: defined using javascript |
b8ee29b2 JM |
125 | for (var n in core.ns) { repl_env.set(types._symbol(n), core.ns[n]); } |
126 | repl_env.set(types._symbol('eval'), function(ast) { | |
127 | return EVAL(ast, repl_env); }); | |
128 | repl_env.set(types._symbol('*ARGV*'), []); | |
31690700 | 129 | |
8cb5cda4 | 130 | // core.mal: defined using the language itself |
31690700 | 131 | rep("(def! not (fn* (a) (if a false true)))"); |
1617910a | 132 | rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); |
31690700 JM |
133 | |
134 | if (typeof process !== 'undefined' && process.argv.length > 2) { | |
86b689f3 JM |
135 | repl_env.set('*ARGV*', process.argv.slice(3)); |
136 | rep('(load-file "' + process.argv[2] + '")'); | |
137 | process.exit(0); | |
138 | } | |
139 | ||
140 | // repl loop | |
e4393504 | 141 | if (typeof require !== 'undefined' && require.main === module) { |
31690700 JM |
142 | // Synchronous node.js commandline mode |
143 | while (true) { | |
144 | var line = readline.readline("user> "); | |
145 | if (line === null) { break; } | |
146 | try { | |
31b44161 | 147 | if (line) { printer.println(rep(line)); } |
31690700 JM |
148 | } catch (exc) { |
149 | if (exc instanceof reader.BlankException) { continue; } | |
31b44161 JM |
150 | if (exc.stack) { printer.println(exc.stack); } |
151 | else { printer.println(exc); } | |
31690700 JM |
152 | } |
153 | } | |
31690700 | 154 | } |