Class: SyntaxTree::YARV::BranchUnless

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/yarv/instructions.rb

Overview

### Summary

branchunless has one argument: the jump index. It pops one value off the stack: the jump condition.

If the value popped off the stack is false or nil, branchunless jumps to the jump index and continues executing there.

### Usage

~~~ruby if 2 + 3

puts "foo"

end ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label) ⇒ BranchUnless

Returns a new instance of BranchUnless.



325
326
327
# File 'lib/syntax_tree/yarv/instructions.rb', line 325

def initialize(label)
  @label = label
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



323
324
325
# File 'lib/syntax_tree/yarv/instructions.rb', line 323

def label
  @label
end

Instance Method Details

#==(other) ⇒ Object



341
342
343
# File 'lib/syntax_tree/yarv/instructions.rb', line 341

def ==(other)
  other.is_a?(BranchUnless) && other.label == label
end

#call(vm) ⇒ Object



361
362
363
# File 'lib/syntax_tree/yarv/instructions.rb', line 361

def call(vm)
  vm.jump(label) unless vm.pop
end

#canonicalObject



357
358
359
# File 'lib/syntax_tree/yarv/instructions.rb', line 357

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



337
338
339
# File 'lib/syntax_tree/yarv/instructions.rb', line 337

def deconstruct_keys(_keys)
  { label: label }
end

#disasm(fmt) ⇒ Object



329
330
331
# File 'lib/syntax_tree/yarv/instructions.rb', line 329

def disasm(fmt)
  fmt.instruction("branchunless", [fmt.label(label)])
end

#lengthObject



345
346
347
# File 'lib/syntax_tree/yarv/instructions.rb', line 345

def length
  2
end

#popsObject



349
350
351
# File 'lib/syntax_tree/yarv/instructions.rb', line 349

def pops
  1
end

#pushesObject



353
354
355
# File 'lib/syntax_tree/yarv/instructions.rb', line 353

def pushes
  0
end

#to_a(_iseq) ⇒ Object



333
334
335
# File 'lib/syntax_tree/yarv/instructions.rb', line 333

def to_a(_iseq)
  [:branchunless, label.name]
end