mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
first draft of node.js REPL
This commit is contained in:
parent
83285fe170
commit
ba2d9df25f
5 changed files with 95 additions and 1 deletions
20
lib/coffee_script/coffee-script.js
Normal file
20
lib/coffee_script/coffee-script.js
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
(function(){
|
||||||
|
var sys;
|
||||||
|
// Executes the `coffee` Ruby program to convert from CoffeeScript to JavaScript.
|
||||||
|
sys = require('sys');
|
||||||
|
exports.compile = function compile(code, callback) {
|
||||||
|
var coffee, js;
|
||||||
|
js = '';
|
||||||
|
coffee = process.createChildProcess('coffee', ['--eval', '--no-wrap', '--globals']);
|
||||||
|
coffee.addListener('output', function(results) {
|
||||||
|
if ((typeof results !== "undefined" && results !== null)) {
|
||||||
|
return js += results;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
coffee.addListener('exit', function() {
|
||||||
|
return callback(js);
|
||||||
|
});
|
||||||
|
coffee.write(code);
|
||||||
|
return coffee.close();
|
||||||
|
};
|
||||||
|
})();
|
|
@ -118,7 +118,7 @@ Usage:
|
||||||
def launch_repl
|
def launch_repl
|
||||||
exec "#{LAUNCHER}"
|
exec "#{LAUNCHER}"
|
||||||
rescue Errno::ENOENT
|
rescue Errno::ENOENT
|
||||||
puts "Error: Narwhal must be installed to use the interactive REPL."
|
puts "Error: Node.js must be installed to use the interactive REPL."
|
||||||
exit(1)
|
exit(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
33
lib/coffee_script/repl.js
Normal file
33
lib/coffee_script/repl.js
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
(function(){
|
||||||
|
var coffee, prompt, quit, readline, run;
|
||||||
|
// A CoffeeScript port/version of the Node.js REPL.
|
||||||
|
// Required modules.
|
||||||
|
coffee = require('./coffee-script');
|
||||||
|
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 coffee.compile(code, run);
|
||||||
|
};
|
||||||
|
// 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.open();
|
||||||
|
process.stdio.addListener('data', readline);
|
||||||
|
print(prompt);
|
||||||
|
})();
|
15
src/coffee-script.coffee
Normal file
15
src/coffee-script.coffee
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
# Executes the `coffee` Ruby program to convert from CoffeeScript to JavaScript.
|
||||||
|
|
||||||
|
sys: require('sys')
|
||||||
|
|
||||||
|
exports.compile: (code, callback) ->
|
||||||
|
js: ''
|
||||||
|
coffee: process.createChildProcess 'coffee', ['--eval', '--no-wrap', '--globals']
|
||||||
|
coffee.addListener 'output', (results) ->
|
||||||
|
js += results if results?
|
||||||
|
coffee.addListener 'exit', ->
|
||||||
|
callback(js)
|
||||||
|
coffee.write(code)
|
||||||
|
coffee.close()
|
||||||
|
|
||||||
|
|
26
src/repl.coffee
Normal file
26
src/repl.coffee
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
# A CoffeeScript port/version of the Node.js REPL.
|
||||||
|
|
||||||
|
# Required modules.
|
||||||
|
coffee: require './coffee-script'
|
||||||
|
process.mixin require 'sys'
|
||||||
|
|
||||||
|
# Shortcut variables.
|
||||||
|
prompt: 'coffee> '
|
||||||
|
quit: -> process.stdio.close()
|
||||||
|
|
||||||
|
# The main REPL function. Called everytime a line of code is entered.
|
||||||
|
readline: (code) -> coffee.compile code, run
|
||||||
|
|
||||||
|
# Attempt to evaluate the command. If there's an exception, print it.
|
||||||
|
run: (js) ->
|
||||||
|
try
|
||||||
|
val: eval(js)
|
||||||
|
p val if val isnt undefined
|
||||||
|
catch err
|
||||||
|
puts err.stack or err.toString()
|
||||||
|
print prompt
|
||||||
|
|
||||||
|
# Start up the REPL.
|
||||||
|
process.stdio.open()
|
||||||
|
process.stdio.addListener 'data', readline
|
||||||
|
print prompt
|
Loading…
Add table
Reference in a new issue