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



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

def initialize(label)
  @label = label
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



202
203
204
# File 'lib/syntax_tree/yarv/instructions.rb', line 202

def label
  @label
end

Instance Method Details

#==(other) ⇒ Object



220
221
222
# File 'lib/syntax_tree/yarv/instructions.rb', line 220

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

#call(vm) ⇒ Object



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

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

#canonicalObject



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

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



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

def deconstruct_keys(_keys)
  { label: label }
end

#disasm(fmt) ⇒ Object



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

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

#lengthObject



224
225
226
# File 'lib/syntax_tree/yarv/instructions.rb', line 224

def length
  2
end

#popsObject



228
229
230
# File 'lib/syntax_tree/yarv/instructions.rb', line 228

def pops
  1
end

#pushesObject



232
233
234
# File 'lib/syntax_tree/yarv/instructions.rb', line 232

def pushes
  0
end

#to_a(_iseq) ⇒ Object



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

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