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.



241
242
243
# File 'lib/syntax_tree/yarv/instructions.rb', line 241

def initialize(label)
  @label = label
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



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

def label
  @label
end

Instance Method Details

#call(vm) ⇒ Object



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

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

#canonicalObject



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

def canonical
  self
end

#disasm(fmt) ⇒ Object



245
246
247
# File 'lib/syntax_tree/yarv/instructions.rb', line 245

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

#lengthObject



253
254
255
# File 'lib/syntax_tree/yarv/instructions.rb', line 253

def length
  2
end

#popsObject



257
258
259
# File 'lib/syntax_tree/yarv/instructions.rb', line 257

def pops
  1
end

#pushesObject



261
262
263
# File 'lib/syntax_tree/yarv/instructions.rb', line 261

def pushes
  0
end

#to_a(_iseq) ⇒ Object



249
250
251
# File 'lib/syntax_tree/yarv/instructions.rb', line 249

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