diff options
| author | Joel Martin <github@martintribe.org> | 2014-04-02 22:23:37 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-04-02 22:23:37 -0500 |
| commit | ea81a8087bcd7953b083a2be9db447f75e7ebf56 (patch) | |
| tree | 6cf47a2dbd55d42efc4a901eaabdec952f40ce89 /js/stepA_more.js | |
| parent | 1617910ad342a55762f3ddabb975849d843cff85 (diff) | |
| download | mal-ea81a8087bcd7953b083a2be9db447f75e7ebf56.tar.gz mal-ea81a8087bcd7953b083a2be9db447f75e7ebf56.zip | |
All: split types into types, env, printer, core.
- types: low-level mapping to the implementation language.
- core: functions on types that are exposed directly to mal.
- printer: implementation called by pr-str, str, prn, println.
- env: the environment implementation
- Also, unindent all TCO while loops so that the diff of step4 and
step5 are minimized.
Diffstat (limited to 'js/stepA_more.js')
| -rw-r--r-- | js/stepA_more.js | 165 |
1 files changed, 85 insertions, 80 deletions
diff --git a/js/stepA_more.js b/js/stepA_more.js index 2778649..a4e1bda 100644 --- a/js/stepA_more.js +++ b/js/stepA_more.js @@ -1,5 +1,8 @@ var types = require('./types'); var reader = require('./reader'); +var printer = require('./printer'); +var Env = require('./env').Env; +var core = require('./core'); if (typeof module !== 'undefined') { var readline = require('./node_readline'); } @@ -11,24 +14,24 @@ function READ(str) { // eval function is_pair(x) { - return types.sequential_Q(x) && x.length > 0; + return types._sequential_Q(x) && x.length > 0; } function quasiquote(ast) { if (!is_pair(ast)) { - return [types.symbol("quote"), ast]; + return [types._symbol("quote"), ast]; } else if (ast[0].value === 'unquote') { return ast[1]; } else if (is_pair(ast[0]) && ast[0][0].value === 'splice-unquote') { - return [types.symbol("concat"), ast[0][1], quasiquote(ast.slice(1))]; + return [types._symbol("concat"), ast[0][1], quasiquote(ast.slice(1))]; } else { - return [types.symbol("cons"), quasiquote(ast[0]), quasiquote(ast.slice(1))]; + return [types._symbol("cons"), quasiquote(ast[0]), quasiquote(ast.slice(1))]; } } function is_macro_call(ast, env) { - return types.list_Q(ast) && - types.symbol_Q(ast[0]) && + return types._list_Q(ast) && + types._symbol_Q(ast[0]) && env.find(ast[0].value) && env.get(ast[0].value)._ismacro_; } @@ -42,15 +45,15 @@ function macroexpand(ast, env) { } function eval_ast(ast, env) { - if (types.symbol_Q(ast)) { + if (types._symbol_Q(ast)) { return env.get(ast); - } else if (types.list_Q(ast)) { + } else if (types._list_Q(ast)) { return ast.map(function(a) { return EVAL(a, env); }); - } else if (types.vector_Q(ast)) { + } else if (types._vector_Q(ast)) { var v = ast.map(function(a) { return EVAL(a, env); }); v.__isvector__ = true; return v; - } else if (types.hash_map_Q(ast)) { + } else if (types._hash_map_Q(ast)) { var new_hm = {}; for (k in ast) { new_hm[EVAL(k, env)] = EVAL(ast[k], env); @@ -63,76 +66,78 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //console.log("EVAL:", types._pr_str(ast, true)); - if (!types.list_Q(ast)) { - return eval_ast(ast, env); + + //console.log("EVAL:", types._pr_str(ast, true)); + if (!types._list_Q(ast)) { + return eval_ast(ast, env); + } + + // apply list + ast = macroexpand(ast, env); + if (!types._list_Q(ast)) { return ast; } + + var a0 = ast[0], a1 = ast[1], a2 = ast[2], a3 = ast[3]; + switch (a0.value) { + case "def!": + var res = EVAL(a2, env); + return env.set(a1, res); + case "let*": + var let_env = new Env(env); + for (var i=0; i < a1.length; i+=2) { + let_env.set(a1[i].value, EVAL(a1[i+1], let_env)); } - - // apply list - ast = macroexpand(ast, env); - if (!types.list_Q(ast)) { return ast; } - - var a0 = ast[0], a1 = ast[1], a2 = ast[2], a3 = ast[3]; - switch (a0.value) { - case "def!": - var res = EVAL(a2, env); - return env.set(a1, res); - case "let*": - var let_env = new types.Env(env); - for (var i=0; i < a1.length; i+=2) { - let_env.set(a1[i].value, EVAL(a1[i+1], let_env)); - } - return EVAL(a2, let_env); - case "quote": - return a1; - case "quasiquote": - return EVAL(quasiquote(a1), env); - case 'defmacro!': - var func = EVAL(a2, env); - func._ismacro_ = true; - return env.set(a1, func); - case 'macroexpand': - return macroexpand(a1, env); - case "js*": - return eval(a1.toString()); - case ".": - var el = eval_ast(ast.slice(2), env), - f = eval(a1.toString()); - return f.apply(f, el); - case "try*": - try { - return EVAL(a1, env); - } catch (exc) { - if (a2 && a2[0].value === "catch*") { - if (exc instanceof Error) { exc = exc.message; } - return EVAL(a2[2], new types.Env(env, [a2[1]], [exc])); - } else { - throw exc; - } - } - case "do": - eval_ast(ast.slice(1, -1), env); - ast = ast[ast.length-1]; - break; - case "if": - var cond = EVAL(a1, env); - if (cond === null || cond === false) { - ast = (typeof a3 !== "undefined") ? a3 : null; - } else { - ast = a2; - } - break; - case "fn*": - return types.new_function(EVAL, a2, env, a1); - default: - var el = eval_ast(ast, env), f = el[0], meta = f.__meta__; - if (meta && meta.exp) { - ast = meta.exp; - env = new types.Env(meta.env, meta.params, el.slice(1)); + return EVAL(a2, let_env); + case "quote": + return a1; + case "quasiquote": + return EVAL(quasiquote(a1), env); + case 'defmacro!': + var func = EVAL(a2, env); + func._ismacro_ = true; + return env.set(a1, func); + case 'macroexpand': + return macroexpand(a1, env); + case "js*": + return eval(a1.toString()); + case ".": + var el = eval_ast(ast.slice(2), env), + f = eval(a1.toString()); + return f.apply(f, el); + case "try*": + try { + return EVAL(a1, env); + } catch (exc) { + if (a2 && a2[0].value === "catch*") { + if (exc instanceof Error) { exc = exc.message; } + return EVAL(a2[2], new Env(env, [a2[1]], [exc])); } else { - return f.apply(f, el.slice(1)); + throw exc; } } + case "do": + eval_ast(ast.slice(1, -1), env); + ast = ast[ast.length-1]; + break; + case "if": + var cond = EVAL(a1, env); + if (cond === null || cond === false) { + ast = (typeof a3 !== "undefined") ? a3 : null; + } else { + ast = a2; + } + break; + case "fn*": + return types._function(EVAL, Env, a2, env, a1); + default: + var el = eval_ast(ast, env), f = el[0], meta = f.__meta__; + if (meta && meta.exp) { + ast = meta.exp; + env = new Env(meta.env, meta.params, el.slice(1)); + } else { + return f.apply(f, el.slice(1)); + } + } + } } @@ -143,16 +148,16 @@ function EVAL(ast, env) { // print function PRINT(exp) { - return types._pr_str(exp, true); + return printer._pr_str(exp, true); } // repl -var repl_env = new types.Env(); +var repl_env = new Env(); var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; _ref = function (k,v) { repl_env.set(k, v); } -// Import types functions -for (var n in types.ns) { repl_env.set(n, types.ns[n]); } +// Import core functions +for (var n in core.ns) { repl_env.set(n, core.ns[n]); } _ref('readline', readline.readline) _ref('read-string', reader.read_str); |
