Class: SyntaxTree::YARV::OptCaseDispatch
- Inherits:
-
Object
- Object
- 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
- #call(vm) ⇒ Object
- #canonical ⇒ Object
- #disasm(fmt) ⇒ Object
-
#initialize(case_dispatch_hash, else_label) ⇒ OptCaseDispatch
constructor
A new instance of OptCaseDispatch.
- #length ⇒ Object
- #pops ⇒ Object
- #pushes ⇒ Object
- #to_a(_iseq) ⇒ Object
Constructor Details
#initialize(case_dispatch_hash, else_label) ⇒ OptCaseDispatch
2789 2790 2791 2792 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2789 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.
2787 2788 2789 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2787 def case_dispatch_hash @case_dispatch_hash end |
#else_label ⇒ Object (readonly)
Returns the value of attribute else_label.
2787 2788 2789 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2787 def else_label @else_label end |
Instance Method Details
#call(vm) ⇒ Object
2825 2826 2827 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2825 def call(vm) vm.jump(case_dispatch_hash.fetch(vm.pop, else_label)) end |
#canonical ⇒ Object
2821 2822 2823 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2821 def canonical self end |
#disasm(fmt) ⇒ Object
2794 2795 2796 2797 2798 2799 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2794 def disasm(fmt) fmt.instruction( "opt_case_dispatch", ["<cdhash>", fmt.label(else_label)] ) end |
#length ⇒ Object
2809 2810 2811 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2809 def length 3 end |
#pops ⇒ Object
2813 2814 2815 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2813 def pops 1 end |
#pushes ⇒ Object
2817 2818 2819 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2817 def pushes 0 end |
#to_a(_iseq) ⇒ Object
2801 2802 2803 2804 2805 2806 2807 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 2801 def to_a(_iseq) [ :opt_case_dispatch, case_dispatch_hash.flat_map { |key, value| [key, value.name] }, else_label.name ] end |