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; | |
31690700 JM |
7 | } |
8 | ||
9 | // read | |
10 | function READ(str) { | |
11 | return reader.read_str(str); | |
12 | } | |
13 | ||
14 | // eval | |
15 | function eval_ast(ast, env) { | |
ea81a808 | 16 | if (types._symbol_Q(ast)) { |
31690700 | 17 | return env.get(ast); |
ea81a808 | 18 | } else if (types._list_Q(ast)) { |
31690700 | 19 | return ast.map(function(a) { return EVAL(a, env); }); |
ea81a808 | 20 | } else if (types._vector_Q(ast)) { |
31690700 JM |
21 | var v = ast.map(function(a) { return EVAL(a, env); }); |
22 | v.__isvector__ = true; | |
23 | return v; | |
ea81a808 | 24 | } else if (types._hash_map_Q(ast)) { |
31690700 JM |
25 | var new_hm = {}; |
26 | for (k in ast) { | |
27 | new_hm[EVAL(k, env)] = EVAL(ast[k], env); | |
28 | } | |
29 | return new_hm; | |
30 | } else { | |
31 | return ast; | |
32 | } | |
33 | } | |
34 | ||
35 | function _EVAL(ast, env) { | |
86b689f3 | 36 | //printer.println("EVAL:", printer._pr_str(ast, true)); |
ea81a808 | 37 | if (!types._list_Q(ast)) { |
31690700 JM |
38 | return eval_ast(ast, env); |
39 | } | |
daf52a0a DM |
40 | if (ast.length === 0) { |
41 | return ast; | |
42 | } | |
31690700 JM |
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*": | |
ea81a808 | 51 | var let_env = new Env(env); |
31690700 | 52 | for (var i=0; i < a1.length; i+=2) { |
b8ee29b2 | 53 | let_env.set(a1[i], EVAL(a1[i+1], let_env)); |
31690700 JM |
54 | } |
55 | return EVAL(a2, let_env); | |
56 | default: | |
57 | var el = eval_ast(ast, env), f = el[0]; | |
58 | return f.apply(f, el.slice(1)); | |
59 | } | |
60 | } | |
61 | ||
62 | function EVAL(ast, env) { | |
63 | var result = _EVAL(ast, env); | |
64 | return (typeof result !== "undefined") ? result : null; | |
65 | } | |
66 | ||
67 | ||
68 | function PRINT(exp) { | |
ea81a808 | 69 | return printer._pr_str(exp, true); |
31690700 JM |
70 | } |
71 | ||
72 | // repl | |
ea81a808 | 73 | var repl_env = new Env(); |
31690700 | 74 | var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; |
31690700 | 75 | |
b8ee29b2 JM |
76 | repl_env.set(types._symbol('+'), function(a,b){return a+b;}); |
77 | repl_env.set(types._symbol('-'), function(a,b){return a-b;}); | |
78 | repl_env.set(types._symbol('*'), function(a,b){return a*b;}); | |
79 | repl_env.set(types._symbol('/'), function(a,b){return a/b;}); | |
31690700 | 80 | |
86b689f3 | 81 | // repl loop |
e4393504 | 82 | if (typeof require !== 'undefined' && require.main === module) { |
31690700 JM |
83 | // Synchronous node.js commandline mode |
84 | while (true) { | |
85 | var line = readline.readline("user> "); | |
86 | if (line === null) { break; } | |
87 | try { | |
31b44161 | 88 | if (line) { printer.println(rep(line)); } |
31690700 JM |
89 | } catch (exc) { |
90 | if (exc instanceof reader.BlankException) { continue; } | |
31b44161 JM |
91 | if (exc.stack) { printer.println(exc.stack); } |
92 | else { printer.println(exc); } | |
31690700 JM |
93 | } |
94 | } | |
31690700 | 95 | } |