Class: SyntaxTree::YARV::NewHash

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

Methods inherited from Instruction

#branch_targets, #canonical, #falls_through?, #leaves?, #side_effects?

Constructor Details

#initialize(number) ⇒ NewHash

Returns a new instance of NewHash.



2473
2474
2475
# File 'lib/syntax_tree/yarv/instructions.rb', line 2473

def initialize(number)
  @number = number
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



2471
2472
2473
# File 'lib/syntax_tree/yarv/instructions.rb', line 2471

def number
  @number
end

Instance Method Details

#==(other) ⇒ Object



2489
2490
2491
# File 'lib/syntax_tree/yarv/instructions.rb', line 2489

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

#call(vm) ⇒ Object



2505
2506
2507
# File 'lib/syntax_tree/yarv/instructions.rb', line 2505

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

#deconstruct_keys(_keys) ⇒ Object



2485
2486
2487
# File 'lib/syntax_tree/yarv/instructions.rb', line 2485

def deconstruct_keys(_keys)
  { number: number }
end

#disasm(fmt) ⇒ Object



2477
2478
2479
# File 'lib/syntax_tree/yarv/instructions.rb', line 2477

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

#lengthObject



2493
2494
2495
# File 'lib/syntax_tree/yarv/instructions.rb', line 2493

def length
  2
end

#popsObject



2497
2498
2499
# File 'lib/syntax_tree/yarv/instructions.rb', line 2497

def pops
  number
end

#pushesObject



2501
2502
2503
# File 'lib/syntax_tree/yarv/instructions.rb', line 2501

def pushes
  1
end

#to_a(_iseq) ⇒ Object



2481
2482
2483
# File 'lib/syntax_tree/yarv/instructions.rb', line 2481

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