1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-10 12:47:45 +03:00
mal/js/step1_read_print.js

52 lines
1.4 KiB
JavaScript

if (typeof module !== 'undefined') {
var types = require('./types');
var readline = require('./node_readline');
var reader = require('./reader');
var printer = require('./printer');
}
// read
function READ(str) {
return reader.read_str(str);
}
// eval
function EVAL(ast, env) {
return ast;
}
// print
function PRINT(exp) {
return printer._pr_str(exp, true);
}
// repl
var re = function(str) { return EVAL(READ(str), {}); };
var rep = function(str) { return PRINT(EVAL(READ(str), {})); };
// repl loop
if (typeof require === 'undefined') {
// Asynchronous browser mode
readline.rlwrap(function(line) { return rep(line); },
function(exc) {
if (exc instanceof reader.BlankException) { return; }
if (exc.stack) { printer.println(exc.stack); }
else { printer.println(exc); }
});
} else if (require.main === module) {
// Synchronous node.js commandline mode
while (true) {
var line = readline.readline("user> ");
if (line === null) { break; }
try {
if (line) { printer.println(rep(line)); }
} catch (exc) {
if (exc instanceof reader.BlankException) { continue; }
if (exc.stack) { printer.println(exc.stack); }
else { printer.println(exc); }
}
}
} else {
exports.rep = rep;
}