Class: SyntaxTree::YARV::NewRange

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

Overview

### Summary

‘newrange` creates a new range object from the top two values on the stack. It pops both of them off, and then pushes on the new range. It takes one argument which is 0 if the end is included or 1 if the end value is excluded.

### Usage

~~~ruby x = 0 y = 1 p (x..y), (x…y) ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

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

Constructor Details

#initialize(exclude_end) ⇒ NewRange

Returns a new instance of NewRange.



2528
2529
2530
# File 'lib/syntax_tree/yarv/instructions.rb', line 2528

def initialize(exclude_end)
  @exclude_end = exclude_end
end

Instance Attribute Details

#exclude_endObject (readonly)

Returns the value of attribute exclude_end.



2526
2527
2528
# File 'lib/syntax_tree/yarv/instructions.rb', line 2526

def exclude_end
  @exclude_end
end

Instance Method Details

#==(other) ⇒ Object



2544
2545
2546
# File 'lib/syntax_tree/yarv/instructions.rb', line 2544

def ==(other)
  other.is_a?(NewRange) && other.exclude_end == exclude_end
end

#call(vm) ⇒ Object



2560
2561
2562
# File 'lib/syntax_tree/yarv/instructions.rb', line 2560

def call(vm)
  vm.push(Range.new(*vm.pop(2), exclude_end == 1))
end

#deconstruct_keys(_keys) ⇒ Object



2540
2541
2542
# File 'lib/syntax_tree/yarv/instructions.rb', line 2540

def deconstruct_keys(_keys)
  { exclude_end: exclude_end }
end

#disasm(fmt) ⇒ Object



2532
2533
2534
# File 'lib/syntax_tree/yarv/instructions.rb', line 2532

def disasm(fmt)
  fmt.instruction("newrange", [fmt.object(exclude_end)])
end

#lengthObject



2548
2549
2550
# File 'lib/syntax_tree/yarv/instructions.rb', line 2548

def length
  2
end

#popsObject



2552
2553
2554
# File 'lib/syntax_tree/yarv/instructions.rb', line 2552

def pops
  2
end

#pushesObject



2556
2557
2558
# File 'lib/syntax_tree/yarv/instructions.rb', line 2556

def pushes
  1
end

#to_a(_iseq) ⇒ Object



2536
2537
2538
# File 'lib/syntax_tree/yarv/instructions.rb', line 2536

def to_a(_iseq)
  [:newrange, exclude_end]
end