Class: SyntaxTree::YARV::BranchIf

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

Overview

### Summary

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

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

### Usage

~~~ruby x = true x ||= “foo” puts x ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label) ⇒ BranchIf

Returns a new instance of BranchIf.



188
189
190
# File 'lib/syntax_tree/yarv/instructions.rb', line 188

def initialize(label)
  @label = label
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



186
187
188
# File 'lib/syntax_tree/yarv/instructions.rb', line 186

def label
  @label
end

Instance Method Details

#call(vm) ⇒ Object



216
217
218
# File 'lib/syntax_tree/yarv/instructions.rb', line 216

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

#canonicalObject



212
213
214
# File 'lib/syntax_tree/yarv/instructions.rb', line 212

def canonical
  self
end

#disasm(fmt) ⇒ Object



192
193
194
# File 'lib/syntax_tree/yarv/instructions.rb', line 192

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

#lengthObject



200
201
202
# File 'lib/syntax_tree/yarv/instructions.rb', line 200

def length
  2
end

#popsObject



204
205
206
# File 'lib/syntax_tree/yarv/instructions.rb', line 204

def pops
  1
end

#pushesObject



208
209
210
# File 'lib/syntax_tree/yarv/instructions.rb', line 208

def pushes
  0
end

#to_a(_iseq) ⇒ Object



196
197
198
# File 'lib/syntax_tree/yarv/instructions.rb', line 196

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