2010-01-30 03:51:51 +00:00
|
|
|
(function(){
|
2010-04-10 22:05:35 +00:00
|
|
|
var CoffeeScript, helpers, prompt, run, stdin;
|
2010-03-07 18:41:15 +00:00
|
|
|
// 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.
|
2010-03-07 19:41:52 +00:00
|
|
|
// Using it looks like this:
|
|
|
|
// coffee> puts "$num bottles of beer" for num in [99..1]
|
2010-03-07 18:41:15 +00:00
|
|
|
// Require the **coffee-script** module to get access to the compiler.
|
2010-03-16 03:39:46 +00:00
|
|
|
CoffeeScript = require('./coffee-script');
|
2010-03-16 05:53:25 +00:00
|
|
|
helpers = require('./helpers').helpers;
|
2010-03-07 18:41:15 +00:00
|
|
|
// Our prompt.
|
2010-01-30 03:51:51 +00:00
|
|
|
prompt = 'coffee> ';
|
2010-03-07 18:41:15 +00:00
|
|
|
// Quick alias for quitting the REPL.
|
2010-03-16 05:53:25 +00:00
|
|
|
helpers.extend(global, {
|
2010-03-08 02:49:08 +00:00
|
|
|
quit: function quit() {
|
|
|
|
return process.exit(0);
|
|
|
|
}
|
|
|
|
});
|
2010-03-07 19:41:52 +00:00
|
|
|
// The main REPL function. **run** is called every time a line of code is entered.
|
2010-03-07 18:41:15 +00:00
|
|
|
// Attempt to evaluate the command. If there's an exception, print it out instead
|
|
|
|
// of exiting.
|
2010-04-10 22:05:35 +00:00
|
|
|
run = function run(buffer) {
|
2010-02-18 04:51:43 +00:00
|
|
|
var val;
|
2010-01-30 03:51:51 +00:00
|
|
|
try {
|
2010-04-10 22:05:35 +00:00
|
|
|
val = CoffeeScript.run(buffer.toString(), {
|
2010-02-17 06:24:02 +00:00
|
|
|
no_wrap: true,
|
2010-03-08 03:17:45 +00:00
|
|
|
globals: true,
|
|
|
|
source: 'repl'
|
2010-03-08 02:49:08 +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);
|
|
|
|
};
|
2010-04-10 22:05:35 +00:00
|
|
|
// Start up the REPL by opening **stdin** and listening for input.
|
|
|
|
stdin = process.openStdin();
|
|
|
|
stdin.addListener('data', run);
|
2010-01-30 03:51:51 +00:00
|
|
|
print(prompt);
|
2010-02-24 23:56:32 +00:00
|
|
|
})();
|