From f5a37035cf6d285372f26780344799da3ea9a4ea Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Fri, 29 Jan 2010 23:30:54 -0500 Subject: [PATCH] node conversion finished, narwhal removed. --- Rakefile | 7 +- documentation/index.html.erb | 8 +- examples/beautiful_code/binary_search.coffee | 6 +- .../beautiful_code/quicksort_runtime.coffee | 6 +- .../regular_expression_matcher.coffee | 12 +-- .../computer_science/binary_search.coffee | 8 +- examples/computer_science/bubble_sort.coffee | 4 +- examples/computer_science/linked_list.coffee | 20 ++--- .../computer_science/luhn_algorithm.coffee | 6 +- examples/computer_science/merge_sort.coffee | 4 +- .../computer_science/selection_sort.coffee | 4 +- index.html | 8 +- lib/coffee_script/coffee-script.js | 13 +++ lib/coffee_script/command_line.rb | 15 ++-- .../narwhal/coffee-script.coffee | 62 -------------- .../narwhal/lib/coffee-script.js | 80 ------------------- .../narwhal/lib/coffee-script/loader.js | 21 ----- lib/coffee_script/narwhal/loader.coffee | 19 ----- lib/coffee_script/runner.js | 10 +++ package.json | 4 +- src/coffee-script.coffee | 8 ++ src/runner.coffee | 6 ++ test/fixtures/execution/test_arguments.coffee | 10 +-- .../execution/test_array_comprehension.coffee | 16 ++-- .../fixtures/execution/test_assignment.coffee | 6 +- test/fixtures/execution/test_blocks.coffee | 2 +- .../execution/test_calling_super.coffee | 4 +- .../execution/test_chained_calls.coffee | 6 +- .../test_destructuring_assignment.coffee | 22 ++--- .../fixtures/execution/test_everything.coffee | 2 +- test/fixtures/execution/test_existence.coffee | 24 +++--- .../execution/test_expressions.coffee | 6 +- .../execution/test_fancy_if_statement.coffee | 6 +- test/fixtures/execution/test_functions.coffee | 36 ++++----- .../execution/test_funky_comments.coffee | 2 +- test/fixtures/execution/test_heredocs.coffee | 12 +-- .../execution/test_lexical_scope.coffee | 4 +- test/fixtures/execution/test_literals.coffee | 14 ++-- .../test_nested_comprehensions.coffee | 6 +- .../execution/test_newline_escaping.coffee | 2 +- .../fixtures/execution/test_operations.coffee | 10 +-- .../execution/test_range_comprehension.coffee | 6 +- .../execution/test_ranges_and_slices.coffee | 4 +- test/fixtures/execution/test_splats.coffee | 10 +-- test/fixtures/execution/test_splices.coffee | 2 +- test/fixtures/execution/test_switch.coffee | 10 +-- test/fixtures/execution/test_while.coffee | 6 +- .../statements_as_expressions.coffee | 6 +- 48 files changed, 208 insertions(+), 357 deletions(-) delete mode 100644 lib/coffee_script/narwhal/coffee-script.coffee delete mode 100644 lib/coffee_script/narwhal/lib/coffee-script.js delete mode 100644 lib/coffee_script/narwhal/lib/coffee-script/loader.js delete mode 100644 lib/coffee_script/narwhal/loader.coffee create mode 100644 lib/coffee_script/runner.js create mode 100644 src/runner.coffee diff --git a/Rakefile b/Rakefile index ce2a0b97..bcf10d30 100644 --- a/Rakefile +++ b/Rakefile @@ -17,10 +17,9 @@ namespace :build do sh "racc #{args[:racc_args]} -o lib/coffee_script/parser.rb lib/coffee_script/grammar.y" end - desc "Compile the Narwhal interface for --interactive and --run" - task :narwhal do - sh "bin/coffee lib/coffee_script/narwhal/*.coffee -o lib/coffee_script/narwhal/lib/coffee-script" - sh "mv lib/coffee_script/narwhal/lib/coffee-script/coffee-script.js lib/coffee_script/narwhal/lib/coffee-script.js" + desc "Continually compile the CoffeeScript/Node.js components with --watch" + task :node do + sh "bin/coffee -w src/*.coffee -o lib/coffee_script/" end desc "Compile and install the Ultraviolet syntax highlighter" diff --git a/documentation/index.html.erb b/documentation/index.html.erb index aefebf9a..4adcd47e 100644 --- a/documentation/index.html.erb +++ b/documentation/index.html.erb @@ -118,7 +118,7 @@ gem install coffee-script Installing the gem provides the coffee command, which can be used to compile CoffeeScript .coffee files into JavaScript, as well as debug them. In conjunction with - Narwhal, the coffee + Node.js, the coffee command also provides direct evaluation and an interactive REPL. When compiling to JavaScript, coffee writes the output as .js files in the same directory by default, but output @@ -130,14 +130,14 @@ gem install coffee-script -i, --interactive Launch an interactive CoffeeScript session. - Requires Narwhal. + Requires Node.js. -r, --run Compile and execute scripts without saving the intermediate - JavaScript. Requires Narwhal. + JavaScript. Requires Node.js. @@ -194,7 +194,7 @@ gem install coffee-script -n, --no-wrap Compile the JavaScript without the top-level function safety wrapper. - (Used for CoffeeScript as a Narwhal module.) + (Used for CoffeeScript as a Node.js module.) diff --git a/examples/beautiful_code/binary_search.coffee b/examples/beautiful_code/binary_search.coffee index 9c6e02d1..8a1456a0 100644 --- a/examples/beautiful_code/binary_search.coffee +++ b/examples/beautiful_code/binary_search.coffee @@ -11,6 +11,6 @@ index: (list, target) -> if val < target then low: mid + 1 else high: mid return -1 -print(2 is index([10, 20, 30, 40, 50], 30)) -print(4 is index([-97, 35, 67, 88, 1200], 1200)) -print(0 is index([0, 45, 70], 0)) \ No newline at end of file +puts 2 is index([10, 20, 30, 40, 50], 30) +puts 4 is index([-97, 35, 67, 88, 1200], 1200) +puts 0 is index([0, 45, 70], 0) \ No newline at end of file diff --git a/examples/beautiful_code/quicksort_runtime.coffee b/examples/beautiful_code/quicksort_runtime.coffee index affd775a..6e9677f2 100644 --- a/examples/beautiful_code/quicksort_runtime.coffee +++ b/examples/beautiful_code/quicksort_runtime.coffee @@ -8,6 +8,6 @@ runtime: (N) -> t: n - 1 + sum / n t -print(runtime(3) is 2.6666666666666665) -print(runtime(5) is 7.4) -print(runtime(8) is 16.92142857142857) +puts runtime(3) is 2.6666666666666665 +puts runtime(5) is 7.4 +puts runtime(8) is 16.92142857142857 diff --git a/examples/beautiful_code/regular_expression_matcher.coffee b/examples/beautiful_code/regular_expression_matcher.coffee index 4ef8237e..903c15d2 100644 --- a/examples/beautiful_code/regular_expression_matcher.coffee +++ b/examples/beautiful_code/regular_expression_matcher.coffee @@ -26,9 +26,9 @@ match_star: (c, regexp, text) -> return false unless text and (text[0] is c or c is '.') text: text.slice(1) -print(match("ex", "some text")) -print(match("s..t", "spit")) -print(match("^..t", "buttercup")) -print(match("i..$", "cherries")) -print(match("o*m", "vrooooommm!")) -print(match("^hel*o$", "hellllllo")) \ No newline at end of file +puts match("ex", "some text") +puts match("s..t", "spit") +puts match("^..t", "buttercup") +puts match("i..$", "cherries") +puts match("o*m", "vrooooommm!") +puts match("^hel*o$", "hellllllo") \ No newline at end of file diff --git a/examples/computer_science/binary_search.coffee b/examples/computer_science/binary_search.coffee index 443eaaa0..2c5e020e 100644 --- a/examples/computer_science/binary_search.coffee +++ b/examples/computer_science/binary_search.coffee @@ -19,7 +19,7 @@ binary_search: (items, value) -> # Test the function. -print(2 is binary_search([10, 20, 30, 40, 50], 30)) -print(4 is binary_search([-97, 35, 67, 88, 1200], 1200)) -print(0 is binary_search([0, 45, 70], 0)) -print(-1 is binary_search([0, 45, 70], 10)) \ No newline at end of file +puts(2 is binary_search([10, 20, 30, 40, 50], 30)) +puts(4 is binary_search([-97, 35, 67, 88, 1200], 1200)) +puts(0 is binary_search([0, 45, 70], 0)) +puts(-1 is binary_search([0, 45, 70], 10)) \ No newline at end of file diff --git a/examples/computer_science/bubble_sort.coffee b/examples/computer_science/bubble_sort.coffee index f671bedd..f27a04df 100644 --- a/examples/computer_science/bubble_sort.coffee +++ b/examples/computer_science/bubble_sort.coffee @@ -7,5 +7,5 @@ bubble_sort: (list) -> # Test the function. -print(bubble_sort([3, 2, 1]).join(' ') is '1 2 3') -print(bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9') \ No newline at end of file +puts(bubble_sort([3, 2, 1]).join(' ') is '1 2 3') +puts(bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9') \ No newline at end of file diff --git a/examples/computer_science/linked_list.coffee b/examples/computer_science/linked_list.coffee index 6af3fddf..1d38e175 100644 --- a/examples/computer_science/linked_list.coffee +++ b/examples/computer_science/linked_list.coffee @@ -91,16 +91,16 @@ LinkedList::toString: -> this.toArray().toString() list: new LinkedList() list.add("Hi") -print(list.size() is 1) -print(list.item(0) is "Hi") -print(list.item(1) is null) +puts(list.size() is 1) +puts(list.item(0) is "Hi") +puts(list.item(1) is null) list: new LinkedList() list.add("zero").add("one").add("two") -print(list.size() is 3) -print(list.item(2) is "two") -print(list.remove(1) is "one") -print(list.item(0) is "zero") -print(list.item(1) is "two") -print(list.size() is 2) -print(list.item(-10) is null) +puts(list.size() is 3) +puts(list.item(2) is "two") +puts(list.remove(1) is "one") +puts(list.item(0) is "zero") +puts(list.item(1) is "two") +puts(list.size() is 2) +puts(list.item(-10) is null) diff --git a/examples/computer_science/luhn_algorithm.coffee b/examples/computer_science/luhn_algorithm.coffee index 042b249d..ceb4f3dd 100644 --- a/examples/computer_science/luhn_algorithm.coffee +++ b/examples/computer_science/luhn_algorithm.coffee @@ -31,6 +31,6 @@ is_valid_identifier: (identifier) -> # Tests. -print(is_valid_identifier("49927398716") is true) -print(is_valid_identifier("4408041234567893") is true) -print(is_valid_identifier("4408041234567890") is false) +puts(is_valid_identifier("49927398716") is true) +puts(is_valid_identifier("4408041234567893") is true) +puts(is_valid_identifier("4408041234567890") is false) diff --git a/examples/computer_science/merge_sort.coffee b/examples/computer_science/merge_sort.coffee index fb3f8ce8..77eabf97 100644 --- a/examples/computer_science/merge_sort.coffee +++ b/examples/computer_science/merge_sort.coffee @@ -15,5 +15,5 @@ merge_sort: (list) -> # Test the function. -print(merge_sort([3, 2, 1]).join(' ') is '1 2 3') -print(merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9') \ No newline at end of file +puts(merge_sort([3, 2, 1]).join(' ') is '1 2 3') +puts(merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9') \ No newline at end of file diff --git a/examples/computer_science/selection_sort.coffee b/examples/computer_science/selection_sort.coffee index f4b970a1..b94c9704 100644 --- a/examples/computer_science/selection_sort.coffee +++ b/examples/computer_science/selection_sort.coffee @@ -19,5 +19,5 @@ selection_sort: (list) -> # Test the function. -print(selection_sort([3, 2, 1]).join(' ') is '1 2 3') -print(selection_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9') \ No newline at end of file +puts(selection_sort([3, 2, 1]).join(' ') is '1 2 3') +puts(selection_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9') \ No newline at end of file diff --git a/index.html b/index.html index c780982d..1e936013 100644 --- a/index.html +++ b/index.html @@ -215,7 +215,7 @@ gem install coffee-script Installing the gem provides the coffee command, which can be used to compile CoffeeScript .coffee files into JavaScript, as well as debug them. In conjunction with - Narwhal, the coffee + Node.js, the coffee command also provides direct evaluation and an interactive REPL. When compiling to JavaScript, coffee writes the output as .js files in the same directory by default, but output @@ -227,14 +227,14 @@ gem install coffee-script -i, --interactive Launch an interactive CoffeeScript session. - Requires Narwhal. + Requires Node.js. -r, --run Compile and execute scripts without saving the intermediate - JavaScript. Requires Narwhal. + JavaScript. Requires Node.js. @@ -291,7 +291,7 @@ gem install coffee-script -n, --no-wrap Compile the JavaScript without the top-level function safety wrapper. - (Used for CoffeeScript as a Narwhal module.) + (Used for CoffeeScript as a Node.js module.) diff --git a/lib/coffee_script/coffee-script.js b/lib/coffee_script/coffee-script.js index 8ed11447..832be407 100644 --- a/lib/coffee_script/coffee-script.js +++ b/lib/coffee_script/coffee-script.js @@ -17,4 +17,17 @@ coffee.write(code); return coffee.close(); }; + exports.compile_files = function compile_files(paths, callback) { + var coffee, js; + js = ''; + coffee = process.createChildProcess('coffee', ['--print'].concat(paths)); + coffee.addListener('output', function(results) { + if ((typeof results !== "undefined" && results !== null)) { + return js += results; + } + }); + return coffee.addListener('exit', function() { + return callback(js); + }); + }; })(); \ No newline at end of file diff --git a/lib/coffee_script/command_line.rb b/lib/coffee_script/command_line.rb index 1a1b26e7..32cafa6f 100644 --- a/lib/coffee_script/command_line.rb +++ b/lib/coffee_script/command_line.rb @@ -28,9 +28,6 @@ Usage: # Path to the root of the CoffeeScript install. ROOT = File.expand_path(File.dirname(__FILE__) + '/../..') - # Command to execute in Narwhal - LAUNCHER = "narwhal -p #{ROOT} -e 'require(\"coffee-script\").run(system.args);'" - # Run the CommandLine off the contents of ARGV. def initialize @mtimes = {} @@ -114,7 +111,7 @@ Usage: puts js end - # Use Narwhal to run an interactive CoffeeScript session. + # Use Node.js to run an interactive CoffeeScript session. def launch_repl exec "node #{ROOT}/lib/coffee_script/repl.js" rescue Errno::ENOENT @@ -122,12 +119,12 @@ Usage: exit(1) end - # Use Narwhal to compile and execute CoffeeScripts. + # Use Node.js to compile and execute CoffeeScripts. def run_scripts sources = @sources.join(' ') - exec "#{LAUNCHER} #{sources}" + exec "node #{ROOT}/lib/coffee_script/runner.js #{sources}" rescue Errno::ENOENT - puts "Error: Narwhal must be installed in order to execute CoffeeScripts." + puts "Error: Node.js must be installed in order to execute scripts." exit(1) end @@ -168,10 +165,10 @@ Usage: def parse_options @options = {} @option_parser = OptionParser.new do |opts| - opts.on('-i', '--interactive', 'run a CoffeeScript REPL (requires Narwhal)') do |i| + opts.on('-i', '--interactive', 'run a CoffeeScript REPL (requires Node.js)') do |i| @options[:interactive] = true end - opts.on('-r', '--run', 'compile and run a script (requires Narwhal)') do |r| + opts.on('-r', '--run', 'compile and run a script (requires Node.js)') do |r| @options[:run] = true end opts.on('-o', '--output [DIR]', 'set the directory for compiled JavaScript') do |d| diff --git a/lib/coffee_script/narwhal/coffee-script.coffee b/lib/coffee_script/narwhal/coffee-script.coffee deleted file mode 100644 index e9f49649..00000000 --- a/lib/coffee_script/narwhal/coffee-script.coffee +++ /dev/null @@ -1,62 +0,0 @@ -# This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.coffee - -# Executes the `coffee` Ruby program to convert from CoffeeScript -# to Javascript. Eventually this will hopefully happen entirely within JS. - -# Require external dependencies. -OS: require('os') -File: require('file') -Readline: require('readline') - -# The path to the CoffeeScript Compiler. -coffeePath: File.path(module.path).dirname().dirname().dirname().dirname().dirname().join('bin', 'coffee') - -# Our general-purpose error handler. -checkForErrors: (coffeeProcess) -> - return true if coffeeProcess.wait() is 0 - system.stderr.print(coffeeProcess.stderr.read()) - throw new Error("CoffeeScript compile error") - -# Run a simple REPL, round-tripping to the CoffeeScript compiler for every -# command. -exports.run: (args) -> - if args.length - for path, i in args - exports.evalCS(File.read(path)) - delete args[i] - return true - - while true - try - system.stdout.write('coffee> ').flush() - result: exports.evalCS(Readline.readline(), ['--globals']) - print(result) if result isnt undefined - catch e - print(e) - -# Compile a given CoffeeScript file into JavaScript. -exports.compileFile: (path) -> - coffee: OS.popen([coffeePath, "--print", "--no-wrap", path]) - checkForErrors(coffee) - coffee.stdout.read() - -# Compile a string of CoffeeScript into JavaScript. -exports.compile: (source, flags) -> - coffee: OS.popen([coffeePath, "--eval", "--no-wrap"].concat(flags or [])) - coffee.stdin.write(source).flush().close() - checkForErrors(coffee) - coffee.stdout.read() - -# Evaluating a string of CoffeeScript first compiles it externally. -exports.evalCS: (source, flags) -> - eval(exports.compile(source, flags)) - -# Make a factory for the CoffeeScript environment. -exports.makeNarwhalFactory: (path) -> - code: exports.compileFile(path) - factoryText: "function(require,exports,module,system,print){" + code + "/**/\n}" - if system.engine is "rhino" - Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null) - else - # eval requires parentheses, but parentheses break compileFunction. - eval("(" + factoryText + ")") diff --git a/lib/coffee_script/narwhal/lib/coffee-script.js b/lib/coffee_script/narwhal/lib/coffee-script.js deleted file mode 100644 index b2283d0f..00000000 --- a/lib/coffee_script/narwhal/lib/coffee-script.js +++ /dev/null @@ -1,80 +0,0 @@ -(function(){ - var File, OS, Readline, checkForErrors, coffeePath; - // This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.coffee - // Executes the `coffee` Ruby program to convert from CoffeeScript - // to Javascript. Eventually this will hopefully happen entirely within JS. - // Require external dependencies. - OS = require('os'); - File = require('file'); - Readline = require('readline'); - // The path to the CoffeeScript Compiler. - coffeePath = File.path(module.path).dirname().dirname().dirname().dirname().dirname().join('bin', 'coffee'); - // Our general-purpose error handler. - checkForErrors = function checkForErrors(coffeeProcess) { - if (coffeeProcess.wait() === 0) { - return true; - } - system.stderr.print(coffeeProcess.stderr.read()); - throw new Error("CoffeeScript compile error"); - }; - // Run a simple REPL, round-tripping to the CoffeeScript compiler for every - // command. - exports.run = function run(args) { - var __a, __b, i, path, result; - if (args.length) { - __a = args; - for (i = 0; i < __a.length; i++) { - path = __a[i]; - exports.evalCS(File.read(path)); - delete args[i]; - } - return true; - } - __b = []; - while (true) { - __b.push((function() { - try { - system.stdout.write('coffee> ').flush(); - result = exports.evalCS(Readline.readline(), ['--globals']); - if (result !== undefined) { - return print(result); - } - } catch (e) { - return print(e); - } - }).call(this)); - } - return __b; - }; - // Compile a given CoffeeScript file into JavaScript. - exports.compileFile = function compileFile(path) { - var coffee; - coffee = OS.popen([coffeePath, "--print", "--no-wrap", path]); - checkForErrors(coffee); - return coffee.stdout.read(); - }; - // Compile a string of CoffeeScript into JavaScript. - exports.compile = function compile(source, flags) { - var coffee; - coffee = OS.popen([coffeePath, "--eval", "--no-wrap"].concat(flags || [])); - coffee.stdin.write(source).flush().close(); - checkForErrors(coffee); - return coffee.stdout.read(); - }; - // Evaluating a string of CoffeeScript first compiles it externally. - exports.evalCS = function evalCS(source, flags) { - return eval(exports.compile(source, flags)); - }; - // Make a factory for the CoffeeScript environment. - exports.makeNarwhalFactory = function makeNarwhalFactory(path) { - var code, factoryText; - code = exports.compileFile(path); - factoryText = "function(require,exports,module,system,print){" + code + "/**/\n}"; - if (system.engine === "rhino") { - return Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null); - } else { - // eval requires parentheses, but parentheses break compileFunction. - return eval("(" + factoryText + ")"); - } - }; -})(); \ No newline at end of file diff --git a/lib/coffee_script/narwhal/lib/coffee-script/loader.js b/lib/coffee_script/narwhal/lib/coffee-script/loader.js deleted file mode 100644 index 9fc8354b..00000000 --- a/lib/coffee_script/narwhal/lib/coffee-script/loader.js +++ /dev/null @@ -1,21 +0,0 @@ -(function(){ - var coffeescript, factories, loader; - // This (javascript) file is generated from lib/coffee_script/narwhal/loader.coffee - coffeescript = null; - factories = { - }; - loader = { - // Reload the coffee-script environment from source. - reload: function reload(topId, path) { - coffeescript = coffeescript || require('coffee-script'); - return factories[topId] = function() { - return coffeescript.makeNarwhalFactory(path); - }; - }, - // Ensure that the coffee-script environment is loaded. - load: function load(topId, path) { - return factories[topId] = factories[topId] || this.reload(topId, path); - } - }; - require.loader.loaders.unshift([".coffee", loader]); -})(); \ No newline at end of file diff --git a/lib/coffee_script/narwhal/loader.coffee b/lib/coffee_script/narwhal/loader.coffee deleted file mode 100644 index 5b80e733..00000000 --- a/lib/coffee_script/narwhal/loader.coffee +++ /dev/null @@ -1,19 +0,0 @@ -# This (javascript) file is generated from lib/coffee_script/narwhal/loader.coffee - -coffeescript: null -factories: {} - -loader: { - - # Reload the coffee-script environment from source. - reload: (topId, path) -> - coffeescript ||= require('coffee-script') - factories[topId]: -> coffeescript.makeNarwhalFactory(path) - - # Ensure that the coffee-script environment is loaded. - load: (topId, path) -> - factories[topId] ||= this.reload(topId, path) - -} - -require.loader.loaders.unshift([".coffee", loader]) diff --git a/lib/coffee_script/runner.js b/lib/coffee_script/runner.js new file mode 100644 index 00000000..4623b2a8 --- /dev/null +++ b/lib/coffee_script/runner.js @@ -0,0 +1,10 @@ +(function(){ + var coffee, paths; + coffee = require('./coffee-script'); + process.mixin(require('sys')); + paths = process.ARGV; + paths = paths.slice(2, paths.length); + coffee.compile_files(paths, function(js) { + return eval(js); + }); +})(); \ No newline at end of file diff --git a/package.json b/package.json index 7db27d07..e32030ac 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "coffee-script", - "lib": "lib/coffee_script/narwhal/lib", - "preload": ["coffee-script/loader"], + "lib": "lib/coffee_script", + "preload": ["loader"], "description": "Unfancy JavaScript", "keywords": ["javascript", "language"], "author": "Jeremy Ashkenas", diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 602032aa..dd0bf89b 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -12,4 +12,12 @@ exports.compile: (code, callback) -> coffee.write(code) coffee.close() +exports.compile_files: (paths, callback) -> + js: '' + coffee: process.createChildProcess 'coffee', ['--print'].concat(paths) + coffee.addListener 'output', (results) -> + js += results if results? + coffee.addListener 'exit', -> + callback(js) + diff --git a/src/runner.coffee b/src/runner.coffee new file mode 100644 index 00000000..1d73ac64 --- /dev/null +++ b/src/runner.coffee @@ -0,0 +1,6 @@ +coffee: require './coffee-script' +process.mixin require 'sys' + +paths: process.ARGV +paths: paths[2...paths.length] +coffee.compile_files paths, (js) -> eval(js) diff --git a/test/fixtures/execution/test_arguments.coffee b/test/fixtures/execution/test_arguments.coffee index 71a6c9c7..00580a71 100644 --- a/test/fixtures/execution/test_arguments.coffee +++ b/test/fixtures/execution/test_arguments.coffee @@ -4,12 +4,12 @@ area: (x, y, x1, y1) -> x: y: 10 x1: y1: 20 -print area(x, y, x1, y1) is 100 +puts area(x, y, x1, y1) is 100 -print(area(x, y, +puts(area(x, y, x1, y1) is 100) -print(area( +puts(area( x y x1 @@ -19,7 +19,7 @@ print(area( # Arguments are turned into arrays. curried: -> - print area.apply(this, arguments.concat(20, 20)) is 100 + puts area.apply(this, arguments.concat(20, 20)) is 100 curried 10, 10 @@ -29,4 +29,4 @@ func: -> arguments: 25 arguments -print func(100) is 25 +puts func(100) is 25 diff --git a/test/fixtures/execution/test_array_comprehension.coffee b/test/fixtures/execution/test_array_comprehension.coffee index 03c1e1f7..a73b7356 100644 --- a/test/fixtures/execution/test_array_comprehension.coffee +++ b/test/fixtures/execution/test_array_comprehension.coffee @@ -1,15 +1,15 @@ nums: n * n for n in [1, 2, 3] when n % 2 isnt 0 results: n * 2 for n in nums -print results.join(',') is '2,18' +puts results.join(',') is '2,18' obj: {one: 1, two: 2, three: 3} names: prop + '!' for prop of obj odds: prop + '!' for prop, value of obj when value % 2 isnt 0 -print names.join(' ') is "one! two! three!" -print odds.join(' ') is "one! three!" +puts names.join(' ') is "one! two! three!" +puts odds.join(' ') is "one! three!" evens: for num in [1, 2, 3, 4, 5, 6] when num % 2 is 0 @@ -17,12 +17,12 @@ evens: for num in [1, 2, 3, 4, 5, 6] when num % 2 is 0 num -= 2 num * -1 -print evens.join(', ') is '4, 6, 8' +puts evens.join(', ') is '4, 6, 8' # Make sure that the "in" operator still works. -print 2 in evens +puts 2 in evens # When functions are being defined within the body of a comprehension, make @@ -37,6 +37,6 @@ for method in methods obj[name]: -> "I'm " + name -print obj.one() is "I'm one" -print obj.two() is "I'm two" -print obj.three() is "I'm three" +puts obj.one() is "I'm one" +puts obj.two() is "I'm two" +puts obj.three() is "I'm three" diff --git a/test/fixtures/execution/test_assignment.coffee b/test/fixtures/execution/test_assignment.coffee index 3fd42dd8..ebe72da7 100644 --- a/test/fixtures/execution/test_assignment.coffee +++ b/test/fixtures/execution/test_assignment.coffee @@ -7,7 +7,7 @@ catch error result2: try nonexistent * missing catch error then true -print result is true and result2 is true +puts result is true and result2 is true # Assign to conditional. @@ -16,8 +16,8 @@ get_x: -> 10 if x: get_x() then 100 -print x is 10 +puts x is 10 x: if get_x() then 100 -print x is 100 \ No newline at end of file +puts x is 100 \ No newline at end of file diff --git a/test/fixtures/execution/test_blocks.coffee b/test/fixtures/execution/test_blocks.coffee index 2efd8aa8..98097dcc 100644 --- a/test/fixtures/execution/test_blocks.coffee +++ b/test/fixtures/execution/test_blocks.coffee @@ -1,4 +1,4 @@ results: [1, 2, 3].map (x) -> x * x -print results.join(' ') is '1 4 9' \ No newline at end of file +puts results.join(' ') is '1 4 9' \ No newline at end of file diff --git a/test/fixtures/execution/test_calling_super.coffee b/test/fixtures/execution/test_calling_super.coffee index 546744b2..4bf626e2 100644 --- a/test/fixtures/execution/test_calling_super.coffee +++ b/test/fixtures/execution/test_calling_super.coffee @@ -20,7 +20,7 @@ ThirdChild::func: (string) -> result: (new ThirdChild()).func 'four' -print result is 'zero/one/two/three/four' +puts result is 'zero/one/two/three/four' TopClass: (arg) -> @@ -35,4 +35,4 @@ SubClass: -> SuperClass extends TopClass SubClass extends SuperClass -print((new SubClass()).prop is 'top-super-sub') \ No newline at end of file +puts((new SubClass()).prop is 'top-super-sub') \ No newline at end of file diff --git a/test/fixtures/execution/test_chained_calls.coffee b/test/fixtures/execution/test_chained_calls.coffee index 8f0b7043..eaad589a 100644 --- a/test/fixtures/execution/test_chained_calls.coffee +++ b/test/fixtures/execution/test_chained_calls.coffee @@ -3,7 +3,7 @@ identity_wrap: (x) -> result: identity_wrap(identity_wrap(true))()() -print result +puts result str: 'god' @@ -14,7 +14,7 @@ result: str. reverse(). reverse() -print result.join('') is 'dog' +puts result.join('') is 'dog' result: str .split('') @@ -22,4 +22,4 @@ result: str .reverse() .reverse() -print result.join('') is 'dog' \ No newline at end of file +puts result.join('') is 'dog' \ No newline at end of file diff --git a/test/fixtures/execution/test_destructuring_assignment.coffee b/test/fixtures/execution/test_destructuring_assignment.coffee index 3aa454c3..da5ab0fc 100644 --- a/test/fixtures/execution/test_destructuring_assignment.coffee +++ b/test/fixtures/execution/test_destructuring_assignment.coffee @@ -3,26 +3,26 @@ b: -2 [a, b]: [b, a] -print a is -2 -print b is -1 +puts a is -2 +puts b is -1 arr: [1, 2, 3] [a, b, c]: arr -print a is 1 -print b is 2 -print c is 3 +puts a is 1 +puts b is 2 +puts c is 3 obj: {x: 10, y: 20, z: 30} {x: a, y: b, z: c}: obj -print a is 10 -print b is 20 -print c is 30 +puts a is 10 +puts b is 20 +puts c is 30 person: { @@ -42,8 +42,8 @@ person: { {name: a, family: {brother: {addresses: [one, {city: b}]}}}: person -print a is "Bob" -print b is "Moquasset NY, 10021" +puts a is "Bob" +puts b is "Moquasset NY, 10021" test: { @@ -59,4 +59,4 @@ test: { {person: {address: [ignore, addr...]}}: test -print addr.join(', ') is "Street 101, Apt 101, City 101" \ No newline at end of file +puts addr.join(', ') is "Street 101, Apt 101, City 101" \ No newline at end of file diff --git a/test/fixtures/execution/test_everything.coffee b/test/fixtures/execution/test_everything.coffee index c6b54b95..c52ae0a1 100644 --- a/test/fixtures/execution/test_everything.coffee +++ b/test/fixtures/execution/test_everything.coffee @@ -26,4 +26,4 @@ func: -> c.single: c.list[1..1][0] -print func() is '-' +puts func() is '-' diff --git a/test/fixtures/execution/test_existence.coffee b/test/fixtures/execution/test_existence.coffee index 253a8bf2..eb7f5c0e 100644 --- a/test/fixtures/execution/test_existence.coffee +++ b/test/fixtures/execution/test_existence.coffee @@ -1,8 +1,8 @@ -print(if my_special_variable? then false else true) +puts(if my_special_variable? then false else true) my_special_variable: false -print(if my_special_variable? then true else false) +puts(if my_special_variable? then true else false) # Existential assignment. @@ -12,7 +12,7 @@ a: null a ?= 10 b ?= 10 -print a is 10 and b is 10 +puts a is 10 and b is 10 # The existential operator. @@ -20,7 +20,7 @@ print a is 10 and b is 10 z: null x: z ? "EX" -print z is null and x is "EX" +puts z is null and x is "EX" # Only evaluate once. @@ -30,7 +30,7 @@ get_next_node: -> throw "up" if counter counter++ -print(if get_next_node()? then true else false) +puts(if get_next_node()? then true else false) # Existence chains, soaking up undefined properties: @@ -39,17 +39,17 @@ obj: { prop: "hello" } -print obj?.prop is "hello" +puts obj?.prop is "hello" -print obj?.prop?.non?.existent?.property is undefined +puts obj?.prop?.non?.existent?.property is undefined # Soaks and caches method calls as well. arr: ["--", "----"] -print arr.pop()?.length is 4 -print arr.pop()?.length is 2 -print arr.pop()?.length is undefined -print arr[0]?.length is undefined -print arr.pop()?.length?.non?.existent()?.property is undefined +puts arr.pop()?.length is 4 +puts arr.pop()?.length is 2 +puts arr.pop()?.length is undefined +puts arr[0]?.length is undefined +puts arr.pop()?.length?.non?.existent()?.property is undefined diff --git a/test/fixtures/execution/test_expressions.coffee b/test/fixtures/execution/test_expressions.coffee index 9686d1a4..eee44179 100644 --- a/test/fixtures/execution/test_expressions.coffee +++ b/test/fixtures/execution/test_expressions.coffee @@ -9,7 +9,7 @@ findit: (items) -> for item in items return item if item is "bacon" -print findit(items) is "bacon" +puts findit(items) is "bacon" # When when a closure wrapper is generated for expression conversion, make sure @@ -26,5 +26,5 @@ obj: { this.num } -print obj.num is obj.func() -print obj.num is obj.result \ No newline at end of file +puts obj.num is obj.func() +puts obj.num is obj.result \ No newline at end of file diff --git a/test/fixtures/execution/test_fancy_if_statement.coffee b/test/fixtures/execution/test_fancy_if_statement.coffee index f71dd511..978c9def 100644 --- a/test/fixtures/execution/test_fancy_if_statement.coffee +++ b/test/fixtures/execution/test_fancy_if_statement.coffee @@ -7,10 +7,10 @@ result: if a if d true -print result +puts result first: if false then false else second: if false then false else true -print first -print second \ No newline at end of file +puts first +puts second \ No newline at end of file diff --git a/test/fixtures/execution/test_functions.coffee b/test/fixtures/execution/test_functions.coffee index 0aa4939b..9c63f39a 100644 --- a/test/fixtures/execution/test_functions.coffee +++ b/test/fixtures/execution/test_functions.coffee @@ -2,11 +2,11 @@ x: 1 y: {} y.x: -> 3 -print x is 1 -print typeof(y.x) is 'function' -print y.x instanceof Function -print y.x() is 3 -print y.x.name is 'x' +puts x is 1 +puts typeof(y.x) is 'function' +puts y.x instanceof Function +puts y.x() is 3 +puts y.x.name is 'x' # The empty function should not cause a syntax error. @@ -17,10 +17,10 @@ obj: { name: "Fred" bound: -> - (=> print(this.name is "Fred"))() + (=> puts(this.name is "Fred"))() unbound: -> - (-> print(!this.name?))() + (-> puts(!this.name?))() } obj.unbound() @@ -44,18 +44,18 @@ Math: { FastAdd: memoize (a, b) -> a + b } -print Math.Add(5, 5) is 10 -print Math.AnonymousAdd(10, 10) is 20 -print Math.FastAdd(20, 20) is 40 +puts Math.Add(5, 5) is 10 +puts Math.AnonymousAdd(10, 10) is 20 +puts Math.FastAdd(20, 20) is 40 # Parens are optional on simple function calls. -print 100 > 1 if 1 > 0 -print true unless false -print true for i in [1..3] +puts 100 > 1 if 1 > 0 +puts true unless false +puts true for i in [1..3] -print_func: (f) -> print(f()) -print_func -> true +puts_func: (f) -> puts(f()) +puts_func -> true # Optional parens can be used in a nested fashion. call: (func) -> func() @@ -64,7 +64,7 @@ result: call -> inner: call -> Math.Add(5, 5) -print result is 10 +puts result is 10 # And even with strange things like this: @@ -72,8 +72,8 @@ print result is 10 funcs: [(x) -> x, (x) -> x * x] result: funcs[1] 5 -print result is 25 +puts result is 25 result: ("hello".slice) 3 -print result is 'lo' \ No newline at end of file +puts result is 'lo' \ No newline at end of file diff --git a/test/fixtures/execution/test_funky_comments.coffee b/test/fixtures/execution/test_funky_comments.coffee index 9fbc030e..6a0940f1 100644 --- a/test/fixtures/execution/test_funky_comments.coffee +++ b/test/fixtures/execution/test_funky_comments.coffee @@ -18,4 +18,4 @@ switch 'string' code() # comment -print func() +puts func() diff --git a/test/fixtures/execution/test_heredocs.coffee b/test/fixtures/execution/test_heredocs.coffee index bb0becfd..5304fb26 100644 --- a/test/fixtures/execution/test_heredocs.coffee +++ b/test/fixtures/execution/test_heredocs.coffee @@ -3,7 +3,7 @@ a: """ on two lines """ -print a is "basic heredoc\non two lines" +puts a is "basic heredoc\non two lines" a: ''' @@ -12,12 +12,12 @@ a: ''' c ''' -print a is "a\n \"b\nc" +puts a is "a\n \"b\nc" a: '''one-liner''' -print a is 'one-liner' +puts a is 'one-liner' a: """ @@ -25,7 +25,7 @@ a: """ here """ -print a is "out\nhere" +puts a is "out\nhere" a: ''' @@ -34,7 +34,7 @@ a: ''' c ''' -print a is " a\n b\nc" +puts a is " a\n b\nc" a: ''' a @@ -43,4 +43,4 @@ a b c ''' -print a is "a\n\n\nb c" +puts a is "a\n\n\nb c" diff --git a/test/fixtures/execution/test_lexical_scope.coffee b/test/fixtures/execution/test_lexical_scope.coffee index 769b5096..5693ecd6 100644 --- a/test/fixtures/execution/test_lexical_scope.coffee +++ b/test/fixtures/execution/test_lexical_scope.coffee @@ -1,10 +1,10 @@ num: 1 + 2 + (a: 3) -print num is 6 +puts num is 6 result: if true false other: "result" -print result is "result" and other is "result" \ No newline at end of file +puts result is "result" and other is "result" \ No newline at end of file diff --git a/test/fixtures/execution/test_literals.coffee b/test/fixtures/execution/test_literals.coffee index c462b565..416d4986 100644 --- a/test/fixtures/execution/test_literals.coffee +++ b/test/fixtures/execution/test_literals.coffee @@ -1,37 +1,37 @@ a: [(x) -> x, (x) -> x * x] -print a.length is 2 +puts a.length is 2 regex: /match/i words: "I think there is a match in here." -print !!words.match(regex) +puts !!words.match(regex) neg: (3 -4) -print neg is -1 +puts neg is -1 func: -> return if true -print func() is null +puts func() is null str: "\\" reg: /\\/ -print reg(str) and str is '\\' +puts reg(str) and str is '\\' i: 10 while i -= 1 -print i is 0 +puts i is 0 money$: 'dollars' -print money$ is 'dollars' \ No newline at end of file +puts money$ is 'dollars' \ No newline at end of file diff --git a/test/fixtures/execution/test_nested_comprehensions.coffee b/test/fixtures/execution/test_nested_comprehensions.coffee index 8f50c490..77ed44b6 100644 --- a/test/fixtures/execution/test_nested_comprehensions.coffee +++ b/test/fixtures/execution/test_nested_comprehensions.coffee @@ -6,6 +6,6 @@ multi_liner: single_liner: [x, y] for y in [3..5] for x in [3..5] -print multi_liner.length is single_liner.length -print 5 is multi_liner[2][2][1] -print 5 is single_liner[2][2][1] +puts multi_liner.length is single_liner.length +puts 5 is multi_liner[2][2][1] +puts 5 is single_liner[2][2][1] diff --git a/test/fixtures/execution/test_newline_escaping.coffee b/test/fixtures/execution/test_newline_escaping.coffee index 9e91f452..d5c30899 100644 --- a/test/fixtures/execution/test_newline_escaping.coffee +++ b/test/fixtures/execution/test_newline_escaping.coffee @@ -3,4 +3,4 @@ six: 2 + 3 -print six is 6 \ No newline at end of file +puts six is 6 \ No newline at end of file diff --git a/test/fixtures/execution/test_operations.coffee b/test/fixtures/execution/test_operations.coffee index 86332804..86699ca2 100644 --- a/test/fixtures/execution/test_operations.coffee +++ b/test/fixtures/execution/test_operations.coffee @@ -1,12 +1,12 @@ # CoffeeScript's operations should be chainable, like Python's. -print 500 > 50 > 5 > -5 +puts 500 > 50 > 5 > -5 -print true is not false is true is not false +puts true is not false is true is not false -print 10 < 20 > 10 +puts 10 < 20 > 10 -print 50 > 10 > 5 is parseInt('5', 10) +puts 50 > 10 > 5 is parseInt('5', 10) # Make sure that each argument is only evaluated once, even if used @@ -15,4 +15,4 @@ print 50 > 10 > 5 is parseInt('5', 10) i: 0 func: -> i++ -print 1 > func() < 1 +puts 1 > func() < 1 diff --git a/test/fixtures/execution/test_range_comprehension.coffee b/test/fixtures/execution/test_range_comprehension.coffee index 947c27d2..67d41e19 100644 --- a/test/fixtures/execution/test_range_comprehension.coffee +++ b/test/fixtures/execution/test_range_comprehension.coffee @@ -5,16 +5,16 @@ negs: negs[0..2] result: nums.concat(negs).join(', ') -print result is '3, 6, 9, -20, -19, -18' +puts result is '3, 6, 9, -20, -19, -18' # Ensure that ranges are safe. This used to infinite loop: j = 5 result: for j in [j..(j+3)] j -print result.join(' ') is '5 6 7 8' +puts result.join(' ') is '5 6 7 8' # With range comprehensions, you can loop in steps. results: x for x in [0..25] by 5 -print results.join(' ') is '0 5 10 15 20 25' \ No newline at end of file +puts results.join(' ') is '0 5 10 15 20 25' \ No newline at end of file diff --git a/test/fixtures/execution/test_ranges_and_slices.coffee b/test/fixtures/execution/test_ranges_and_slices.coffee index a6a0bf18..af901563 100644 --- a/test/fixtures/execution/test_ranges_and_slices.coffee +++ b/test/fixtures/execution/test_ranges_and_slices.coffee @@ -5,7 +5,7 @@ b: array[2...4] result: a.concat(b).join(' ') -print result is "7 8 9 2 3" +puts result is "7 8 9 2 3" countdown: [10..1].join(' ') -print countdown is "10 9 8 7 6 5 4 3 2 1" \ No newline at end of file +puts countdown is "10 9 8 7 6 5 4 3 2 1" \ No newline at end of file diff --git a/test/fixtures/execution/test_splats.coffee b/test/fixtures/execution/test_splats.coffee index 18083d89..b50c2fb2 100644 --- a/test/fixtures/execution/test_splats.coffee +++ b/test/fixtures/execution/test_splats.coffee @@ -3,7 +3,7 @@ func: (first, second, rest...) -> result: func 1, 2, 3, 4, 5 -print result is "3 4 5" +puts result is "3 4 5" gold: silver: bronze: the_field: null @@ -29,7 +29,7 @@ contenders: [ medalists "Mighty Mouse", contenders... -print gold is "Mighty Mouse" -print silver is "Michael Phelps" -print bronze is "Liu Xiang" -print the_field.length is 8 \ No newline at end of file +puts gold is "Mighty Mouse" +puts silver is "Michael Phelps" +puts bronze is "Liu Xiang" +puts the_field.length is 8 \ No newline at end of file diff --git a/test/fixtures/execution/test_splices.coffee b/test/fixtures/execution/test_splices.coffee index e9baf877..56e6787d 100644 --- a/test/fixtures/execution/test_splices.coffee +++ b/test/fixtures/execution/test_splices.coffee @@ -2,4 +2,4 @@ array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] array[5..10]: [0, 0, 0] -print array.join(' ') is '0 1 2 3 4 0 0 0' \ No newline at end of file +puts array.join(' ') is '0 1 2 3 4 0 0 0' \ No newline at end of file diff --git a/test/fixtures/execution/test_switch.coffee b/test/fixtures/execution/test_switch.coffee index 3911f043..b97fa011 100644 --- a/test/fixtures/execution/test_switch.coffee +++ b/test/fixtures/execution/test_switch.coffee @@ -14,7 +14,7 @@ result: switch num when 11 then false else false -print result +puts result func: (num) -> switch num @@ -24,7 +24,7 @@ func: (num) -> false else false -print func(2) -print func(6) -print !func(3) -print !func(8) +puts func(2) +puts func(6) +puts !func(3) +puts !func(8) diff --git a/test/fixtures/execution/test_while.coffee b/test/fixtures/execution/test_while.coffee index c065fe04..e44a061f 100644 --- a/test/fixtures/execution/test_while.coffee +++ b/test/fixtures/execution/test_while.coffee @@ -1,17 +1,17 @@ i: 100 while i -= 1 -print i is 0 +puts i is 0 i: 5 list: while i -= 1 i * 2 -print list.join(' ') is "8 6 4 2" +puts list.join(' ') is "8 6 4 2" i: 5 list: (i * 3 while i -= 1) -print list.join(' ') is "12 9 6 3" \ No newline at end of file +puts list.join(' ') is "12 9 6 3" \ No newline at end of file diff --git a/test/fixtures/generation/statements_as_expressions.coffee b/test/fixtures/generation/statements_as_expressions.coffee index 56baa374..6f6f8616 100644 --- a/test/fixtures/generation/statements_as_expressions.coffee +++ b/test/fixtures/generation/statements_as_expressions.coffee @@ -3,14 +3,14 @@ result: while sunny? go_outside() -print(3 + try +puts(3 + try nonexistent.no_way catch error - print(error) + puts(error) 3 ) func: (x) -> return throw x -print(x * x for x in [1..100]) \ No newline at end of file +puts(x * x for x in [1..100]) \ No newline at end of file