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