Class: SyntaxTree::YARV::OptCaseDispatch
- Inherits:
-
Instruction
- Object
- Instruction
- SyntaxTree::YARV::OptCaseDispatch
- 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
-
#case_dispatch_hash ⇒ Object
readonly
Returns the value of attribute case_dispatch_hash.
-
#else_label ⇒ Object
readonly
Returns the value of attribute else_label.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #branch_targets ⇒ Object
- #call(vm) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #disasm(fmt) ⇒ Object
- #falls_through? ⇒ Boolean
-
#initialize(case_dispatch_hash, else_label) ⇒ OptCaseDispatch
constructor
A new instance of OptCaseDispatch.
- #length ⇒ Object
- #pops ⇒ Object
- #to_a(_iseq) ⇒ Object
Methods inherited from Instruction
#canonical, #leaves?, #pushes, #side_effects?
Constructor Details
#initialize(case_dispatch_hash, else_label) ⇒ OptCaseDispatch
Returns a new instance of OptCaseDispatch.
3020 3021 3022 3023 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 3020 def initialize(case_dispatch_hash, else_label) @case_dispatch_hash = case_dispatch_hash @else_label = else_label end |
Instance Attribute Details
#case_dispatch_hash ⇒ Object (readonly)
Returns the value of attribute case_dispatch_hash.
3018 3019 3020 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 3018 def case_dispatch_hash @case_dispatch_hash end |
#else_label ⇒ Object (readonly)
Returns the value of attribute else_label.
3018 3019 3020 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 3018 def else_label @else_label end |
Instance Method Details
#==(other) ⇒ Object
3044 3045 3046 3047 3048 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 3044 def ==(other) other.is_a?(OptCaseDispatch) && other.case_dispatch_hash == case_dispatch_hash && other.else_label == else_label end |
#branch_targets ⇒ Object
3062 3063 3064 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 3062 def branch_targets case_dispatch_hash.values.push(else_label) end |
#call(vm) ⇒ Object
3058 3059 3060 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 3058 def call(vm) vm.jump(case_dispatch_hash.fetch(vm.pop, else_label)) end |
#deconstruct_keys(_keys) ⇒ Object
3040 3041 3042 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 3040 def deconstruct_keys(_keys) { case_dispatch_hash: case_dispatch_hash, else_label: else_label } end |
#disasm(fmt) ⇒ Object
3025 3026 3027 3028 3029 3030 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 3025 def disasm(fmt) fmt.instruction( "opt_case_dispatch", ["<cdhash>", fmt.label(else_label)] ) end |
#falls_through? ⇒ Boolean
3066 3067 3068 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 3066 def falls_through? true end |
#length ⇒ Object
3050 3051 3052 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 3050 def length 3 end |
#pops ⇒ Object
3054 3055 3056 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 3054 def pops 1 end |
#to_a(_iseq) ⇒ Object
3032 3033 3034 3035 3036 3037 3038 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 3032 def to_a(_iseq) [ :opt_case_dispatch, case_dispatch_hash.flat_map { |key, value| [key, value.name] }, else_label.name ] end |