Class: SyntaxTree::YARV::OptCaseDispatch

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

Methods inherited from Instruction

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

Constructor Details

#initialize(case_dispatch_hash, else_label) ⇒ OptCaseDispatch

Returns a new instance of OptCaseDispatch.



2967
2968
2969
2970
# File 'lib/syntax_tree/yarv/instructions.rb', line 2967

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.



2965
2966
2967
# File 'lib/syntax_tree/yarv/instructions.rb', line 2965

def case_dispatch_hash
  @case_dispatch_hash
end

#else_labelObject (readonly)

Returns the value of attribute else_label.



2965
2966
2967
# File 'lib/syntax_tree/yarv/instructions.rb', line 2965

def else_label
  @else_label
end

Instance Method Details

#==(other) ⇒ Object



2991
2992
2993
2994
2995
# File 'lib/syntax_tree/yarv/instructions.rb', line 2991

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

#branch_targetsObject



3009
3010
3011
# File 'lib/syntax_tree/yarv/instructions.rb', line 3009

def branch_targets
  case_dispatch_hash.values.push(else_label)
end

#call(vm) ⇒ Object



3005
3006
3007
# File 'lib/syntax_tree/yarv/instructions.rb', line 3005

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

#deconstruct_keys(_keys) ⇒ Object



2987
2988
2989
# File 'lib/syntax_tree/yarv/instructions.rb', line 2987

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

#disasm(fmt) ⇒ Object



2972
2973
2974
2975
2976
2977
# File 'lib/syntax_tree/yarv/instructions.rb', line 2972

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

#falls_through?Boolean

Returns:

  • (Boolean)


3013
3014
3015
# File 'lib/syntax_tree/yarv/instructions.rb', line 3013

def falls_through?
  true
end

#lengthObject



2997
2998
2999
# File 'lib/syntax_tree/yarv/instructions.rb', line 2997

def length
  3
end

#popsObject



3001
3002
3003
# File 'lib/syntax_tree/yarv/instructions.rb', line 3001

def pops
  1
end

#to_a(_iseq) ⇒ Object



2979
2980
2981
2982
2983
2984
2985
# File 'lib/syntax_tree/yarv/instructions.rb', line 2979

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