Class: SyntaxTree::YARV::BranchNil

Inherits:
Instruction 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

Methods inherited from Instruction

#canonical, #leaves?, #pushes, #side_effects?

Constructor Details

#initialize(label) ⇒ BranchNil

Returns a new instance of BranchNil.



239
240
241
# File 'lib/syntax_tree/yarv/instructions.rb', line 239

def initialize(label)
  @label = label
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



237
238
239
# File 'lib/syntax_tree/yarv/instructions.rb', line 237

def label
  @label
end

Instance Method Details

#==(other) ⇒ Object



255
256
257
# File 'lib/syntax_tree/yarv/instructions.rb', line 255

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

#branch_targetsObject



271
272
273
# File 'lib/syntax_tree/yarv/instructions.rb', line 271

def branch_targets
  [label]
end

#call(vm) ⇒ Object



267
268
269
# File 'lib/syntax_tree/yarv/instructions.rb', line 267

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

#deconstruct_keys(_keys) ⇒ Object



251
252
253
# File 'lib/syntax_tree/yarv/instructions.rb', line 251

def deconstruct_keys(_keys)
  { label: label }
end

#disasm(fmt) ⇒ Object



243
244
245
# File 'lib/syntax_tree/yarv/instructions.rb', line 243

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

#falls_through?Boolean

Returns:

  • (Boolean)


275
276
277
# File 'lib/syntax_tree/yarv/instructions.rb', line 275

def falls_through?
  true
end

#lengthObject



259
260
261
# File 'lib/syntax_tree/yarv/instructions.rb', line 259

def length
  2
end

#popsObject



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

def pops
  1
end

#to_a(_iseq) ⇒ Object



247
248
249
# File 'lib/syntax_tree/yarv/instructions.rb', line 247

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