Skip to content

Commit

Permalink
pulling out pushes into a pushnode
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Jan 14, 2010
1 parent bb9fdd3 commit 2d206e7
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/coffee_script/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,17 @@ def compile_node(o)
end
end

# A faux-node that is never created by the grammar, but is used during
# code generation to generate a quick "array.push(value)" tree of nodes.
class PushNode
def self.wrap(array, expressions)
Expressions.wrap(CallNode.new(
ValueNode.new(LiteralNode.new(array), [AccessorNode.new('push')]),
[expressions.unwrap]
))
end
end

# A while loop, the only sort of low-level loop exposed by CoffeeScript. From
# it, all other loops can be manufactured.
class WhileNode < Node
Expand All @@ -683,9 +694,7 @@ def compile_node(o)
if !top
rvar = o[:scope].free_variable
set = "#{idt}#{rvar} = [];\n"
@body = Expressions.wrap(CallNode.new(
ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [@body.unwrap]
))
@body = PushNode.wrap(rvar, @body)
end
post = returns ? "\n#{idt}return #{rvar};" : ''
return write("#{set}#{idt}while (#{cond}) null;#{post}") if @body.nil?
Expand Down Expand Up @@ -743,9 +752,7 @@ def compile_node(o)
if top_level
body = Expressions.wrap(body)
else
body = Expressions.wrap(CallNode.new(
ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [body.unwrap]
))
body = PushNode.wrap(rvar, body)
end
if o[:return]
return_result = "return #{return_result}" if o[:return]
Expand Down

0 comments on commit 2d206e7

Please sign in to comment.