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
|
2020-07-21 19:01:48 +03:00
|
|
|
function qqLoop (acc, elt) {
|
|
|
|
if (types._list_Q(elt) && elt.length
|
|
|
|
&& types._symbol_Q(elt[0]) && elt[0].value == 'splice-unquote') {
|
|
|
|
return [types._symbol("concat"), elt[1], acc];
|
|
|
|
} else {
|
|
|
|
return [types._symbol("cons"), quasiquote (elt), acc];
|
|
|
|
}
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
function quasiquote(ast) {
|
2020-07-21 19:01:48 +03:00
|
|
|
if (types._list_Q(ast) && 0<ast.length
|
|
|
|
&& types._symbol_Q(ast[0]) && ast[0].value == 'unquote') {
|
2014-03-25 01:32:24 +04:00
|
|
|
return ast[1];
|
2020-07-21 19:01:48 +03:00
|
|
|
} else if (types._list_Q(ast)) {
|
|
|
|
return ast.reduceRight(qqLoop,[]);
|
|
|
|
} else if (types._vector_Q(ast)) {
|
|
|
|
return [types._symbol("vec"), ast.reduceRight(qqLoop,[])];
|
|
|
|
} else if (types._symbol_Q(ast) || types._hash_map_Q(ast)) {
|
|
|
|
return [types._symbol("quote"), ast];
|
2014-03-25 01:32:24 +04:00
|
|
|
} else {
|
2020-07-21 19:01:48 +03:00
|
|
|
return ast;
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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));
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
2022-01-10 02:15:40 +03:00
|
|
|
// 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
|
|
|
// apply list
|
2016-03-28 04:42:36 +03:00
|
|
|
if (ast.length === 0) {
|
|
|
|
return ast;
|
|
|
|
}
|
2014-04-03 07:23:37 +04:00
|
|
|
|
|
|
|
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 "quote":
|
|
|
|
return a1;
|
|
|
|
case "quasiquote":
|
2014-04-24 06:59:50 +04:00
|
|
|
ast = quasiquote(a1);
|
|
|
|
break;
|
2014-04-03 07:23:37 +04:00
|
|
|
case 'defmacro!':
|
2019-12-17 14:16:28 +03:00
|
|
|
var func = types._clone(EVAL(a2, env));
|
2014-04-03 07:23:37 +04:00
|
|
|
func._ismacro_ = true;
|
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, func);
|
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);
|
|
|
|
if (f._ismacro_) {
|
|
|
|
ast = f.apply(f, ast.slice(1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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]); }
|
|
|
|
repl_env.set('eval', function(ast) {
|
2014-12-19 05:33:49 +03:00
|
|
|
return EVAL(ast, repl_env); });
|
2022-01-10 02:15:40 +03:00
|
|
|
repl_env.set('*ARGV*', []);
|
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)))");
|
2019-07-16 00:57:02 +03:00
|
|
|
rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))");
|
2014-04-17 08:57:50 +04:00
|
|
|
rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))");
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
if (typeof process !== 'undefined' && process.argv.length > 2) {
|
2014-12-19 05:33:49 +03:00
|
|
|
repl_env.set(types._symbol('*ARGV*'), process.argv.slice(3));
|
2014-04-19 22:04:09 +04:00
|
|
|
rep('(load-file "' + process.argv[2] + '")');
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|