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) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2016-03-28 04:42:36 +03:00
|
|
|
if (ast.length === 0) {
|
|
|
|
return ast;
|
|
|
|
}
|
2014-03-25 01:32:24 +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-03-25 01:32:24 +04:00
|
|
|
case "let*":
|
2014-04-03 07:23:37 +04:00
|
|
|
var let_env = new Env(env);
|
2014-03-25 01:32:24 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
return EVAL(a2, let_env);
|
|
|
|
case "do":
|
2022-01-10 02:15:40 +03:00
|
|
|
for (var i=1; i < ast.length - 1; i++) {
|
|
|
|
EVAL(ast[i], env);
|
|
|
|
}
|
|
|
|
return EVAL(ast[ast.length-1], env);
|
2014-03-25 01:32:24 +04:00
|
|
|
case "if":
|
|
|
|
var cond = EVAL(a1, env);
|
|
|
|
if (cond === null || cond === false) {
|
|
|
|
return typeof a3 !== "undefined" ? EVAL(a3, env) : null;
|
|
|
|
} else {
|
|
|
|
return EVAL(a2, env);
|
|
|
|
}
|
|
|
|
case "fn*":
|
|
|
|
return function() {
|
2014-04-03 07:23:37 +04:00
|
|
|
return EVAL(a2, new Env(env, a1, arguments));
|
2014-03-25 01:32:24 +04:00
|
|
|
};
|
|
|
|
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); });
|
|
|
|
return f.apply(f, args);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|