Permalink
Browse files

Fix for-in loops.

  • Loading branch information...
1 parent a7d5133 commit d5bcb969027f838fd7464c37ee3da1890afa80f4 @whitequark committed Aug 19, 2012
Showing with 26 additions and 7 deletions.
  1. +26 −7 lib/furnace-avm2/transform/nf_normalize.rb
@@ -44,20 +44,22 @@ def on_r(node)
# Loops can get expanded, but conditionals would never contain
# has-next2.
- def on_if(node)
+ def do_if(node, parent)
if ExpandedForInMatcher.match node
condition, body, rest = node.children
body.children << AST::Node.new(:break)
loop = AST::Node.new(:while, [ condition, body ])
- on_while(loop, node.parent, node)
+ do_while(loop, parent, node)
if rest
- node.update(:expand, [ loop ] + rest.children)
+ [ loop ] + rest.children
else
- node.update(:expand, [ loop ])
+ [ loop ]
end
+ else
+ node
end
end
@@ -86,8 +88,7 @@ def on_if(node)
capture(:root)]]
end
-=begin
- def on_while(node, parent=node.parent, enclosure=node)
+ def do_while(node, parent, enclosure=node)
*whatever, code = node.children
if captures = ForInMatcher.match(node)
@@ -125,9 +126,27 @@ def on_while(node, parent=node.parent, enclosure=node)
AST::Node.new(:begin, captures[:body])
])
end
+
+ node
end
-=end
+
def on_begin(node)
+ # Fix for-in loops.
+ node.children.map! do |child|
+ if child.type == :if
+ do_if(child, node)
+ elsif child.type == :while
+ do_while(child, node)
+ else
+ child
+ end
+ end
+ node.children.flatten!
+
+ node.children.reject! do |child|
+ child.type == :remove
+ end
+
# Fold (with)'s
with_begin = node.children.index do |child|
child.type == :push_with

0 comments on commit d5bcb96

Please sign in to comment.