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 | } | |
40 | ||
41 | // apply list | |
42 | var a0 = ast[0], a1 = ast[1], a2 = ast[2], a3 = ast[3]; | |
43 | switch (a0.value) { | |
44 | case "def!": | |
45 | var res = EVAL(a2, env); | |
46 | return env.set(a1, res); | |
47 | case "let*": | |
ea81a808 | 48 | var let_env = new Env(env); |
31690700 JM |
49 | for (var i=0; i < a1.length; i+=2) { |
50 | let_env.set(a1[i].value, EVAL(a1[i+1], let_env)); | |
51 | } | |
52 | return EVAL(a2, let_env); | |
53 | default: | |
54 | var el = eval_ast(ast, env), f = el[0]; | |
55 | return f.apply(f, el.slice(1)); | |
56 | } | |
57 | } | |
58 | ||
59 | function EVAL(ast, env) { | |
60 | var result = _EVAL(ast, env); | |
61 | return (typeof result !== "undefined") ? result : null; | |
62 | } | |
63 | ||
64 | ||
65 | function PRINT(exp) { | |
ea81a808 | 66 | return printer._pr_str(exp, true); |
31690700 JM |
67 | } |
68 | ||
69 | // repl | |
ea81a808 | 70 | var repl_env = new Env(); |
31690700 | 71 | var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; |
31690700 | 72 | |
8cb5cda4 JM |
73 | repl_env.set('+', function(a,b){return a+b;}); |
74 | repl_env.set('-', function(a,b){return a-b;}); | |
75 | repl_env.set('*', function(a,b){return a*b;}); | |
76 | repl_env.set('/', function(a,b){return a/b;}); | |
31690700 | 77 | |
86b689f3 | 78 | // repl loop |
e4393504 | 79 | if (typeof require !== 'undefined' && require.main === module) { |
31690700 JM |
80 | // Synchronous node.js commandline mode |
81 | while (true) { | |
82 | var line = readline.readline("user> "); | |
83 | if (line === null) { break; } | |
84 | try { | |
31b44161 | 85 | if (line) { printer.println(rep(line)); } |
31690700 JM |
86 | } catch (exc) { |
87 | if (exc instanceof reader.BlankException) { continue; } | |
31b44161 JM |
88 | if (exc.stack) { printer.println(exc.stack); } |
89 | else { printer.println(exc); } | |
31690700 JM |
90 | } |
91 | } | |
31690700 | 92 | } |