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
- #scope ⇒ Object readonly
Instance Method Summary collapse
-
#<<(node) ⇒ Node
Add the given node to the graph and return it.
- #apply_styles ⇒ Object
-
#node_for(node_type, s, opt = {}) {|node| ... } ⇒ Node
Find a node or create it and add it to the graph.
-
#prune ⇒ FixNum
Were any more nodes pruned?.
Instance Attribute Details
#scope ⇒ Object (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.
184 185 186 187 |
# File 'lib/code_node/ir/graph.rb', line 184 def <<(node) @nodes[node.path] ||= node @nodes[node.path] end |
#apply_styles ⇒ Object
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
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 |
#prune ⇒ FixNum
Returns 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 |