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

Included in:
CodeNode::IR::Graph
Defined in:
lib/code_node/ir/graph/builder_methods.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)



9
10
11
# File 'lib/code_node/ir/graph/builder_methods.rb', line 9

def scope
  @scope
end

Instance Method Details

#add_or_find_duplicate(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



67
68
69
70
# File 'lib/code_node/ir/graph/builder_methods.rb', line 67

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

#apply_stylesObject



11
12
13
14
15
16
17
18
19
# File 'lib/code_node/ir/graph/builder_methods.rb', line 11

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.

  • opt (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opt):

  • :not_sure_if_nested (Boolean) — default: false

Yield Parameters:

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/code_node/ir/graph/builder_methods.rb', line 44

def node_for(node_type, s, opt={}, &block)
  name = determine_name s
  return if name.nil?
    
  node = if opt[:not_sure_if_nested]
    search_for_node_in_parent(@scope.last, name) ||
    Node.new(name, :node_type => node_type)
  else
    Node.new name, :parent => @scope.last, :node_type => node_type
  end

  node = add_or_find_duplicate node
  unless block.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?



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/code_node/ir/graph/builder_methods.rb', line 22

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