Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e9b767c

Browse files
committed
Compile for loops
1 parent 1b2f19a commit e9b767c

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lib/syntax_tree/visitor/compiler.rb

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,11 @@ def newrange(flag)
545545
iseq.push([:newrange, flag])
546546
end
547547

548+
def nop
549+
stack.change_by(0)
550+
iseq.push([:nop])
551+
end
552+
548553
def objtostring(method_id, argc, flag)
549554
stack.change_by(-1 + 1)
550555
iseq.push([:objtostring, call_data(method_id, argc, flag)])
@@ -1190,13 +1195,9 @@ def visit_float(node)
11901195
def visit_for(node)
11911196
visit(node.collection)
11921197

1193-
# Be sure we set up the local table before we start compiling the body
1194-
# of the for loop.
1195-
if node.index.is_a?(VarField) && node.index.value.is_a?(Ident)
1196-
name = node.index.value.value.to_sym
1197-
unless current_iseq.local_variables.include?(name)
1198-
current_iseq.local_variables << name
1199-
end
1198+
name = node.index.value.value.to_sym
1199+
unless current_iseq.local_variables.include?(name)
1200+
current_iseq.local_variables << name
12001201
end
12011202

12021203
block_iseq =
@@ -1206,6 +1207,19 @@ def visit_for(node)
12061207
current_iseq,
12071208
node.statements
12081209
) do
1210+
current_iseq.argument_options[:lead_num] ||= 0
1211+
current_iseq.argument_options[:lead_num] += 1
1212+
current_iseq.argument_options[:ambiguous_param0] = true
1213+
1214+
current_iseq.argument_size += 1
1215+
current_iseq.local_variables << 2
1216+
1217+
builder.getlocal(0, 0)
1218+
1219+
local_variable = current_iseq.local_variable(name)
1220+
builder.setlocal(local_variable.index, local_variable.level)
1221+
builder.nop
1222+
12091223
visit(node.statements)
12101224
builder.leave
12111225
end

test/compiler_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ class CompilerTest < Minitest::Test
254254
"if foo then bar else baz end",
255255
"foo if bar",
256256
"foo while bar",
257+
"for i in [1, 2, 3] do i end",
257258
# Constructed values
258259
"foo..bar",
259260
"foo...bar",

0 commit comments

Comments
 (0)