Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
setspecial
  • Loading branch information
kddnewton committed Nov 22, 2022
commit f35c452221590d1f3dcea49e99d2992d674952e6
52 changes: 38 additions & 14 deletions lib/syntax_tree/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def visit_assoc_splat(node)
end

def visit_backref(node)
iseq.getspecial(1, 2 * node.value[1..].to_i)
iseq.getspecial(YARV::VM_SVAR_BACKREF, 2 * node.value[1..].to_i)
end

def visit_bare_assoc_hash(node)
Expand Down Expand Up @@ -888,25 +888,49 @@ def visit_heredoc(node)
end

def visit_if(node)
visit(node.predicate)
branchunless = iseq.branchunless(-1)
visit(node.statements)
if node.predicate.is_a?(RangeNode)
iseq.getspecial(YARV::VM_SVAR_FLIPFLOP_START, 0)
branchif = iseq.branchif(-1)

if last_statement?
iseq.leave
branchunless.patch!(iseq)
visit(node.predicate.left)
branchunless_true = iseq.branchunless(-1)

node.consequent ? visit(node.consequent) : iseq.putnil
iseq.putobject(true)
iseq.setspecial(YARV::VM_SVAR_FLIPFLOP_START)
branchif.patch!(iseq)

visit(node.predicate.right)
branchunless_false = iseq.branchunless(-1)

iseq.putobject(false)
iseq.setspecial(YARV::VM_SVAR_FLIPFLOP_START)
branchunless_false.patch!(iseq)

visit(node.statements)
iseq.leave
branchunless_true.patch!(iseq)
iseq.putnil
else
iseq.pop
visit(node.predicate)
branchunless = iseq.branchunless(-1)
visit(node.statements)

if node.consequent
jump = iseq.jump(-1)
if last_statement?
iseq.leave
branchunless.patch!(iseq)
visit(node.consequent)
jump[1] = iseq.label

node.consequent ? visit(node.consequent) : iseq.putnil
else
branchunless.patch!(iseq)
iseq.pop

if node.consequent
jump = iseq.jump(-1)
branchunless.patch!(iseq)
visit(node.consequent)
jump[1] = iseq.label
else
branchunless.patch!(iseq)
end
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions lib/syntax_tree/yarv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,11 @@ def setn(number)
push([:setn, number])
end

def setspecial(key)
stack.change_by(-1)
push([:setspecial, key])
end

def splatarray(flag)
stack.change_by(-1 + 1)
push([:splatarray, flag])
Expand Down Expand Up @@ -817,5 +822,10 @@ def call_data(method_id, argc, flag = VM_CALL_ARGS_SIMPLE)
VM_CALL_ZSUPER = 1 << 10
VM_CALL_OPT_SEND = 1 << 11
VM_CALL_KW_SPLAT_MUT = 1 << 12

# These constants correspond to the setspecial instruction.
VM_SVAR_LASTLINE = 0 # $_
VM_SVAR_BACKREF = 1 # $~
VM_SVAR_FLIPFLOP_START = 2 # flipflop
end
end
1 change: 1 addition & 0 deletions test/compiler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ class CompilerTest < Minitest::Test
"foo ? bar : baz",
"case foo when bar then 1 end",
"case foo when bar then 1 else 2 end",
"baz if (foo == 1) .. (bar == 1)",
# Constructed values
"foo..bar",
"foo...bar",
Expand Down