Class: SyntaxTree::YARV::NewHash

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

Overview

### Summary

newhash puts a new hash onto the stack, using number elements from the stack. number needs to be even. It pops number elements off the stack and pushes a hash onto the stack.

### Usage

~~~ruby def foo(key, value)

{ key => value }

end ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ NewHash

Returns a new instance of NewHash.



2584
2585
2586
# File 'lib/syntax_tree/yarv/instructions.rb', line 2584

def initialize(number)
  @number = number
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



2582
2583
2584
# File 'lib/syntax_tree/yarv/instructions.rb', line 2582

def number
  @number
end

Instance Method Details

#==(other) ⇒ Object



2600
2601
2602
# File 'lib/syntax_tree/yarv/instructions.rb', line 2600

def ==(other)
  other.is_a?(NewHash) && other.number == number
end

#call(vm) ⇒ Object



2620
2621
2622
# File 'lib/syntax_tree/yarv/instructions.rb', line 2620

def call(vm)
  vm.push(vm.pop(number).each_slice(2).to_h)
end

#canonicalObject



2616
2617
2618
# File 'lib/syntax_tree/yarv/instructions.rb', line 2616

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



2596
2597
2598
# File 'lib/syntax_tree/yarv/instructions.rb', line 2596

def deconstruct_keys(_keys)
  { number: number }
end

#disasm(fmt) ⇒ Object



2588
2589
2590
# File 'lib/syntax_tree/yarv/instructions.rb', line 2588

def disasm(fmt)
  fmt.instruction("newhash", [fmt.object(number)])
end

#lengthObject



2604
2605
2606
# File 'lib/syntax_tree/yarv/instructions.rb', line 2604

def length
  2
end

#popsObject



2608
2609
2610
# File 'lib/syntax_tree/yarv/instructions.rb', line 2608

def pops
  number
end

#pushesObject



2612
2613
2614
# File 'lib/syntax_tree/yarv/instructions.rb', line 2612

def pushes
  1
end

#to_a(_iseq) ⇒ Object



2592
2593
2594
# File 'lib/syntax_tree/yarv/instructions.rb', line 2592

def to_a(_iseq)
  [:newhash, number]
end