Class: SyntaxTree::YARV::BranchIf

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

Methods inherited from Instruction

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

Constructor Details

#initialize(label) ⇒ BranchIf

Returns a new instance of BranchIf.



178
179
180
# File 'lib/syntax_tree/yarv/instructions.rb', line 178

def initialize(label)
  @label = label
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



176
177
178
# File 'lib/syntax_tree/yarv/instructions.rb', line 176

def label
  @label
end

Instance Method Details

#==(other) ⇒ Object



194
195
196
# File 'lib/syntax_tree/yarv/instructions.rb', line 194

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

#branch_targetsObject



210
211
212
# File 'lib/syntax_tree/yarv/instructions.rb', line 210

def branch_targets
  [label]
end

#call(vm) ⇒ Object



206
207
208
# File 'lib/syntax_tree/yarv/instructions.rb', line 206

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

#deconstruct_keys(_keys) ⇒ Object



190
191
192
# File 'lib/syntax_tree/yarv/instructions.rb', line 190

def deconstruct_keys(_keys)
  { label: label }
end

#disasm(fmt) ⇒ Object



182
183
184
# File 'lib/syntax_tree/yarv/instructions.rb', line 182

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

#falls_through?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/syntax_tree/yarv/instructions.rb', line 214

def falls_through?
  true
end

#lengthObject



198
199
200
# File 'lib/syntax_tree/yarv/instructions.rb', line 198

def length
  2
end

#popsObject



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

def pops
  1
end

#to_a(_iseq) ⇒ Object



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

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