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.



48
49
50
51
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 48

def initialize
  @head_node = nil
  @tail_node = nil
end

Instance Attribute Details

#head_nodeObject (readonly)

Returns the value of attribute head_node.



46
47
48
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 46

def head_node
  @head_node
end

#tail_nodeObject (readonly)

Returns the value of attribute tail_node.



46
47
48
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 46

def tail_node
  @tail_node
end

Instance Method Details

#each(&_blk) ⇒ Object



53
54
55
56
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 53

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

#each_nodeObject



58
59
60
61
62
63
64
65
66
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 58

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



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 68

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