Sha256: 533ab32a16e643b204bbf6156c9b1f2447f8177ba72fe69ec794671f702b6dd6

Contents?: true

Size: 1.22 KB

Versions: 6

Compression:

Stored size: 1.22 KB

Contents

module Furnace::CFG
  class Graph
    attr_reader :nodes, :edges

    def initialize
      @nodes = Set.new
      @edges = Set.new

      @pending_label      = nil
      @pending_operations = []
    end

    def find_node(label)
      if node = @nodes.find { |n| n.label == label }
        node
      else
        raise "Cannot find CFG node #{label}"
      end
    end

    def expand(label, operation)
      @pending_label ||= label
      @pending_operations << operation
    end

    def transfer(targets)
      return unless @pending_label

      @nodes << Node.new(self, @pending_label, @pending_operations)

      targets.each do |operation, target|
        @edges << Edge.new(self, operation, @pending_label, target)
      end

      @pending_label      = nil
      @pending_operations = []
    end

    def to_graphviz
      Furnace::Graphviz.new do |graph|
        @nodes.each do |node|
          graph.node node.label, node.operations.map(&:inspect).join("\n")
        end

        @edges.each do |edge|
          if edge.source_operation.nil?
            label = "~"
          else
            label = edge.source_operation
          end

          graph.edge edge.source_label, edge.target_label, label
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
furnace-0.0.8 lib/furnace/cfg/graph.rb
furnace-0.0.7 lib/furnace/cfg/graph.rb
furnace-0.0.6 lib/furnace/cfg/graph.rb
furnace-0.0.4 lib/furnace/cfg/graph.rb
furnace-0.0.3 lib/furnace/cfg/graph.rb
furnace-0.0.2 lib/furnace/cfg/graph.rb