jashkenas--coffeescript/lib/repl.js

33 lines
848 B
JavaScript
Raw Normal View History

2010-01-30 03:51:51 +00:00
(function(){
var coffee, prompt, quit, readline;
2010-01-30 03:51:51 +00:00
// A CoffeeScript port/version of the Node.js REPL.
// Required modules.
coffee = require('coffee-script');
2010-01-30 03:51:51 +00:00
// Shortcut variables.
prompt = 'coffee> ';
quit = function quit() {
return process.exit(0);
2010-01-30 03:51:51 +00:00
};
// The main REPL function. Called everytime a line of code is entered.
// Attempt to evaluate the command. If there's an exception, print it.
readline = function readline(code) {
2010-02-18 04:51:43 +00:00
var val;
2010-01-30 03:51:51 +00:00
try {
2010-02-18 04:51:43 +00:00
val = eval(coffee.compile(code, {
no_wrap: true,
globals: true
2010-02-18 04:51:43 +00:00
}));
2010-01-30 03:51:51 +00:00
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);
})();