Class: SyntaxTree::YARV::BranchNil

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

Overview

### Summary

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

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

### Usage

~~~ruby x = nil if x&.to_s

puts "hi"

end ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label) ⇒ BranchNil

Returns a new instance of BranchNil.



265
266
267
# File 'lib/syntax_tree/yarv/instructions.rb', line 265

def initialize(label)
  @label = label
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



263
264
265
# File 'lib/syntax_tree/yarv/instructions.rb', line 263

def label
  @label
end

Instance Method Details

#==(other) ⇒ Object



281
282
283
# File 'lib/syntax_tree/yarv/instructions.rb', line 281

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

#call(vm) ⇒ Object



301
302
303
# File 'lib/syntax_tree/yarv/instructions.rb', line 301

def call(vm)
  vm.jump(label) if vm.pop.nil?
end

#canonicalObject



297
298
299
# File 'lib/syntax_tree/yarv/instructions.rb', line 297

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



277
278
279
# File 'lib/syntax_tree/yarv/instructions.rb', line 277

def deconstruct_keys(_keys)
  { label: label }
end

#disasm(fmt) ⇒ Object



269
270
271
# File 'lib/syntax_tree/yarv/instructions.rb', line 269

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

#lengthObject



285
286
287
# File 'lib/syntax_tree/yarv/instructions.rb', line 285

def length
  2
end

#popsObject



289
290
291
# File 'lib/syntax_tree/yarv/instructions.rb', line 289

def pops
  1
end

#pushesObject



293
294
295
# File 'lib/syntax_tree/yarv/instructions.rb', line 293

def pushes
  0
end

#to_a(_iseq) ⇒ Object



273
274
275
# File 'lib/syntax_tree/yarv/instructions.rb', line 273

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