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');
|
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) {
|
|
|
|
// 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
|
|
|
if (ast.value in env) {
|
|
|
|
return env[ast.value];
|
2018-11-29 09:41:47 +03:00
|
|
|
} else {
|
|
|
|
throw new Error("'" + ast.value + "' not found");
|
|
|
|
}
|
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-04-03 01:56:04 +03:00
|
|
|
if (ast.length === 0) {
|
|
|
|
return ast;
|
|
|
|
}
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
// apply list
|
2022-01-10 02:15:40 +03:00
|
|
|
var f = EVAL(ast[0], 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
|
|
|
|
repl_env = {};
|
|
|
|
var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); };
|
|
|
|
|
|
|
|
repl_env['+'] = function(a,b){return a+b;};
|
|
|
|
repl_env['-'] = function(a,b){return a-b;};
|
|
|
|
repl_env['*'] = function(a,b){return a*b;};
|
|
|
|
repl_env['/'] = function(a,b){return a/b;};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|