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]; |
3dcaa6ce | 23 | } else if (types._symbol_Q(ast[0]) && ast[0].value === 'unquote') { |
31690700 JM |
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 | } | |
daf52a0a DM |
63 | if (ast.length === 0) { |
64 | return ast; | |
65 | } | |
ea81a808 JM |
66 | |
67 | // apply list | |
68 | var a0 = ast[0], a1 = ast[1], a2 = ast[2], a3 = ast[3]; | |
69 | switch (a0.value) { | |
70 | case "def!": | |
71 | var res = EVAL(a2, env); | |
72 | return env.set(a1, res); | |
73 | case "let*": | |
74 | var let_env = new Env(env); | |
75 | for (var i=0; i < a1.length; i+=2) { | |
b8ee29b2 | 76 | let_env.set(a1[i], EVAL(a1[i+1], let_env)); |
31690700 | 77 | } |
6301e0b6 JM |
78 | ast = a2; |
79 | env = let_env; | |
80 | break; | |
ea81a808 JM |
81 | case "quote": |
82 | return a1; | |
83 | case "quasiquote": | |
6301e0b6 JM |
84 | ast = quasiquote(a1); |
85 | break; | |
ea81a808 JM |
86 | case "do": |
87 | eval_ast(ast.slice(1, -1), env); | |
88 | ast = ast[ast.length-1]; | |
89 | break; | |
90 | case "if": | |
91 | var cond = EVAL(a1, env); | |
92 | if (cond === null || cond === false) { | |
93 | ast = (typeof a3 !== "undefined") ? a3 : null; | |
94 | } else { | |
95 | ast = a2; | |
31690700 | 96 | } |
ea81a808 JM |
97 | break; |
98 | case "fn*": | |
99 | return types._function(EVAL, Env, a2, env, a1); | |
100 | default: | |
8adb0827 | 101 | var el = eval_ast(ast, env), f = el[0]; |
a34b0200 JM |
102 | if (f.__ast__) { |
103 | ast = f.__ast__; | |
104 | env = f.__gen_env__(el.slice(1)); | |
ea81a808 JM |
105 | } else { |
106 | return f.apply(f, el.slice(1)); | |
107 | } | |
108 | } | |
109 | ||
31690700 JM |
110 | } |
111 | } | |
112 | ||
113 | function EVAL(ast, env) { | |
114 | var result = _EVAL(ast, env); | |
115 | return (typeof result !== "undefined") ? result : null; | |
116 | } | |
117 | ||
118 | ||
119 | function PRINT(exp) { | |
ea81a808 | 120 | return printer._pr_str(exp, true); |
31690700 JM |
121 | } |
122 | ||
123 | // repl | |
ea81a808 | 124 | var repl_env = new Env(); |
31690700 | 125 | var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; |
31690700 | 126 | |
8cb5cda4 | 127 | // core.js: defined using javascript |
b8ee29b2 JM |
128 | for (var n in core.ns) { repl_env.set(types._symbol(n), core.ns[n]); } |
129 | repl_env.set(types._symbol('eval'), function(ast) { | |
130 | return EVAL(ast, repl_env); }); | |
131 | repl_env.set(types._symbol('*ARGV*'), []); | |
31690700 | 132 | |
8cb5cda4 | 133 | // core.mal: defined using the language itself |
31690700 | 134 | rep("(def! not (fn* (a) (if a false true)))"); |
e6d41de4 | 135 | rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))"); |
31690700 JM |
136 | |
137 | if (typeof process !== 'undefined' && process.argv.length > 2) { | |
5f5c06e2 | 138 | repl_env.set(types._symbol('*ARGV*'), process.argv.slice(3)); |
86b689f3 JM |
139 | rep('(load-file "' + process.argv[2] + '")'); |
140 | process.exit(0); | |
141 | } | |
142 | ||
143 | // repl loop | |
e4393504 | 144 | if (typeof require !== 'undefined' && require.main === module) { |
31690700 JM |
145 | // Synchronous node.js commandline mode |
146 | while (true) { | |
147 | var line = readline.readline("user> "); | |
148 | if (line === null) { break; } | |
149 | try { | |
31b44161 | 150 | if (line) { printer.println(rep(line)); } |
31690700 | 151 | } catch (exc) { |
dd7a4f55 JM |
152 | if (exc instanceof reader.BlankException) { continue } |
153 | if (exc instanceof Error) { console.warn(exc.stack) } | |
154 | else { console.warn("Error: " + printer._pr_str(exc, true)) } | |
31690700 JM |
155 | } |
156 | } | |
31690700 | 157 | } |