Class: SyntaxTree::YARV::OptCaseDispatch

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

Overview

### Summary

opt_case_dispatch is a branch instruction that moves the control flow for case statements that have clauses where they can all be used as hash keys for an internal hash.

It has two arguments: the case_dispatch_hash and an else_label. It pops one value off the stack: a hash key. opt_case_dispatch looks up the key in the case_dispatch_hash and jumps to the corresponding label if there is one. If there is no value in the case_dispatch_hash, opt_case_dispatch jumps to the else_label index.

### Usage

~~~ruby case 1 when 1

puts "foo"

else

puts "bar"

end ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(case_dispatch_hash, else_label) ⇒ OptCaseDispatch



3171
3172
3173
3174
# File 'lib/syntax_tree/yarv/instructions.rb', line 3171

def initialize(case_dispatch_hash, else_label)
  @case_dispatch_hash = case_dispatch_hash
  @else_label = else_label
end

Instance Attribute Details

#case_dispatch_hashObject (readonly)

Returns the value of attribute case_dispatch_hash.



3169
3170
3171
# File 'lib/syntax_tree/yarv/instructions.rb', line 3169

def case_dispatch_hash
  @case_dispatch_hash
end

#else_labelObject (readonly)

Returns the value of attribute else_label.



3169
3170
3171
# File 'lib/syntax_tree/yarv/instructions.rb', line 3169

def else_label
  @else_label
end

Instance Method Details

#==(other) ⇒ Object



3195
3196
3197
3198
3199
# File 'lib/syntax_tree/yarv/instructions.rb', line 3195

def ==(other)
  other.is_a?(OptCaseDispatch) &&
    other.case_dispatch_hash == case_dispatch_hash &&
    other.else_label == else_label
end

#call(vm) ⇒ Object



3217
3218
3219
# File 'lib/syntax_tree/yarv/instructions.rb', line 3217

def call(vm)
  vm.jump(case_dispatch_hash.fetch(vm.pop, else_label))
end

#canonicalObject



3213
3214
3215
# File 'lib/syntax_tree/yarv/instructions.rb', line 3213

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



3191
3192
3193
# File 'lib/syntax_tree/yarv/instructions.rb', line 3191

def deconstruct_keys(_keys)
  { case_dispatch_hash: case_dispatch_hash, else_label: else_label }
end

#disasm(fmt) ⇒ Object



3176
3177
3178
3179
3180
3181
# File 'lib/syntax_tree/yarv/instructions.rb', line 3176

def disasm(fmt)
  fmt.instruction(
    "opt_case_dispatch",
    ["<cdhash>", fmt.label(else_label)]
  )
end

#lengthObject



3201
3202
3203
# File 'lib/syntax_tree/yarv/instructions.rb', line 3201

def length
  3
end

#popsObject



3205
3206
3207
# File 'lib/syntax_tree/yarv/instructions.rb', line 3205

def pops
  1
end

#pushesObject



3209
3210
3211
# File 'lib/syntax_tree/yarv/instructions.rb', line 3209

def pushes
  0
end

#to_a(_iseq) ⇒ Object



3183
3184
3185
3186
3187
3188
3189
# File 'lib/syntax_tree/yarv/instructions.rb', line 3183

def to_a(_iseq)
  [
    :opt_case_dispatch,
    case_dispatch_hash.flat_map { |key, value| [key, value.name] },
    else_label.name
  ]
end