Skip to content

Commit

Permalink
Added spec for while loops.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Edgar committed Apr 2, 2010
1 parent d5a25b6 commit 84fe898
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
27 changes: 14 additions & 13 deletions lib/reversal/reverser.rb
Expand Up @@ -123,22 +123,23 @@ def backward_jump?(current, label)
end

def next_instruction_number(cur_inst, cur_line)
if cur_inst.is_a?(Array) && (cur_inst[0] == :branchif || cur_inst[0] == :branchunless)
target = cur_inst[1]
location_of_target = @iseq.labels[target]
else_instruction = @iseq.body[location_of_target - 1]
if else_instruction.first == :jump
else_target = else_instruction[1]
location_of_else_target = @iseq.labels[else_target]
return location_of_else_target
else
if cur_inst.is_a?(Array)
if (cur_inst[0] == :branchif || cur_inst[0] == :branchunless) && forward_jump?(cur_line,cur_inst[1])
target = cur_inst[1]
location_of_target = @iseq.labels[target]
else_instruction = @iseq.body[location_of_target - 1]
if else_instruction.first == :jump
else_target = else_instruction[1]
location_of_else_target = @iseq.labels[else_target]
return location_of_else_target
else
return @iseq.body.size + 1
end
elsif cur_inst[0] == :leave
return @iseq.body.size + 1
end
elsif cur_inst.is_a?(Array) && (cur_inst[0] == :leave)
return @iseq.body.size + 1
else
return cur_line + 1
end
return cur_line + 1
end

def decompile_body(instruction = @iseq.body_start, stop = @iseq.body.size)
Expand Down
14 changes: 14 additions & 0 deletions spec/control_flow_spec.rb
Expand Up @@ -112,6 +112,16 @@ def test(x)
puts(x)
end
end
RESULT

@simple_while = CompiledDecompilationTestCase.new <<CLASS, <<RESULT
begin
x
end while y
CLASS
begin
x
end while y
RESULT
end

Expand Down Expand Up @@ -142,4 +152,8 @@ def test(x)
it "can decompile a condition using the || operator" do
@uses_oror.assert_correct
end

it "can decompile simple do..while loops" do
@simple_while.assert_correct
end
end

0 comments on commit 84fe898

Please sign in to comment.