2014-03-25 01:32:24 +04:00
|
|
|
if (typeof module !== 'undefined') {
|
2014-04-07 01:40:55 +04:00
|
|
|
var types = require('./types');
|
2014-03-25 01:32:24 +04:00
|
|
|
var readline = require('./node_readline');
|
2014-04-07 01:40:55 +04:00
|
|
|
var reader = require('./reader');
|
|
|
|
var printer = require('./printer');
|
|
|
|
var Env = require('./env').Env;
|
|
|
|
var core = require('./core');
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// read
|
|
|
|
function READ(str) {
|
|
|
|
return reader.read_str(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
// eval
|
2022-01-10 02:15:40 +03:00
|
|
|
function _EVAL(ast, env) {
|
|
|
|
while (true) {
|
|
|
|
// Show a trace if DEBUG-EVAL is enabled.
|
|
|
|
var dbgevalenv = env.find("DEBUG-EVAL");
|
|
|
|
if (dbgevalenv !== null) {
|
|
|
|
var dbgeval = env.get("DEBUG-EVAL");
|
|
|
|
if (dbgeval !== null && dbgeval !== false)
|
|
|
|
printer.println("EVAL:", printer._pr_str(ast, true));
|
|
|
|
}
|
|
|
|
// Non-list types.
|
2014-04-03 07:23:37 +04:00
|
|
|
if (types._symbol_Q(ast)) {
|
2022-01-10 02:15:40 +03:00
|
|
|
return env.get(ast.value);
|
2014-04-03 07:23:37 +04:00
|
|
|
} else if (types._list_Q(ast)) {
|
2022-01-10 02:15:40 +03:00
|
|
|
// Exit this switch.
|
2014-04-03 07:23:37 +04:00
|
|
|
} else if (types._vector_Q(ast)) {
|
2014-03-25 01:32:24 +04:00
|
|
|
var v = ast.map(function(a) { return EVAL(a, env); });
|
|
|
|
v.__isvector__ = true;
|
|
|
|
return v;
|
2014-04-03 07:23:37 +04:00
|
|
|
} else if (types._hash_map_Q(ast)) {
|
2014-03-25 01:32:24 +04:00
|
|
|
var new_hm = {};
|
|
|
|
for (k in ast) {
|
2021-08-21 20:08:17 +03:00
|
|
|
new_hm[k] = EVAL(ast[k], env);
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
return new_hm;
|
|
|
|
} else {
|
|
|
|
return ast;
|
|
|
|
}
|
2014-04-03 07:23:37 +04:00
|
|
|
|
2016-03-28 04:42:36 +03:00
|
|
|
if (ast.length === 0) {
|
|
|
|
return ast;
|
|
|
|
}
|
2014-04-03 07:23:37 +04:00
|
|
|
|
|
|
|
// apply list
|
|
|
|
var a0 = ast[0], a1 = ast[1], a2 = ast[2], a3 = ast[3];
|
|
|
|
switch (a0.value) {
|
|
|
|
case "def!":
|
|
|
|
var res = EVAL(a2, env);
|
2022-01-10 02:15:40 +03:00
|
|
|
if (!a1.constructor || a1.constructor.name !== 'Symbol') {
|
|
|
|
throw new Error("env.get key must be a symbol")
|
|
|
|
}
|
|
|
|
return env.set(a1.value, res);
|
2014-04-03 07:23:37 +04:00
|
|
|
case "let*":
|
|
|
|
var let_env = new Env(env);
|
|
|
|
for (var i=0; i < a1.length; i+=2) {
|
2022-01-10 02:15:40 +03:00
|
|
|
if (!a1[i].constructor || a1[i].constructor.name !== 'Symbol') {
|
|
|
|
throw new Error("env.get key must be a symbol")
|
|
|
|
}
|
|
|
|
let_env.set(a1[i].value, EVAL(a1[i+1], let_env));
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
2014-04-24 06:59:50 +04:00
|
|
|
ast = a2;
|
|
|
|
env = let_env;
|
|
|
|
break;
|
2014-04-03 07:23:37 +04:00
|
|
|
case "do":
|
2022-01-10 02:15:40 +03:00
|
|
|
for (var i=1; i < ast.length - 1; i++) {
|
|
|
|
EVAL(ast[i], env);
|
|
|
|
}
|
2014-04-03 07:23:37 +04:00
|
|
|
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;
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
2014-04-03 07:23:37 +04:00
|
|
|
break;
|
|
|
|
case "fn*":
|
|
|
|
return types._function(EVAL, Env, a2, env, a1);
|
|
|
|
default:
|
2022-01-10 02:15:40 +03:00
|
|
|
var f = EVAL(a0, env);
|
|
|
|
var args = ast.slice(1).map(function(a) { return EVAL(a, env); });
|
2014-04-10 08:58:27 +04:00
|
|
|
if (f.__ast__) {
|
|
|
|
ast = f.__ast__;
|
2022-01-10 02:15:40 +03:00
|
|
|
env = f.__gen_env__(args);
|
2014-04-03 07:23:37 +04:00
|
|
|
} else {
|
2022-01-10 02:15:40 +03:00
|
|
|
return f.apply(f, args);
|
2014-04-03 07:23:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function EVAL(ast, env) {
|
|
|
|
var result = _EVAL(ast, env);
|
|
|
|
return (typeof result !== "undefined") ? result : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// print
|
|
|
|
function PRINT(exp) {
|
2014-04-03 07:23:37 +04:00
|
|
|
return printer._pr_str(exp, true);
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// repl
|
2014-04-03 07:23:37 +04:00
|
|
|
var repl_env = new Env();
|
2014-03-25 01:32:24 +04:00
|
|
|
var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); };
|
|
|
|
|
2014-04-17 08:57:50 +04:00
|
|
|
// core.js: defined using javascript
|
2022-01-10 02:15:40 +03:00
|
|
|
for (var n in core.ns) { repl_env.set(n, core.ns[n]); }
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-17 08:57:50 +04:00
|
|
|
// core.mal: defined using the language itself
|
2014-03-25 01:32:24 +04:00
|
|
|
rep("(def! not (fn* (a) (if a false true)))");
|
|
|
|
|
2014-04-19 22:04:09 +04:00
|
|
|
// repl loop
|
2014-04-26 23:19:49 +04:00
|
|
|
if (typeof require !== 'undefined' && require.main === module) {
|
2014-03-25 01:32:24 +04:00
|
|
|
// Synchronous node.js commandline mode
|
|
|
|
while (true) {
|
|
|
|
var line = readline.readline("user> ");
|
|
|
|
if (line === null) { break; }
|
|
|
|
try {
|
2014-04-07 01:40:55 +04:00
|
|
|
if (line) { printer.println(rep(line)); }
|
2014-03-25 01:32:24 +04:00
|
|
|
} catch (exc) {
|
Test uncaught throw, catchless try* . Fix 46 impls.
Fixes made to: ada, c, chuck, clojure, coffee, common-lisp, cpp,
crystal, d, dart, elm, erlang, es6, factor, fsharp, gnu-smalltalk,
groovy, guile, haxe, hy, js, livescript, matlab, miniMAL, nasm, nim,
objc, objpascal, ocaml, perl, perl6, php, plsql, ps, python, r,
rpython, ruby, scheme, swift3, tcl, ts, vb, vimscript, wasm, yorick.
Catchless try* test is an optional test. Not all implementations
support catchless try* but a number were fixed so they at least don't
crash on catchless try*.
2018-12-03 22:20:44 +03:00
|
|
|
if (exc instanceof reader.BlankException) { continue }
|
|
|
|
if (exc instanceof Error) { console.warn(exc.stack) }
|
|
|
|
else { console.warn("Error: " + printer._pr_str(exc, true)) }
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|