From 5fe419b1ce75b6b4b77539f815e709d1d06beb50 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 5 Jan 2010 09:10:45 -0500 Subject: [PATCH] automatic conversion of arguments into arrays --- lib/coffee_script/grammar.y | 2 ++ lib/coffee_script/lexer.rb | 1 + lib/coffee_script/nodes.rb | 7 ++++++- test/fixtures/execution/test_arguments.coffee | 7 +++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/coffee_script/grammar.y b/lib/coffee_script/grammar.y index 105ac110..5ba10e26 100644 --- a/lib/coffee_script/grammar.y +++ b/lib/coffee_script/grammar.y @@ -12,6 +12,7 @@ token FOR IN BY WHEN WHILE token SWITCH LEADING_WHEN token DELETE INSTANCEOF TYPEOF token SUPER EXTENDS +token ARGUMENTS token NEWLINE token COMMENT token JS @@ -97,6 +98,7 @@ rule | REGEX { result = LiteralNode.new(val[0]) } | BREAK { result = LiteralNode.new(val[0]) } | CONTINUE { result = LiteralNode.new(val[0]) } + | ARGUMENTS { result = LiteralNode.new(val[0]) } | TRUE { result = LiteralNode.new(true) } | FALSE { result = LiteralNode.new(false) } | YES { result = LiteralNode.new(true) } diff --git a/lib/coffee_script/lexer.rb b/lib/coffee_script/lexer.rb index f67cddbd..798156a6 100644 --- a/lib/coffee_script/lexer.rb +++ b/lib/coffee_script/lexer.rb @@ -15,6 +15,7 @@ module CoffeeScript "for", "in", "by", "where", "while", "switch", "when", "super", "extends", + "arguments", "delete", "instanceof", "typeof"] # Token matching regexes. diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 7ad9418f..ffc2998c 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -134,6 +134,10 @@ module CoffeeScript class LiteralNode < Node STATEMENTS = ['break', 'continue'] + CONVERSIONS = { + 'arguments' => 'Array.prototype.slice.call(arguments, 0)' + } + attr_reader :value def initialize(value) @@ -146,9 +150,10 @@ module CoffeeScript alias_method :statement_only?, :statement? def compile_node(o) + val = CONVERSIONS[@value.to_s] || @value.to_s indent = statement? ? o[:indent] : '' ending = statement? ? ';' : '' - write(indent + @value.to_s + ending) + write("#{indent}#{val}#{ending}") end end diff --git a/test/fixtures/execution/test_arguments.coffee b/test/fixtures/execution/test_arguments.coffee index 21fdbcac..d5c94909 100644 --- a/test/fixtures/execution/test_arguments.coffee +++ b/test/fixtures/execution/test_arguments.coffee @@ -15,3 +15,10 @@ print(area( x1 y1 ) is 100) + + +# Arguments are turned into arrays. +curried: => + print(area.apply(this, arguments.concat(20, 20)) is 100) + +curried(10, 10)