diff --git a/lib/coffee_script/coffee-script.js b/lib/coffee_script/coffee-script.js new file mode 100644 index 00000000..8ed11447 --- /dev/null +++ b/lib/coffee_script/coffee-script.js @@ -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(); + }; +})(); \ No newline at end of file diff --git a/lib/coffee_script/command_line.rb b/lib/coffee_script/command_line.rb index 2427169a..b2e4dba1 100644 --- a/lib/coffee_script/command_line.rb +++ b/lib/coffee_script/command_line.rb @@ -118,7 +118,7 @@ Usage: def launch_repl exec "#{LAUNCHER}" 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) end diff --git a/lib/coffee_script/repl.js b/lib/coffee_script/repl.js new file mode 100644 index 00000000..0a0dfee4 --- /dev/null +++ b/lib/coffee_script/repl.js @@ -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); +})(); \ No newline at end of file diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee new file mode 100644 index 00000000..602032aa --- /dev/null +++ b/src/coffee-script.coffee @@ -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() + + diff --git a/src/repl.coffee b/src/repl.coffee new file mode 100644 index 00000000..1ab9c70b --- /dev/null +++ b/src/repl.coffee @@ -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 \ No newline at end of file