jashkenas--coffeescript/lib/repl.js

36 lines
1.1 KiB
JavaScript
Raw Normal View History

2010-01-29 22:51:51 -05:00
(function(){
var CoffeeScript, prompt, quit, run;
// A very simple Read-Eval-Print-Loop. Compiles one line at a time to JavaScript
// and evaluates it. Good for simple tests, or poking around the **Node.js** API.
// Require the **coffee-script** module to get access to the compiler.
CoffeeScript = require('coffee-script');
// Our prompt.
2010-01-29 22:51:51 -05:00
prompt = 'coffee> ';
// Quick alias for quitting the REPL.
2010-01-29 22:51:51 -05:00
quit = function quit() {
return process.exit(0);
2010-01-29 22:51:51 -05:00
};
// The main REPL function. `run` is called every time a line of code is entered.
// Attempt to evaluate the command. If there's an exception, print it out instead
// of exiting.
run = function run(code) {
2010-02-17 23:51:43 -05:00
var val;
2010-01-29 22:51:51 -05:00
try {
val = eval(CoffeeScript.compile(code, {
no_wrap: true,
globals: true
2010-02-17 23:51:43 -05:00
}));
2010-01-29 22:51:51 -05:00
if (val !== undefined) {
p(val);
}
} catch (err) {
puts(err.stack || err.toString());
}
return print(prompt);
};
// Start up the REPL.
process.stdio.addListener('data', run);
process.stdio.open();
2010-01-29 22:51:51 -05:00
print(prompt);
})();