-
Notifications
You must be signed in to change notification settings - Fork 61
/
flow.rb
381 lines (327 loc) · 10.3 KB
/
flow.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# Copyright (c) 2010 The Mirah project authors. All Rights Reserved.
# All contributing project authors may be found in the NOTICE file.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
module Mirah
module AST
class Condition < Node
child :predicate
def initialize(parent, line_number, &block)
super(parent, line_number, &block)
end
def infer(typer, expression)
unless resolved?
@inferred_type = typer.infer(predicate, true)
if @inferred_type && !@inferred_type.primitive?
call = Call.new(parent, position, '!=') do |call|
predicate.parent = call
[predicate, [Null.new(call, position)]]
end
self.predicate = call
@inferred_type = typer.infer(predicate, true)
end
@inferred_type ? resolved! : typer.defer(self)
end
@inferred_type
end
end
class If < Node
child :condition
child :body
child :else
def initialize(parent, line_number, &block)
super(parent, line_number, &block)
end
def infer(typer, expression)
unless resolved?
condition_type = typer.infer(condition, true)
unless condition_type
typer.defer(condition)
end
# condition type is unrelated to body types, so we proceed with bodies
then_type = typer.infer(body, expression) if body
else_type = typer.infer(self.else, expression) if self.else
if expression
have_body_type = body.nil? || then_type
have_else_type = self.else.nil? || else_type
if have_body_type && have_else_type
if then_type && else_type
# both then and else inferred, ensure they're compatible
if then_type.compatible?(else_type)
# types are compatible...if condition is resolved, we're done
@inferred_type = then_type.narrow(else_type)
resolved! if condition_type
else
raise Mirah::Typer::InferenceError.new("if statement with incompatible result types #{then_type} and #{else_type}")
end
else
@inferred_type = then_type || else_type
resolved!
end
else
typer.defer(self)
end
else
@inferred_type = typer.no_type
resolved!
end
end
@inferred_type
end
end
class Loop < Node
child :init
child :condition
child :pre
child :body
child :post
attr_accessor :check_first, :negative, :redo
def initialize(parent, position, check_first, negative, &block)
@check_first = check_first
@negative = negative
@children = [
Body.new(self, position),
nil,
Body.new(self, position),
nil,
Body.new(self, position),
]
super(parent, position) do |l|
condition, body = yield(l)
[self.init, condition, self.pre, body, self.post]
end
end
def infer(typer, expression)
unless resolved?
child_types = children.map do |c|
if c.nil? || (Body === c && c.empty?)
typer.no_type
else
typer.infer(c, true)
end
end
if child_types.any? {|t| t.nil?}
typer.defer(self)
else
resolved!
@inferred_type = typer.null_type
end
end
@inferred_type
end
def check_first?; @check_first; end
def negative?; @negative; end
def redo?
if @redo.nil?
nodes = @children.dup
until nodes.empty?
node = nodes.shift
while node.respond_to?(:inlined) && node.inlined
node = node.inlined
end
next if node.nil? || Loop === node
if Redo === node
return @redo = true
end
nodes.insert(-1, *node.children.flatten)
end
return @redo = false
else
@redo
end
end
def init?
init && !(init.kind_of?(Body) && init.empty?)
end
def pre?
pre && !(pre.kind_of?(Body) && pre.empty?)
end
def post?
post && !(post.kind_of?(Body) && post.empty?)
end
def to_s
"Loop(check_first = #{check_first?}, negative = #{negative?})"
end
end
class Not < Node
def initialize(parent, line_number, &block)
super(parent, line_number, &block)
end
end
class Return < Node
include Valued
child :value
def initialize(parent, line_number, &block)
super(parent, line_number, &block)
end
def infer(typer, expression)
resolve_if(typer) do
if value
typer.infer(value, true)
else
typer.no_type
end
end
end
end
class Break < Node;
def infer(typer, expression)
unless resolved?
resolved!
@inferred_type = typer.null_type
end
@inferred_type
end
end
class Next < Break; end
class Redo < Break; end
class Raise < Node
include Valued
child :exception
def initialize(parent, line_number, &block)
super(parent, line_number, &block)
end
def infer(typer, expression)
unless resolved?
@inferred_type = AST.unreachable_type
throwable = AST.type(nil, 'java.lang.Throwable')
if children.size == 1
arg_type = typer.infer(self.exception, true)
unless arg_type
typer.defer(self)
return
end
if throwable.compatible?(arg_type) && !arg_type.meta?
resolved!
return @inferred_type
end
end
arg_types = children.map {|c| typer.infer(c, true)}
if arg_types.any? {|c| c.nil?}
typer.defer(self)
else
if arg_types[0] && throwable.compatible?(arg_types[0])
klass = children.shift
else
klass = Constant.new(self, position, 'RuntimeException')
end
exception = Call.new(self, position, 'new') do
[klass, children, nil]
end
resolved!
@children = [exception]
typer.infer(exception, true)
end
end
@inferred_type
end
end
defmacro('raise') do |transformer, fcall, parent|
Raise.new(parent, fcall.position) do |raise_node|
fcall.parameters
end
end
class RescueClause < Node
include Scope
include Scoped
attr_accessor :name, :type, :types
child :type_nodes
child :body
def initialize(parent, position)
super(parent, position) do
static_scope.parent = scope.static_scope
yield(self) if block_given?
end
end
def infer(typer, expression)
unless resolved?
@types ||= type_nodes.map {|n| n.type_reference(typer)}
if name
static_scope.shadow(name)
# TODO find the common parent Throwable
@type = types.size == 1 ? types[0] : AST.type(nil, 'java.lang.Throwable')
typer.learn_local_type(static_scope, name, @type)
end
@inferred_type = typer.infer(body, true)
(@inferred_type && body.resolved?) ? resolved! : typer.defer(self)
end
@inferred_type
end
def binding_type(duby=nil)
static_scope.parent.binding_type(defining_class, duby)
end
def binding_type=(type)
static_scope.parent.binding_type = type
end
def has_binding?
static_scope.parent.has_binding?
end
end
class Rescue < Node
child :body
child :clauses
child :else_node
def initialize(parent, position, &block)
super(parent, position, &block)
end
def infer(typer, expression)
unless resolved?
# TODO: generalize this s.t.
# the problem with deferred inference
# assuming expression == true is dealt with
@expression = expression if @expression == nil
expression = @expression
primary_type = if else_node
typer.infer(body, false) if body
typer.infer(else_node, expression)
elsif body
typer.infer(body, expression)
end
clause_types = clauses.map {|c| typer.infer(c, expression)}
types = []
types << primary_type if primary_type
types += clause_types
if types.any? {|t| t.nil?}
typer.defer(self)
else
if !expression || clause_types.all?{ |t| primary_type.compatible? t}
resolved!
types.each do |type|
@inferred_type ||= type unless type.unreachable?
end
@inferred_type ||= primary_type
else
clause, clause_type = clauses.zip(clause_types).find{ |clause, t| !primary_type.compatible? t }
raise Mirah::Typer::InferenceError.new("rescue statement with incompatible result types #{primary_type} and #{clause_type}", clause)
end
end
end
@inferred_type
end
end
class Ensure < Node
child :body
child :clause
attr_accessor :state # Used by some compilers.
def initialize(parent, position, &block)
super(parent, position, &block)
end
def infer(typer, expression)
resolve_if(typer) do
typer.infer(clause, false)
typer.infer(body, true)
end
end
end
end
end