/
graph.rb
231 lines (181 loc) · 4.19 KB
/
graph.rb
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
$: << File.expand_path(File.dirname(__FILE__))
require 'compiler'
require 'generator'
require 'bytecode'
require 'text'
class Compiler::Node
# Debug print for the AST
def describe_ast(indent = 0, stack = [self])
str, offset = '', (' ' * indent)
str << "#{self.class.name.split('::').last}\n"
ivs = instance_variables.map {|iv| [iv.sub('@', '.'), instance_variable_get(iv)] }
max = ivs.sort_by {|iv| iv.first.length }.last.first.length
ivs.each do |name|
name, var = name.first, name.last
next if name == '.compiler'
str << "#{offset}#{name.ljust(max)} = "
if var.kind_of? Array
indented = var.map do |v|
if v.kind_of? Compiler::Node
if stack.include? v
"#<#{var.class.name} RECURSIVE...>"
else
v.describe_ast(indent + max + 3 + 1, stack).chomp
end
elsif v.class.name =~ /^Compiler/
"#<#{v.class.name} ...>"
else
v.inspect
end
end.join(",\n#{' ' * (indent + max + 3 + 1)}")
str << "[" << indented << "]" << "\n"
next
end
if var.kind_of? Compiler::Node
if stack.include? var
str << "#<#{var.class.name} RECURSIVE...>"
else
str << var.describe_ast(indent + max + 3, stack)
end
elsif var.class.name =~ /^Compiler/
str << "#<#{var.class.name} ...>" << "\n"
else
str << var.inspect << "\n"
end
end
stack.pop
str
end
# More sophisticated graphing
def to_dot(output, ds)
i = ds.next
output << %Q!node#{i} [shape=box, label="#{dot_name}", fontname="Monaco"];\n!
orig = i
instance_variables.each do |name|
obj = instance_variable_get(name)
n = name.to_s[::Range.new(1,-1)]
if obj.kind_of? Compiler::Node
j = obj.to_dot(output, ds)
output << "node#{orig} -> node#{j} [label=\" #{n} \", fontsize=8.0, fontname=\"Monaco\"];\n"
elsif obj.kind_of? Array
j = 0
obj.each do |x|
if x.kind_of? Compiler::Node
k = x.to_dot(output, ds)
output << "node#{orig} -> node#{k} [label=\"#{n}:#{j}\", fontsize=8.0, fontname=\"Monaco\"];\n"
end
j += 1
end
end
end
orig
end
def dot_name
self.class.name.split(':').last
end
class FCall
def dot_name
"fcall:#{@method}"
end
end
class Call
def dot_name
"call:#{@method}"
end
end
class LocalAccess
def dot_name
"lvar:#{@name}"
end
end
class LocalAssignment
def dot_name
"lasgn:#{@name}"
end
end
class Newline
def dot_name
"line:#{@line}"
end
end
class StringLiteral
def dot_name
r = ::Range.new(1,-2)
"'#{@string.dump[r]}'"
end
end
class NumberLiteral
def dot_name
"#{@value}"
end
end
class ArrayLiteral
def dot_name
"[...]"
end
end
class HashLiteral
def dot_name
"{...}"
end
end
class IVar
def dot_name
"ivar:#{@name}"
end
end
class IAssign
def dot_name
"iasgn:#{@name}"
end
end
class Literal
def dot_name
if @value.kind_of? Symbol
":#{@value.to_s.dump[::Range.new(1,-2)]}"
else
"#{@value.inspect.dump[::Range.new(1,-2)]}"
end
end
end
class Nil
def dot_name
"nil"
end
end
class True
def dot_name
"true"
end
end
class False
def dot_name
"false"
end
end
end
class DotState
def initialize
@node = 0
end
def next
@node += 1
end
end
if "./#{$0}" == __FILE__ # TODO: Fix this when we set the same way
file = ARGV.shift
gen = Compiler::Generator
c = Compiler.new(gen)
if file
x = File.to_sexp(file)
n = c.into_script(x)
else
code = Readline.readline("> ")
n = c.into_script(code.to_sexp)
end
output = "digraph G {\n"
ds = DotState.new
n.to_dot(output, ds)
output << "}\n"
puts output
end