Module: CodeNode::IR::Graph::BuilderMethods

Included in:
CodeNode::IR::Graph
Defined in:
lib/code_node/ir/graph.rb

Overview

CodeNode::IR::Graph methods used during the graph building phase

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#scopeObject (readonly)



104
105
106
# File 'lib/code_node/ir/graph.rb', line 104

def scope
  @scope
end

Instance Method Details

#<<(node) ⇒ Node

Add the given node to the graph and return it. If a node with the same path is already in the graph, do not add it again, and return the original node.

Parameters:

  • node (Node)

    a node to add to the graph

Returns:

  • (Node)

    the newly added node, or another node with the same path which was already in the graph



184
185
186
187
# File 'lib/code_node/ir/graph.rb', line 184

def <<(node)
  @nodes[node.path] ||= node
  @nodes[node.path]
end

#apply_stylesObject



106
107
108
109
110
111
112
113
114
# File 'lib/code_node/ir/graph.rb', line 106

def apply_styles
  @nodes.each_value do |node|
    @style_matchers.each do |pair|
      if pair[0].matches? node
        node.style.update pair[1]
      end
    end
  end
end

#node_for(node_type, s, opt = {}) {|node| ... } ⇒ Node

Find a node or create it and add it to the graph

Parameters:

  • node_type (Symbol)

    either :module or :class

  • s (Symbol, Sexp)

    either flat name, or a Sexp representing a color (:) separated path.

Yield Parameters:

Returns:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/code_node/ir/graph.rb', line 138

def node_for(node_type, s, opt={}, &block)
  name = if s.is_a? Symbol
    s
  elsif s[0] == :const
    s[1]
  elsif s[0] == :colon2
    x = []
    while s[0] == :colon2
      x << s[2] ; s = s[1]
    end
    x << s[1]
    x.reverse
  elsif s[0] == :self
    @scope.last.mark_as_singleton
    nil
  end
  return if name.nil?
    
  node = if opt[:not_sure_if_nested]
    candidate = if name.is_a?(Array)
      parts = name.dup
      n = !@scope.empty? && @scope.last.find(parts.shift)
      while n && !parts.empty?
        n = n.find parts.shift
      end
      n
    else
      !@scope.empty? && @scope.last.find(name)
    end
    candidate || Node.new(name, :node_type => node_type)
  else
    Node.new name, :parent => @scope.last, :node_type => node_type
  end

  node = self << node
  unless block.nil? || node.nil?
    @scope << node
    block.call node
    @scope.pop
  end
  node
end

#pruneFixNum

Returns were any more nodes pruned?.

Returns:

  • (FixNum)

    were any more nodes pruned?



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/code_node/ir/graph.rb', line 117

def prune
  prunees = []
  @nodes.each_value do |node|
    if @exclude_matchers.any? {|m| m.matches? node}
      prunees << node
    end
  end
  prunees.each do |node|
    puts "  #{node.path}"
    node.prune
    @nodes.delete node.path
  end
  prunees.length
end