Class: SyntaxTree::YARV::InstructionSequence::InstructionList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/syntax_tree/yarv/instruction_sequence.rb

Overview

When the list of instructions is first being created, it’s stored as a linked list. This is to make it easier to perform peephole optimizations and other transformations like instruction specialization.

Defined Under Namespace

Classes: Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstructionList

Returns a new instance of InstructionList.



26
27
28
29
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 26

def initialize
  @head_node = nil
  @tail_node = nil
end

Instance Attribute Details

#head_nodeObject (readonly)

Returns the value of attribute head_node.



24
25
26
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 24

def head_node
  @head_node
end

#tail_nodeObject (readonly)

Returns the value of attribute tail_node.



24
25
26
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 24

def tail_node
  @tail_node
end

Instance Method Details

#eachObject



31
32
33
34
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 31

def each
  return to_enum(__method__) unless block_given?
  each_node { |node| yield node.value }
end

#each_nodeObject



36
37
38
39
40
41
42
43
44
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 36

def each_node
  return to_enum(__method__) unless block_given?
  node = head_node

  while node
    yield node, node.value
    node = node.next_node
  end
end

#push(instruction) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 46

def push(instruction)
  node = Node.new(instruction)

  if head_node.nil?
    @head_node = node
    @tail_node = node
  else
    @tail_node.next_node = node
    @tail_node = node
  end

  node
end