Class: CodeTools::AST::AsciiGrapher

Inherits:
Object
  • Object
show all
Defined in:
lib/rubinius/code/ast/grapher.rb

Instance Method Summary collapse

Constructor Details

#initialize(ast, node_kind = Node) ⇒ AsciiGrapher

Returns a new instance of AsciiGrapher.



6
7
8
9
# File 'lib/rubinius/code/ast/grapher.rb', line 6

def initialize(ast, node_kind=Node)
  @ast = ast
  @node_kind = node_kind
end

Instance Method Details

#graph_node(node, level = 0, idx = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubinius/code/ast/grapher.rb', line 27

def graph_node(node, level=0, idx=nil)
  print_node node, level, idx
  level += 2

  nodes = []
  node.instance_variables.each do |v|
    next if v == "@compiler"

    value = node.instance_variable_get v

    # lame, yes. remove when Node doesn't have @body by default
    next if v == "@body" and value.nil? and not v.respond_to? :body=

    if value.kind_of? @node_kind
      nodes << [v, value]
    else
      graph_value v, value, level
    end
  end

  nodes.each do |name, n|
    puts "#{" " * level}#{name}: \\"
    graph_node n, level + 2
  end
end

#graph_simple(name, value, level) ⇒ Object



53
54
55
# File 'lib/rubinius/code/ast/grapher.rb', line 53

def graph_simple(name, value, level)
  puts "#{" " * level}#{name}: #{value}"
end

#graph_value(name, value, level) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rubinius/code/ast/grapher.rb', line 57

def graph_value(name, value, level)
  case value
  when NilClass, String
    graph_simple name, value.inspect, level
  when Symbol
    puts "#{" " * level}#{name}: :#{value}"
  when TrueClass, FalseClass, Fixnum
    graph_simple name, value, level
  when Array
    if value.empty?
      puts "#{" " * level}#{name}: []"
    else
      puts "#{" " * level}#{name}: ["
      nodes = []
      value.each_with_index do |v,i|
        if v.kind_of? @node_kind
          nodes << [v, i]
        else
          graph_value "[#{i}] ", v, level + 2
        end
      end

      nodes.each { |n| graph_node n[0], level + 2, n[1] }

      puts "#{' ' * level}]"
    end
  else
    graph_simple name, value.class, level
  end
end

#indented_print(level, value) ⇒ Object



15
16
17
# File 'lib/rubinius/code/ast/grapher.rb', line 15

def indented_print(level, value)
  puts "#{" " * level}#{value}"
end


11
12
13
# File 'lib/rubinius/code/ast/grapher.rb', line 11

def print
  graph_node @ast
end


19
20
21
22
23
24
25
# File 'lib/rubinius/code/ast/grapher.rb', line 19

def print_node(node, level, idx=nil)
  name = node.class.to_s.split("::").last

  name = "#{name} [#{idx}]" if idx

  indented_print level, name
end