-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.rb
349 lines (273 loc) · 6.66 KB
/
ast.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
require 'pp'
module Ddir
class Ast
class Context
attr_reader :depth
def initialize
@depth = 0
@expressions_stack = []
@locals_stack = [[]]
end
def push_expressions
exprs = Expressions.new depth: depth
@expressions_stack.push exprs
yield
@expressions_stack.pop while @expressions_stack.include? exprs
exprs
end
def current_expressions
@expressions_stack.last
end
def add_child(depth, child)
update_depth depth
exprs_at(depth).add_child child
end
def modify(depth, &block)
update_depth depth
exprs_at(depth-1).modify_child(&block)
end
def exprs_at(depth)
current_expressions.child_at depth
end
def update_depth(new_depth)
@depth = new_depth
@expressions_stack.pop while depth < current_expressions.depth
end
def declare_local(local)
@locals_stack.last << local
local
end
def record_locals
@locals_stack.push []
yield
@locals_stack.pop
end
end
include Enumerable
def initialize(attrs={})
return if attrs.empty?
raise "Additional attrs! #{attrs.inspect} for #{self.class.inspect}"
end
def children
raise NotImplementedError, 'Override #children in subclasses'
end
def type
@type ||= classname.gsub(/([a-z])([A-Z])/, '\1_\2').downcase.intern
end
def each(&block)
children.each(&block)
end
def inspect
pretty_inspect
end
def pretty_print(pp)
pp.text "#<#{classname}"
pp.group 2 do
pp.breakable '' # place inside so that if we break, we are indented
last_child = children.last
children.each do |child|
# it can break with a comma/newline, after any child except after the last
pp.pp child
pp.comma_breakable unless child == last_child
end
end
pp.breakable # can break after last child
pp.text '>'
end
def classname
@classname ||= self.class.to_s.split('::').last
end
class Body < Ast
attr_accessor :expressions
def initialize(expressions:, **rest)
self.expressions = expressions # expressions node, not a collection
super rest
end
def children
if expressions.length == 1
expressions.children
else
[expressions]
end
end
end
class Expressions < Ast
attr_accessor :expressions, :depth
def initialize(depth:, expressions:[], **rest)
self.expressions = expressions
self.depth = depth
super rest
end
def length
expressions.length
end
def children
expressions
end
def add_child(child)
children << child
child
end
def modify_child(&block)
children.push block.call children.pop
end
def child_at(depth)
return self if self.depth == depth
expressions.last.child_at(depth)
end
end
class EntryLocation < Ast
attr_accessor :name, :body, :depth
def initialize(name:, body:, depth:, **rest)
self.name = name
self.body = body
self.depth = depth
super rest
end
def children
[name, body]
end
def via_class?
!!(name =~ /^[A-Z]/)
end
def via_method?
!via_class?
end
def child_at(depth)
self.body ||= Block.new(depth: self.depth)
body.child_at(depth)
end
end
class BinaryExpression < Ast
attr_accessor :left_child, :operator, :right_child
def initialize(left_child:, operator:, right_child:, **rest)
self.left_child = left_child
self.operator = operator
self.right_child = right_child
super rest
end
def children
[left_child, operator, right_child]
end
end
class Assignment < Ast
attr_accessor :target, :value
def initialize(target:, value:, **rest)
self.target = target
self.value = value
super rest
end
def children
[target, value]
end
end
class SendMessage < Ast
attr_accessor :receiver, :name, :arguments, :block, :depth
def initialize(receiver:, name:, arguments:, block:, depth:, **rest)
self.receiver = receiver
self.name = name
self.arguments = arguments
self.block = block
self.depth = depth
super rest
end
def children
[receiver, '.', name, arguments, block]
end
def child_at(depth)
self.block ||= Block.new(depth: self.depth)
block.child_at(depth)
end
end
class Block < Ast
attr_accessor :params, :body, :depth
def initialize(params:[], body:nil, depth:, **rest)
self.params = params
self.body = body
self.depth = depth
super rest
end
def params?
params.any?
end
def children
[body]
end
def child_at(depth)
self.body ||= Expressions.new depth: self.depth+1
body.child_at(depth)
end
end
class Params < Ast
attr_accessor :params
def initialize(params:, **rest)
self.params = params
super rest
end
def children
params
end
end
class DestructuredParam < Params
end
class Param < Ast
attr_accessor :name
def initialize(name:, **rest)
self.name = name
super rest
end
def children
[]
end
end
class DefaultParam < Param
attr_accessor :value
def initialize(value:, **rest)
self.value = value
super rest
end
end
class OrdinalParam < Param
end
class RequiredParam < Param
end
class ValueLiteral < Ast
attr_accessor :value
def initialize(value:, **rest)
self.value = value
super rest
end
def children
[value]
end
end
class Integer < ValueLiteral
end
class Symbol < ValueLiteral
end
class String < ValueLiteral
end
class Self < Ast
def children
[]
end
end
class Variable < Ast
attr_accessor :name
def initialize(name:, **rest)
self.name = name
super rest
end
def children
[]
end
def pretty_print(pp)
pp.text "#<#{classname} #{name.inspect}>"
end
end
class LocalVariable < Variable
end
class InstanceVariable < Variable
end
end
end