1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 52 53 54 55 56 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
require 'jruby'
code1 = "a = 1; b = 2; if a < b; c = 3; else; c = 4; end; c = 5"
code2 = "a = 1; b = 2"
code3 = "
a = 1
if a < 2
a
else
fib(a - 1) + fib(a - 2)
end"
code4 = "
if alpha
if beta
if charlie
if delta
echo
end
foxtrot
elsif golf
hotel
end
india
else
juliet
end
elsif kilo
lima
end
mike"
code5 = "
if 1
if 2
3
end
else
if 4
5
end
end
"
code6 = "
a = 1
while a < 100
a += 1
end
puts a
"
code7 = "
x = y = z = 1
if y >= x
return z
else
return tak( tak(x-1, y, z),
tak(y-1, z, x),
tak(z-1, x, y))
end
"
node = JRuby.parse(code7)
head = org.jruby.compiler.DAGBuilder.get_extents(node).head
require 'rubygems'
require 'graphviz'
g = GraphViz.new('G')
def add_graph(g, gnode, head, hash = {})
hash[head.name] ||= 0
nxt = hash[head.object_id]
unless nxt
hash[head.name] += 1
shape = head.name == "PHI" ? "ellipse" : "record"
nxt = g.add_node(head.name + hash[head.name].to_s, :label => "#{label(head)}", :shape => shape)
hash[head.object_id] = nxt
end
hash["edge #{gnode.object_id},#{nxt.object_id}"] ||= (g.add_edge(gnode, nxt); true)
if head.tail && shape
if org.jruby.compiler.DAGBuilder::B === head
add_graph(g, nxt, head.tail, hash)
add_graph(g, nxt, head.alt, hash);
else
add_graph(g, nxt, head.tail, hash)
end
end
end
def label(node)
label = node.name
if node.payload.size > 0
label << " [#{node.payload.to_a.join(', ')}] (#{node.type})"
end
label
end
root = g.add_node("root")
add_graph(g, root, head)
g.output(:output => "png", :file => "output.png")
system "open output.png"
|