jashkenas--coffeescript/lib/repl.js

36 lines
937 B
JavaScript
Raw Normal View History

2010-01-30 03:51:51 +00:00
(function(){
var coffee, prompt, quit, readline, run;
// A CoffeeScript port/version of the Node.js REPL.
// Required modules.
coffee = require('coffee-script');
2010-01-30 03:51:51 +00:00
process.mixin(require('sys'));
// Shortcut variables.
prompt = 'coffee> ';
quit = function quit() {
return process.stdio.close();
};
// The main REPL function. Called everytime a line of code is entered.
readline = function readline(code) {
return run(coffee.compile(code, {
no_wrap: true,
globals: true
}));
2010-01-30 03:51:51 +00:00
};
// Attempt to evaluate the command. If there's an exception, print it.
run = function run(js) {
var val;
try {
val = eval(js);
if (val !== undefined) {
p(val);
}
} catch (err) {
puts(err.stack || err.toString());
}
return print(prompt);
};
// Start up the REPL.
process.stdio.addListener('data', readline);
process.stdio.open();
2010-01-30 03:51:51 +00:00
print(prompt);
})();