unified ParamSplatNode and ArgSplatNode into SplatNode

This commit is contained in:
Jeremy Ashkenas
2010-01-12 23:49:47 -05:00
parent ea349a1a59
commit abd9ab5c71
2 changed files with 15 additions and 18 deletions

View File

@@ -211,12 +211,12 @@ rule
# A Parameter (or ParamSplat) in a function definition. # A Parameter (or ParamSplat) in a function definition.
Param: Param:
PARAM PARAM
| PARAM "." "." "." { result = ParamSplatNode.new(val[0]) } | PARAM "." "." "." { result = SplatNode.new(val[0]) }
; ;
# A regular splat. # A regular splat.
Splat: Splat:
Expression "." "." "." { result = ArgSplatNode.new(val[0])} Expression "." "." "." { result = SplatNode.new(val[0]) }
; ;
# Expressions that can be treated as values. # Expressions that can be treated as values.

View File

@@ -244,7 +244,7 @@ module CoffeeScript
end end
def splat? def splat?
@arguments.any? {|a| a.is_a?(ArgSplatNode) } @arguments.any? {|a| a.is_a?(SplatNode) }
end end
def <<(argument) def <<(argument)
@@ -274,7 +274,7 @@ module CoffeeScript
obj = @variable.source || 'this' obj = @variable.source || 'this'
args = @arguments.map do |arg| args = @arguments.map do |arg|
code = arg.compile(o) code = arg.compile(o)
code = arg.is_a?(ArgSplatNode) ? code : "[#{code}]" code = arg.is_a?(SplatNode) ? code : "[#{code}]"
arg.equal?(@arguments.first) ? code : ".concat(#{code})" arg.equal?(@arguments.first) ? code : ".concat(#{code})"
end end
"#{prefix}#{meth}.apply(#{obj}, #{args.join('')})" "#{prefix}#{meth}.apply(#{obj}, #{args.join('')})"
@@ -562,7 +562,7 @@ module CoffeeScript
o.delete(:no_wrap) o.delete(:no_wrap)
o.delete(:globals) o.delete(:globals)
name = o.delete(:immediate_assign) name = o.delete(:immediate_assign)
if @params.last.is_a?(ParamSplatNode) if @params.last.is_a?(SplatNode)
splat = @params.pop splat = @params.pop
splat.index = @params.length splat.index = @params.length
@body.unshift(splat) @body.unshift(splat)
@@ -574,8 +574,9 @@ module CoffeeScript
end end
end end
# A parameter splat in a function definition. # A splat, either as a parameter to a function, an argument to a call,
class ParamSplatNode < Node # or in a destructuring assignment.
class SplatNode < Node
attr_accessor :index attr_accessor :index
attr_reader :name attr_reader :name
@@ -584,20 +585,16 @@ module CoffeeScript
end end
def compile_node(o={}) def compile_node(o={})
write(@index ? compile_param(o) : compile_arg(o))
end
def compile_param(o)
o[:scope].find(@name) o[:scope].find(@name)
write("#{@name} = Array.prototype.slice.call(arguments, #{@index})") "#{@name} = Array.prototype.slice.call(arguments, #{@index})"
end
end
class ArgSplatNode < Node
attr_reader :value
def initialize(value)
@value = value
end end
def compile_node(o={}) def compile_arg(o)
write(@value.compile(o)) @name.compile(o)
end end
end end