jashkenas--coffeescript/lib/repl.js

39 lines
924 B
JavaScript
Raw Normal View History

(function() {
var CoffeeScript, helpers, readline, repl, run, stdio;
CoffeeScript = require('./coffee-script');
2010-09-25 00:29:44 +00:00
helpers = require('./helpers');
readline = require('readline');
stdio = process.openStdin();
helpers.extend(global, {
quit: function() {
return process.exit(0);
}
});
run = function(buffer) {
2010-02-18 04:51:43 +00:00
var val;
2010-01-30 03:51:51 +00:00
try {
val = CoffeeScript.eval(buffer.toString(), {
2010-10-13 19:09:56 +00:00
bare: true,
globals: true,
fileName: 'repl'
});
if (val !== void 0) {
2010-10-25 01:15:20 +00:00
console.log(val);
2010-01-30 03:51:51 +00:00
}
} catch (err) {
console.error(err.stack || err.toString());
2010-01-30 03:51:51 +00:00
}
return repl.prompt();
2010-01-30 03:51:51 +00:00
};
repl = readline.createInterface(stdio);
repl.setPrompt('coffee> ');
stdio.on('data', function(buffer) {
return repl.write(buffer);
});
repl.on('close', function() {
return stdio.destroy();
});
repl.on('line', run);
repl.prompt();
}).call(this);