Skip to content

Commit

Permalink
unified ParamSplatNode and ArgSplatNode into SplatNode
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Jan 13, 2010
1 parent ea349a1 commit abd9ab5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/coffee_script/grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ rule
# A Parameter (or ParamSplat) in a function definition.
Param:
PARAM
| PARAM "." "." "." { result = ParamSplatNode.new(val[0]) }
| PARAM "." "." "." { result = SplatNode.new(val[0]) }
;
# A regular splat.
Splat:
Expression "." "." "." { result = ArgSplatNode.new(val[0])}
Expression "." "." "." { result = SplatNode.new(val[0]) }
;
# Expressions that can be treated as values.
Expand Down
27 changes: 12 additions & 15 deletions lib/coffee_script/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def prefix
end

def splat?
@arguments.any? {|a| a.is_a?(ArgSplatNode) }
@arguments.any? {|a| a.is_a?(SplatNode) }
end

def <<(argument)
Expand Down Expand Up @@ -274,7 +274,7 @@ def compile_splat(o)
obj = @variable.source || 'this'
args = @arguments.map do |arg|
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})"
end
"#{prefix}#{meth}.apply(#{obj}, #{args.join('')})"
Expand Down Expand Up @@ -562,7 +562,7 @@ def compile_node(o)
o.delete(:no_wrap)
o.delete(:globals)
name = o.delete(:immediate_assign)
if @params.last.is_a?(ParamSplatNode)
if @params.last.is_a?(SplatNode)
splat = @params.pop
splat.index = @params.length
@body.unshift(splat)
Expand All @@ -574,8 +574,9 @@ def compile_node(o)
end
end

# A parameter splat in a function definition.
class ParamSplatNode < Node
# A splat, either as a parameter to a function, an argument to a call,
# or in a destructuring assignment.
class SplatNode < Node
attr_accessor :index
attr_reader :name

Expand All @@ -584,20 +585,16 @@ def initialize(name)
end

def compile_node(o={})
o[:scope].find(@name)
write("#{@name} = Array.prototype.slice.call(arguments, #{@index})")
write(@index ? compile_param(o) : compile_arg(o))
end
end

class ArgSplatNode < Node
attr_reader :value

def initialize(value)
@value = value
def compile_param(o)
o[:scope].find(@name)
"#{@name} = Array.prototype.slice.call(arguments, #{@index})"
end

def compile_node(o={})
write(@value.compile(o))
def compile_arg(o)
@name.compile(o)
end

end
Expand Down

0 comments on commit abd9ab5

Please sign in to comment.