katakata_irb 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/katakata_irb/completor.rb +86 -11
- data/lib/katakata_irb/nesting_parser.rb +34 -3
- data/lib/katakata_irb/scope.rb +256 -0
- data/lib/katakata_irb/type_simulator.rb +184 -363
- data/lib/katakata_irb/types.rb +124 -19
- data/lib/katakata_irb/version.rb +1 -1
- data/sig/katakata_irb.rbs +4 -1
- metadata +4 -3
@@ -1,6 +1,7 @@
|
|
1
|
-
require_relative 'types'
|
2
1
|
require 'ripper'
|
3
2
|
require 'set'
|
3
|
+
require_relative 'types'
|
4
|
+
require_relative 'scope'
|
4
5
|
|
5
6
|
class KatakataIrb::TypeSimulator
|
6
7
|
class DigTarget
|
@@ -17,228 +18,6 @@ class KatakataIrb::TypeSimulator
|
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
20
|
-
class BaseScope
|
21
|
-
def initialize(binding, self_object)
|
22
|
-
@binding, @self_object = binding, self_object
|
23
|
-
@cache = { SELF => KatakataIrb::Types.type_from_object(self_object) }
|
24
|
-
@local_variables = binding.local_variables.map(&:to_s).to_set
|
25
|
-
end
|
26
|
-
|
27
|
-
def mutable?() = false
|
28
|
-
|
29
|
-
def [](name)
|
30
|
-
@cache[name] ||= (
|
31
|
-
fallback = KatakataIrb::Types::NIL
|
32
|
-
case BaseScope.type_by_name name
|
33
|
-
when :cvar
|
34
|
-
KatakataIrb::TypeSimulator.type_of(fallback: fallback) { @self_object.class_variable_get name }
|
35
|
-
when :ivar
|
36
|
-
KatakataIrb::TypeSimulator.type_of(fallback: fallback) { @self_object.instance_variable_get name }
|
37
|
-
when :lvar
|
38
|
-
KatakataIrb::TypeSimulator.type_of(fallback: fallback) { @binding.local_variable_get(name) }
|
39
|
-
when :const
|
40
|
-
KatakataIrb::TypeSimulator.type_of(fallback: fallback) { @binding.eval name }
|
41
|
-
end
|
42
|
-
)
|
43
|
-
end
|
44
|
-
|
45
|
-
def self_type
|
46
|
-
self[SELF]
|
47
|
-
end
|
48
|
-
|
49
|
-
def local_variables
|
50
|
-
@local_variables.to_a
|
51
|
-
end
|
52
|
-
|
53
|
-
def self.type_by_name(name)
|
54
|
-
if name.start_with? '@@'
|
55
|
-
:cvar
|
56
|
-
elsif name.start_with? '@'
|
57
|
-
:ivar
|
58
|
-
elsif name.start_with? '$'
|
59
|
-
:gvar
|
60
|
-
elsif name.start_with? '%'
|
61
|
-
:internal
|
62
|
-
elsif name[0].downcase != name[0]
|
63
|
-
:const
|
64
|
-
else
|
65
|
-
:lvar
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def has?(name)
|
70
|
-
case BaseScope.type_by_name name
|
71
|
-
when :cvar
|
72
|
-
@self_object.class_variable_defined? name
|
73
|
-
when :ivar
|
74
|
-
@self_object.instance_variable_defined? name
|
75
|
-
when :lvar
|
76
|
-
@local_variables.include? name
|
77
|
-
when :const
|
78
|
-
@binding.eval("#{name};true") rescue false
|
79
|
-
when :internal
|
80
|
-
true
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
class Scope
|
86
|
-
attr_reader :parent, :jump_branches
|
87
|
-
|
88
|
-
def self.from_binding(binding) = new(BaseScope.new(binding, binding.eval('self')))
|
89
|
-
|
90
|
-
def initialize(parent, table = {}, trace_cvar: true, trace_ivar: true, trace_lvar: true, passthrough: false)
|
91
|
-
@tables = [table]
|
92
|
-
@parent = parent
|
93
|
-
@trace_cvar = trace_cvar
|
94
|
-
@trace_ivar = trace_ivar
|
95
|
-
@trace_lvar = trace_lvar
|
96
|
-
@passthrough = passthrough
|
97
|
-
@terminated = false
|
98
|
-
@jump_branches = []
|
99
|
-
end
|
100
|
-
|
101
|
-
def mutable? = true
|
102
|
-
|
103
|
-
def terminated?
|
104
|
-
@terminated
|
105
|
-
end
|
106
|
-
|
107
|
-
def terminate_with(type, value)
|
108
|
-
return if terminated?
|
109
|
-
self[type] = value
|
110
|
-
store_jump type
|
111
|
-
terminate
|
112
|
-
end
|
113
|
-
|
114
|
-
def store_jump(type)
|
115
|
-
scopes = ancestors.select(&:mutable?)
|
116
|
-
scope = scopes.find { _1.has_own? type } || scopes.last
|
117
|
-
index = scopes.index scope
|
118
|
-
scope.jump_branches << scopes.drop(index).map(&:branch_table_clone)
|
119
|
-
end
|
120
|
-
|
121
|
-
def terminate
|
122
|
-
@terminated = true
|
123
|
-
end
|
124
|
-
|
125
|
-
def branch_table_clone() = @tables.last.dup
|
126
|
-
|
127
|
-
def trace?(name)
|
128
|
-
return false unless @parent
|
129
|
-
type = BaseScope.type_by_name(name)
|
130
|
-
type == :cvar ? @trace_cvar : type == :ivar ? @trace_ivar : type == :lvar ? @trace_lvar : true
|
131
|
-
end
|
132
|
-
|
133
|
-
def [](name)
|
134
|
-
@tables.reverse_each do |table|
|
135
|
-
return table[name] if table.key? name
|
136
|
-
end
|
137
|
-
@parent[name] if trace? name
|
138
|
-
end
|
139
|
-
|
140
|
-
def []=(name, type)
|
141
|
-
if @passthrough && BaseScope.type_by_name(name) != :internal
|
142
|
-
@parent[name] = type
|
143
|
-
elsif trace?(name) && @parent.mutable? && !@tables.any? { _1.key? name } && @parent.has?(name)
|
144
|
-
@parent[name] = type
|
145
|
-
else
|
146
|
-
@tables.last[name] = type
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
def self_type
|
151
|
-
self[SELF]
|
152
|
-
end
|
153
|
-
|
154
|
-
def local_variables
|
155
|
-
lvar_keys = @tables.flat_map(&:keys).select do |name|
|
156
|
-
BaseScope.type_by_name(name) == :lvar
|
157
|
-
end
|
158
|
-
lvar_keys |= @parent.local_variables if @trace_lvar
|
159
|
-
lvar_keys
|
160
|
-
end
|
161
|
-
|
162
|
-
def start_branch
|
163
|
-
@tables << {}
|
164
|
-
end
|
165
|
-
|
166
|
-
def end_branch
|
167
|
-
@tables.pop
|
168
|
-
end
|
169
|
-
|
170
|
-
def merge_jumps
|
171
|
-
if terminated?
|
172
|
-
merge @jump_branches
|
173
|
-
else
|
174
|
-
merge [*@jump_branches, [{}] * ancestors.size]
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
def merge_branch(tables)
|
179
|
-
target_table = @tables.last
|
180
|
-
keys = tables.flat_map(&:keys).uniq
|
181
|
-
keys.each do |key|
|
182
|
-
original_value = self[key] || KatakataIrb::Types::NIL
|
183
|
-
target_table[key] = KatakataIrb::Types::UnionType[*tables.map { _1[key] || original_value }.uniq]
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
def ancestors
|
188
|
-
scopes = [self]
|
189
|
-
scopes << scopes.last.parent while scopes.last.parent&.mutable?
|
190
|
-
scopes
|
191
|
-
end
|
192
|
-
|
193
|
-
def conditional(&block)
|
194
|
-
run_branches(block, -> {}).first || KatakataIrb::Types::NIL
|
195
|
-
end
|
196
|
-
|
197
|
-
def never(&block)
|
198
|
-
branch(&block)
|
199
|
-
end
|
200
|
-
|
201
|
-
def run_branches(*blocks)
|
202
|
-
results = blocks.map { branch(&_1) }.reject(&:last)
|
203
|
-
merge results.map { _2 }
|
204
|
-
if results.empty?
|
205
|
-
terminate
|
206
|
-
[]
|
207
|
-
else
|
208
|
-
results.map(&:first)
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
def branch
|
213
|
-
scopes = ancestors
|
214
|
-
scopes.each(&:start_branch)
|
215
|
-
@terminated = false
|
216
|
-
result = yield
|
217
|
-
terminated = @terminated
|
218
|
-
@terminated = false
|
219
|
-
[result, scopes.map(&:end_branch), terminated]
|
220
|
-
end
|
221
|
-
|
222
|
-
def merge(branches)
|
223
|
-
scopes = ancestors
|
224
|
-
scopes.zip(*branches).each do |scope, *tables|
|
225
|
-
scope.merge_branch(tables)
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
def base_scope
|
230
|
-
@parent&.mutable? ? @parent.base_scope : @parent
|
231
|
-
end
|
232
|
-
|
233
|
-
def has_own?(name)
|
234
|
-
@tables.any? { _1.key? name }
|
235
|
-
end
|
236
|
-
|
237
|
-
def has?(name)
|
238
|
-
has_own?(name) || (trace?(name) && @parent.has?(name))
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
21
|
module LexerElemMatcher
|
243
22
|
refine Ripper::Lexer::Elem do
|
244
23
|
def deconstruct_keys(_keys)
|
@@ -268,13 +47,6 @@ class KatakataIrb::TypeSimulator
|
|
268
47
|
to_r: KatakataIrb::Types::RATIONAL
|
269
48
|
}
|
270
49
|
|
271
|
-
SELF = '%self'
|
272
|
-
BREAK_RESULT = '%break'
|
273
|
-
NEXT_RESULT = '%next'
|
274
|
-
RETURN_RESULT = '%return'
|
275
|
-
PATTERNMATCH_BREAK = '%match'
|
276
|
-
RAISE_BREAK = '%raise'
|
277
|
-
|
278
50
|
def initialize(dig_targets)
|
279
51
|
@dig_targets = dig_targets
|
280
52
|
end
|
@@ -298,7 +70,7 @@ class KatakataIrb::TypeSimulator
|
|
298
70
|
else
|
299
71
|
current_self_types = scope.self_type.types
|
300
72
|
self_types = current_self_types.map do |type|
|
301
|
-
if (
|
73
|
+
if type.is_a?(KatakataIrb::Types::SingletonType) && type.module_or_class.is_a?(Class)
|
302
74
|
KatakataIrb::Types::InstanceType.new type.module_or_class
|
303
75
|
else
|
304
76
|
type
|
@@ -309,10 +81,16 @@ class KatakataIrb::TypeSimulator
|
|
309
81
|
if @dig_targets.dig? sexp
|
310
82
|
params in [:paren, params]
|
311
83
|
params_table = extract_param_names(params).to_h { [_1, KatakataIrb::Types::NIL] }
|
312
|
-
method_scope = Scope.new
|
84
|
+
method_scope = KatakataIrb::Scope.new(
|
85
|
+
scope,
|
86
|
+
{ **params_table, KatakataIrb::Scope::SELF => self_type, KatakataIrb::Scope::BREAK_RESULT => nil, KatakataIrb::Scope::NEXT_RESULT => nil, KatakataIrb::Scope::RETURN_RESULT => nil },
|
87
|
+
trace_lvar: false
|
88
|
+
)
|
313
89
|
evaluate_assign_params params, [], method_scope
|
314
|
-
method_scope.conditional { evaluate_param_defaults params,
|
90
|
+
method_scope.conditional { evaluate_param_defaults params, _1 }
|
315
91
|
simulate_evaluate body_stmt, method_scope
|
92
|
+
method_scope.merge_jumps
|
93
|
+
scope.update method_scope
|
316
94
|
end
|
317
95
|
KatakataIrb::Types::SYMBOL
|
318
96
|
in [:@int,]
|
@@ -351,10 +129,10 @@ class KatakataIrb::TypeSimulator
|
|
351
129
|
in [:array, [:args_add_star,] => star]
|
352
130
|
args, kwargs = retrieve_method_args star
|
353
131
|
types = args.flat_map do |elem|
|
354
|
-
if elem
|
132
|
+
if elem.is_a? KatakataIrb::Types::Splat
|
355
133
|
splat = simulate_evaluate elem.item, scope
|
356
|
-
|
357
|
-
|
134
|
+
array_elem, non_array = partition_to_array splat.nonnillable, :to_a
|
135
|
+
KatakataIrb::Types::UnionType[*array_elem, *non_array]
|
358
136
|
else
|
359
137
|
simulate_evaluate elem, scope
|
360
138
|
end
|
@@ -395,10 +173,10 @@ class KatakataIrb::TypeSimulator
|
|
395
173
|
values << simulate_evaluate(value, scope)
|
396
174
|
in [:assoc_splat, value]
|
397
175
|
hash = simulate_evaluate value, scope
|
398
|
-
unless (
|
176
|
+
unless hash.is_a?(KatakataIrb::Types::InstanceType) && hash.klass == Hash
|
399
177
|
hash = simulate_call hash, :to_hash, [], nil, nil
|
400
178
|
end
|
401
|
-
if (
|
179
|
+
if hash.is_a?(KatakataIrb::Types::InstanceType) && hash.klass == Hash
|
402
180
|
keys << hash.params[:K] if hash.params[:K]
|
403
181
|
values << hash.params[:V] if hash.params[:V]
|
404
182
|
end
|
@@ -414,7 +192,7 @@ class KatakataIrb::TypeSimulator
|
|
414
192
|
statements.map { simulate_evaluate _1, scope }.last
|
415
193
|
in [:const_path_ref, receiver, [:@const, name,]]
|
416
194
|
r = simulate_evaluate receiver, scope
|
417
|
-
(
|
195
|
+
r.is_a?(KatakataIrb::Types::SingletonType) ? KatakataIrb::BaseScope.type_of { r.module_or_class.const_get name } : KatakataIrb::Types::NIL
|
418
196
|
in [:__var_ref_or_call, [type, name, pos]]
|
419
197
|
sexp = scope.has?(name) ? [:var_ref, [type, name, pos]] : [:vcall, [:@ident, name, pos]]
|
420
198
|
simulate_evaluate sexp, scope
|
@@ -443,7 +221,7 @@ class KatakataIrb::TypeSimulator
|
|
443
221
|
receiver_type = simulate_evaluate receiver, scope if receiver
|
444
222
|
args, kwargs, _block = retrieve_method_args args
|
445
223
|
args_type = args.map do |arg|
|
446
|
-
if arg
|
224
|
+
if arg.is_a? KatakataIrb::Types::Splat
|
447
225
|
simulate_evaluate arg.item, scope
|
448
226
|
nil # TODO: splat
|
449
227
|
else
|
@@ -456,15 +234,15 @@ class KatakataIrb::TypeSimulator
|
|
456
234
|
# workaround for https://bugs.ruby-lang.org/issues/19175
|
457
235
|
return scope[name]
|
458
236
|
end
|
459
|
-
receiver, method, args, kwargs, block,
|
237
|
+
receiver, method, args, kwargs, block, optional_chain = retrieve_method_call sexp
|
460
238
|
if receiver.nil? && method == 'raise'
|
461
|
-
scope.terminate_with RAISE_BREAK, KatakataIrb::Types::TRUE
|
239
|
+
scope.terminate_with KatakataIrb::Scope::RAISE_BREAK, KatakataIrb::Types::TRUE
|
462
240
|
return KatakataIrb::Types::NIL
|
463
241
|
end
|
464
242
|
receiver_type = receiver ? simulate_evaluate(receiver, scope) : scope.self_type
|
465
|
-
evaluate_method = lambda do
|
243
|
+
evaluate_method = lambda do |scope|
|
466
244
|
args_type = args.map do |arg|
|
467
|
-
if arg
|
245
|
+
if arg.is_a? KatakataIrb::Types::Splat
|
468
246
|
simulate_evaluate arg.item, scope
|
469
247
|
nil # TODO: splat
|
470
248
|
else
|
@@ -474,14 +252,14 @@ class KatakataIrb::TypeSimulator
|
|
474
252
|
|
475
253
|
if block
|
476
254
|
if block in [:symbol_literal, [:symbol, [:@ident, block_name,]]]
|
477
|
-
call_block_proc = ->(block_args) do
|
255
|
+
call_block_proc = ->(block_args, _self_type) do
|
478
256
|
block_receiver, *rest = block_args
|
479
257
|
block_receiver ? simulate_call(block_receiver || KatakataIrb::Types::OBJECT, block_name, rest, nil, nil) : KatakataIrb::Types::OBJECT
|
480
258
|
end
|
481
259
|
elsif block in [:do_block | :brace_block => type, block_var, body]
|
482
260
|
block_var in [:block_var, params,]
|
483
|
-
call_block_proc = ->(block_args) do
|
484
|
-
|
261
|
+
call_block_proc = ->(block_args, block_self_type) do
|
262
|
+
scope.conditional do |s|
|
485
263
|
if params
|
486
264
|
names = extract_param_names(params)
|
487
265
|
else
|
@@ -489,40 +267,52 @@ class KatakataIrb::TypeSimulator
|
|
489
267
|
params = [:params, names.map { [:@ident, _1, [0, 0]] }, nil, nil, nil, nil, nil, nil]
|
490
268
|
end
|
491
269
|
params_table = names.zip(block_args).to_h { [_1, _2 || KatakataIrb::Types::NIL] }
|
492
|
-
|
270
|
+
table = { **params_table, KatakataIrb::Scope::BREAK_RESULT => nil, KatakataIrb::Scope::NEXT_RESULT => nil }
|
271
|
+
table[KatakataIrb::Scope::SELF] = block_self_type if block_self_type
|
272
|
+
block_scope = KatakataIrb::Scope.new s, table
|
493
273
|
evaluate_assign_params params, block_args, block_scope
|
494
|
-
block_scope.conditional { evaluate_param_defaults params,
|
274
|
+
block_scope.conditional { evaluate_param_defaults params, _1 } if params
|
495
275
|
if type == :do_block
|
496
276
|
result = simulate_evaluate body, block_scope
|
497
277
|
else
|
498
278
|
result = body.map { simulate_evaluate _1, block_scope }.last
|
499
279
|
end
|
500
280
|
block_scope.merge_jumps
|
501
|
-
|
502
|
-
|
281
|
+
s.update block_scope
|
282
|
+
nexts = block_scope[KatakataIrb::Scope::NEXT_RESULT]
|
283
|
+
breaks = block_scope[KatakataIrb::Scope::BREAK_RESULT]
|
284
|
+
if block_scope.terminated?
|
285
|
+
[KatakataIrb::Types::UnionType[*nexts], breaks]
|
286
|
+
else
|
287
|
+
[KatakataIrb::Types::UnionType[result, *nexts], breaks]
|
288
|
+
end
|
503
289
|
end
|
504
|
-
[KatakataIrb::Types::UnionType[result, *nexts], breaks]
|
505
290
|
end
|
506
291
|
else
|
507
|
-
call_block_proc = ->(_block_args) { KatakataIrb::Types::OBJECT }
|
292
|
+
call_block_proc = ->(_block_args, _self_type) { KatakataIrb::Types::OBJECT }
|
508
293
|
simulate_evaluate block, scope
|
509
294
|
end
|
510
295
|
end
|
511
296
|
simulate_call receiver_type, method, args_type, kwargs_type(kwargs, scope), call_block_proc
|
512
297
|
end
|
513
|
-
if
|
514
|
-
scope.conditional { evaluate_method.call }
|
298
|
+
if optional_chain
|
299
|
+
result = scope.conditional { evaluate_method.call _1 }
|
300
|
+
if receiver_type.nillable?
|
301
|
+
KatakataIrb::Types::UnionType[result, KatakataIrb::Types::NIL]
|
302
|
+
else
|
303
|
+
result
|
304
|
+
end
|
515
305
|
else
|
516
|
-
evaluate_method.call
|
306
|
+
evaluate_method.call scope
|
517
307
|
end
|
518
308
|
in [:binary, a, Symbol => op, b]
|
519
309
|
atype = simulate_evaluate a, scope
|
520
310
|
case op
|
521
311
|
when :'&&', :and
|
522
|
-
btype = scope.conditional { simulate_evaluate b,
|
312
|
+
btype = scope.conditional { simulate_evaluate b, _1 }
|
523
313
|
KatakataIrb::Types::UnionType[btype, KatakataIrb::Types::NIL, KatakataIrb::Types::FALSE]
|
524
314
|
when :'||', :or
|
525
|
-
btype = scope.conditional { simulate_evaluate b,
|
315
|
+
btype = scope.conditional { simulate_evaluate b, _1 }
|
526
316
|
KatakataIrb::Types::UnionType[atype, btype]
|
527
317
|
else
|
528
318
|
btype = simulate_evaluate b, scope
|
@@ -535,11 +325,14 @@ class KatakataIrb::TypeSimulator
|
|
535
325
|
statements in [:bodystmt, statements, _unknown, _unknown, _unknown] # -> do end
|
536
326
|
params in [:paren, params]
|
537
327
|
params_table = extract_param_names(params).to_h { [_1, KatakataIrb::Types::NIL] }
|
538
|
-
block_scope = Scope.new scope, { **params_table, BREAK_RESULT => nil, NEXT_RESULT => nil, RETURN_RESULT => nil }
|
539
|
-
|
540
|
-
|
541
|
-
|
328
|
+
block_scope = KatakataIrb::Scope.new scope, { **params_table, KatakataIrb::Scope::BREAK_RESULT => nil, KatakataIrb::Scope::NEXT_RESULT => nil, KatakataIrb::Scope::RETURN_RESULT => nil }
|
329
|
+
block_scope.conditional do |s|
|
330
|
+
evaluate_assign_params params, [], s
|
331
|
+
s.conditional { evaluate_param_defaults params, _1 }
|
332
|
+
statements.each { simulate_evaluate _1, s }
|
333
|
+
end
|
542
334
|
block_scope.merge_jumps
|
335
|
+
scope.update block_scope
|
543
336
|
KatakataIrb::Types::ProcType.new
|
544
337
|
in [:assign, [:var_field, [:@gvar | :@ivar | :@cvar | :@ident | :@const, name,]], value]
|
545
338
|
res = simulate_evaluate value, scope
|
@@ -549,7 +342,7 @@ class KatakataIrb::TypeSimulator
|
|
549
342
|
simulate_evaluate receiver, scope
|
550
343
|
args, kwargs, _block = retrieve_method_args key
|
551
344
|
args.each do |arg|
|
552
|
-
item = (
|
345
|
+
item = arg.is_a?(KatakataIrb::Types::Splat) ? arg.item : arg
|
553
346
|
simulate_evaluate item, scope
|
554
347
|
end
|
555
348
|
kwargs_type kwargs, scope
|
@@ -587,35 +380,37 @@ class KatakataIrb::TypeSimulator
|
|
587
380
|
in [:ifop, cond, tval, fval]
|
588
381
|
simulate_evaluate cond, scope
|
589
382
|
KatakataIrb::Types::UnionType[*scope.run_branches(
|
590
|
-
-> { simulate_evaluate tval,
|
591
|
-
-> { simulate_evaluate fval,
|
383
|
+
-> { simulate_evaluate tval, _1 },
|
384
|
+
-> { simulate_evaluate fval, _1 }
|
592
385
|
)]
|
593
386
|
in [:if_mod | :unless_mod, cond, statement]
|
594
387
|
simulate_evaluate cond, scope
|
595
|
-
KatakataIrb::Types::UnionType[scope.conditional { simulate_evaluate statement,
|
388
|
+
KatakataIrb::Types::UnionType[scope.conditional { simulate_evaluate statement, _1 }, KatakataIrb::Types::NIL]
|
596
389
|
in [:if | :unless | :elsif, cond, statements, else_statement]
|
597
390
|
simulate_evaluate cond, scope
|
598
391
|
results = scope.run_branches(
|
599
|
-
-> { statements.map { simulate_evaluate _1,
|
600
|
-
-> { else_statement ? simulate_evaluate(else_statement,
|
392
|
+
->(s) { statements.map { simulate_evaluate _1, s }.last },
|
393
|
+
->(s) { else_statement ? simulate_evaluate(else_statement, s) : KatakataIrb::Types::NIL }
|
601
394
|
)
|
602
395
|
results.empty? ? KatakataIrb::Types::NIL : KatakataIrb::Types::UnionType[*results]
|
603
396
|
in [:while | :until, cond, statements]
|
604
|
-
inner_scope = Scope.new scope, { BREAK_RESULT => nil }, passthrough: true
|
397
|
+
inner_scope = KatakataIrb::Scope.new scope, { KatakataIrb::Scope::BREAK_RESULT => nil }, passthrough: true
|
605
398
|
simulate_evaluate cond, inner_scope
|
606
|
-
|
399
|
+
inner_scope.conditional { |s| statements.each { simulate_evaluate _1, s } }
|
607
400
|
inner_scope.merge_jumps
|
608
|
-
|
401
|
+
scope.update inner_scope
|
402
|
+
breaks = inner_scope[KatakataIrb::Scope::BREAK_RESULT]
|
609
403
|
breaks ? KatakataIrb::Types::UnionType[breaks, KatakataIrb::Types::NIL] : KatakataIrb::Types::NIL
|
610
404
|
in [:while_mod | :until_mod, cond, statement]
|
611
|
-
inner_scope = Scope.new scope, { BREAK_RESULT => nil }, passthrough: true
|
405
|
+
inner_scope = KatakataIrb::Scope.new scope, { KatakataIrb::Scope::BREAK_RESULT => nil }, passthrough: true
|
612
406
|
simulate_evaluate cond, inner_scope
|
613
|
-
|
407
|
+
inner_scope.conditional { |s| simulate_evaluate statement, s }
|
614
408
|
inner_scope.merge_jumps
|
615
|
-
|
409
|
+
scope.update inner_scope
|
410
|
+
breaks = inner_scope[KatakataIrb::Scope::BREAK_RESULT]
|
616
411
|
breaks ? KatakataIrb::Types::UnionType[breaks, KatakataIrb::Types::NIL] : KatakataIrb::Types::NIL
|
617
412
|
in [:break | :next | :return => jump_type, value]
|
618
|
-
internal_key = jump_type == :break ? BREAK_RESULT : jump_type == :next ? NEXT_RESULT : RETURN_RESULT
|
413
|
+
internal_key = jump_type == :break ? KatakataIrb::Scope::BREAK_RESULT : jump_type == :next ? KatakataIrb::Scope::NEXT_RESULT : KatakataIrb::Scope::RETURN_RESULT
|
619
414
|
if value.empty?
|
620
415
|
jump_value = KatakataIrb::Types::NIL
|
621
416
|
else
|
@@ -626,7 +421,7 @@ class KatakataIrb::TypeSimulator
|
|
626
421
|
scope.terminate_with internal_key, jump_value
|
627
422
|
KatakataIrb::Types::NIL
|
628
423
|
in [:return0]
|
629
|
-
scope.terminate_with RETURN_RESULT, KatakataIrb::Types::NIL
|
424
|
+
scope.terminate_with KatakataIrb::Scope::RETURN_RESULT, KatakataIrb::Types::NIL
|
630
425
|
KatakataIrb::Types::NIL
|
631
426
|
in [:yield, args]
|
632
427
|
evaluate_mrhs args, scope
|
@@ -640,7 +435,7 @@ class KatakataIrb::TypeSimulator
|
|
640
435
|
in [:super, args]
|
641
436
|
args, kwargs, _block = retrieve_method_args args
|
642
437
|
args.each do |arg|
|
643
|
-
item = (
|
438
|
+
item = arg.is_a?(KatakataIrb::Types::Splat) ? arg.item : arg
|
644
439
|
simulate_evaluate item, scope
|
645
440
|
end
|
646
441
|
kwargs_type kwargs, scope
|
@@ -649,55 +444,66 @@ class KatakataIrb::TypeSimulator
|
|
649
444
|
simulate_evaluate body_stmt, scope
|
650
445
|
in [:bodystmt, statements, rescue_stmt, _unknown, ensure_stmt]
|
651
446
|
statements = [statements] if statements in [Symbol,] # oneliner-def body
|
652
|
-
rescue_scope = Scope.new scope, { RAISE_BREAK => nil }, passthrough: true if rescue_stmt
|
447
|
+
rescue_scope = KatakataIrb::Scope.new scope, { KatakataIrb::Scope::RAISE_BREAK => nil }, passthrough: true if rescue_stmt
|
653
448
|
return_type = statements.map { simulate_evaluate _1, rescue_scope || scope }.last
|
654
|
-
rescue_scope&.merge_jumps
|
655
449
|
if rescue_stmt
|
656
|
-
|
450
|
+
rescue_scope.merge_jumps
|
451
|
+
scope.update rescue_scope
|
452
|
+
return_type = KatakataIrb::Types::UnionType[return_type, scope.conditional { simulate_evaluate rescue_stmt, _1 }]
|
657
453
|
end
|
658
454
|
simulate_evaluate ensure_stmt, scope if ensure_stmt
|
659
455
|
return_type
|
660
456
|
in [:rescue, error_class_stmts, error_var_stmt, statements, rescue_stmt]
|
661
|
-
return_type = scope.conditional do
|
457
|
+
return_type = scope.conditional do |s|
|
662
458
|
if error_var_stmt in [:var_field, [:@ident, error_var,]]
|
663
459
|
if (error_class_stmts in [:mrhs_new_from_args, Array => stmts, stmt])
|
664
460
|
error_class_stmts = [*stmts, stmt]
|
665
461
|
end
|
666
|
-
error_classes = (error_class_stmts || []).flat_map { simulate_evaluate _1,
|
667
|
-
error_types = error_classes.filter_map { KatakataIrb::Types::InstanceType.new _1.module_or_class if _1
|
462
|
+
error_classes = (error_class_stmts || []).flat_map { simulate_evaluate _1, s }.uniq
|
463
|
+
error_types = error_classes.filter_map { KatakataIrb::Types::InstanceType.new _1.module_or_class if _1.is_a?(KatakataIrb::Types::SingletonType) }
|
668
464
|
error_types << KatakataIrb::Types::InstanceType.new(StandardError) if error_types.empty?
|
669
|
-
|
465
|
+
s[error_var] = KatakataIrb::Types::UnionType[*error_types]
|
670
466
|
end
|
671
|
-
statements.map { simulate_evaluate _1,
|
467
|
+
statements.map { simulate_evaluate _1, s }.last
|
672
468
|
end
|
673
469
|
if rescue_stmt
|
674
|
-
return_type = KatakataIrb::Types::UnionType[return_type, scope.conditional { simulate_evaluate rescue_stmt,
|
470
|
+
return_type = KatakataIrb::Types::UnionType[return_type, scope.conditional { simulate_evaluate rescue_stmt, _1 }]
|
675
471
|
end
|
676
472
|
return_type
|
677
473
|
in [:rescue_mod, statement1, statement2]
|
678
|
-
rescue_scope = Scope.new scope, { RAISE_BREAK => nil }, passthrough: true
|
474
|
+
rescue_scope = KatakataIrb::Scope.new scope, { KatakataIrb::Scope::RAISE_BREAK => nil }, passthrough: true
|
679
475
|
a = simulate_evaluate statement1, rescue_scope
|
680
476
|
rescue_scope.merge_jumps
|
681
|
-
|
477
|
+
scope.update rescue_scope
|
478
|
+
b = scope.conditional { simulate_evaluate statement2, _1 }
|
682
479
|
KatakataIrb::Types::UnionType[a, b]
|
683
480
|
in [:module, module_stmt, body_stmt]
|
684
481
|
module_types = simulate_evaluate(module_stmt, scope).types.grep(KatakataIrb::Types::SingletonType)
|
685
482
|
module_types << KatakataIrb::Types::MODULE if module_types.empty?
|
686
|
-
|
483
|
+
module_scope = KatakataIrb::Scope.new(scope, { KatakataIrb::Scope::SELF => KatakataIrb::Types::UnionType[*module_types], KatakataIrb::Scope::BREAK_RESULT => nil, KatakataIrb::Scope::NEXT_RESULT => nil, KatakataIrb::Scope::RETURN_RESULT => nil }, trace_cvar: false, trace_ivar: false, trace_lvar: false)
|
484
|
+
result = simulate_evaluate body_stmt, module_scope
|
485
|
+
scope.update module_scope
|
486
|
+
result
|
687
487
|
in [:sclass, klass_stmt, body_stmt]
|
688
488
|
klass_types = simulate_evaluate(klass_stmt, scope).types.filter_map do |type|
|
689
|
-
KatakataIrb::Types::SingletonType.new type.klass if type
|
489
|
+
KatakataIrb::Types::SingletonType.new type.klass if type.is_a? KatakataIrb::Types::InstanceType
|
690
490
|
end
|
691
491
|
klass_types = [KatakataIrb::Types::CLASS] if klass_types.empty?
|
692
|
-
|
492
|
+
sclass_scope = KatakataIrb::Scope.new(scope, { KatakataIrb::Scope::SELF => KatakataIrb::Types::UnionType[*klass_types], KatakataIrb::Scope::BREAK_RESULT => nil, KatakataIrb::Scope::NEXT_RESULT => nil, KatakataIrb::Scope::RETURN_RESULT => nil }, trace_cvar: false, trace_ivar: false, trace_lvar: false)
|
493
|
+
result = simulate_evaluate body_stmt, sclass_scope
|
494
|
+
scope.update sclass_scope
|
495
|
+
result
|
693
496
|
in [:class, klass_stmt, superclass_stmt, body_stmt]
|
694
497
|
klass_types = simulate_evaluate(klass_stmt, scope).types
|
695
498
|
klass_types += simulate_evaluate(superclass_stmt, scope).types if superclass_stmt
|
696
499
|
klass_types = klass_types.select do |type|
|
697
|
-
(
|
500
|
+
type.is_a?(KatakataIrb::Types::SingletonType) && type.module_or_class.is_a?(Class)
|
698
501
|
end
|
699
502
|
klass_types << KatakataIrb::Types::CLASS if klass_types.empty?
|
700
|
-
|
503
|
+
klass_scope = KatakataIrb::Scope.new(scope, { KatakataIrb::Scope::SELF => KatakataIrb::Types::UnionType[*klass_types], KatakataIrb::Scope::BREAK_RESULT => nil, KatakataIrb::Scope::NEXT_RESULT => nil, KatakataIrb::Scope::RETURN_RESULT => nil }, trace_cvar: false, trace_ivar: false, trace_lvar: false)
|
504
|
+
result = simulate_evaluate body_stmt, klass_scope
|
505
|
+
scope.update klass_scope
|
506
|
+
result
|
701
507
|
in [:for, fields, enum, statements]
|
702
508
|
fields = [fields] if fields in [:var_field | :field | :aref_field,]
|
703
509
|
params = [:params, fields, nil, nil, nil, nil, nil, nil]
|
@@ -705,47 +511,49 @@ class KatakataIrb::TypeSimulator
|
|
705
511
|
extract_param_names(params).each { scope[_1] = KatakataIrb::Types::NIL }
|
706
512
|
response = simulate_call enum, :first, [], nil, nil
|
707
513
|
evaluate_assign_params params, [response], scope
|
708
|
-
inner_scope = Scope.new scope, { BREAK_RESULT => nil }, passthrough: true
|
709
|
-
scope.conditional do
|
710
|
-
statements.each { simulate_evaluate _1,
|
514
|
+
inner_scope = KatakataIrb::Scope.new scope, { KatakataIrb::Scope::BREAK_RESULT => nil }, passthrough: true
|
515
|
+
scope.conditional do |s|
|
516
|
+
statements.each { simulate_evaluate _1, s }
|
711
517
|
end
|
712
518
|
inner_scope.merge_jumps
|
713
|
-
|
519
|
+
scope.update inner_scope
|
520
|
+
breaks = inner_scope[KatakataIrb::Scope::BREAK_RESULT]
|
714
521
|
breaks ? KatakataIrb::Types::UnionType[breaks, enum] : enum
|
715
522
|
in [:when, pattern, if_statements, else_statement]
|
716
|
-
eval_pattern = lambda do |pattern, *rest|
|
717
|
-
simulate_evaluate pattern,
|
718
|
-
scope.conditional { eval_pattern.call(*rest) } if rest.any?
|
523
|
+
eval_pattern = lambda do |s, pattern, *rest|
|
524
|
+
simulate_evaluate pattern, s
|
525
|
+
scope.conditional { eval_pattern.call(_1, *rest) } if rest.any?
|
719
526
|
end
|
720
|
-
if_branch = lambda do
|
721
|
-
eval_pattern.call(*pattern)
|
722
|
-
if_statements.map { simulate_evaluate _1,
|
527
|
+
if_branch = lambda do |s|
|
528
|
+
eval_pattern.call(s, *pattern)
|
529
|
+
if_statements.map { simulate_evaluate _1, s }.last
|
723
530
|
end
|
724
|
-
else_branch = lambda do
|
725
|
-
pattern.each { simulate_evaluate _1,
|
726
|
-
simulate_evaluate(else_statement,
|
531
|
+
else_branch = lambda do |s|
|
532
|
+
pattern.each { simulate_evaluate _1, s }
|
533
|
+
simulate_evaluate(else_statement, s, case_target: case_target)
|
727
534
|
end
|
728
535
|
if if_statements && else_statement
|
729
536
|
KatakataIrb::Types::UnionType[*scope.run_branches(if_branch, else_branch)]
|
730
537
|
else
|
731
|
-
KatakataIrb::Types::UnionType[scope.conditional { (if_branch || else_branch).call }, KatakataIrb::Types::NIL]
|
538
|
+
KatakataIrb::Types::UnionType[scope.conditional { (if_branch || else_branch).call _1 }, KatakataIrb::Types::NIL]
|
732
539
|
end
|
733
540
|
in [:in, [:var_field, [:@ident, name,]], if_statements, else_statement]
|
734
541
|
scope.never { simulate_evaluate else_statement, scope } if else_statement
|
735
542
|
scope[name] = case_target || KatakataIrb::Types::OBJECT
|
736
543
|
if_statements ? if_statements.map { simulate_evaluate _1, scope }.last : KatakataIrb::Types::NIL
|
737
544
|
in [:in, pattern, if_statements, else_statement]
|
738
|
-
pattern_scope = Scope.new(scope, { PATTERNMATCH_BREAK => nil }, passthrough: true)
|
739
|
-
results =
|
740
|
-
-> {
|
741
|
-
match_pattern case_target, pattern,
|
742
|
-
if_statements ? if_statements.map { simulate_evaluate _1,
|
545
|
+
pattern_scope = KatakataIrb::Scope.new(scope, { KatakataIrb::Scope::PATTERNMATCH_BREAK => nil }, passthrough: true)
|
546
|
+
results = pattern_scope.run_branches(
|
547
|
+
->(s) {
|
548
|
+
match_pattern case_target, pattern, s
|
549
|
+
if_statements ? if_statements.map { simulate_evaluate _1, s }.last : KatakataIrb::Types::NIL
|
743
550
|
},
|
744
|
-
-> {
|
745
|
-
|
746
|
-
else_statement ? simulate_evaluate(else_statement, scope, case_target: case_target) : KatakataIrb::Types::NIL
|
551
|
+
->(s) {
|
552
|
+
else_statement ? simulate_evaluate(else_statement, s, case_target: case_target) : KatakataIrb::Types::NIL
|
747
553
|
}
|
748
554
|
)
|
555
|
+
pattern_scope.merge_jumps
|
556
|
+
scope.update pattern_scope
|
749
557
|
KatakataIrb::Types::UnionType[*results]
|
750
558
|
in [:case, target_exp, match_exp]
|
751
559
|
target = simulate_evaluate target_exp, scope
|
@@ -753,11 +561,19 @@ class KatakataIrb::TypeSimulator
|
|
753
561
|
in [:void_stmt]
|
754
562
|
KatakataIrb::Types::NIL
|
755
563
|
in [:dot2 | :dot3, range_beg, range_end]
|
756
|
-
simulate_evaluate range_beg, scope if range_beg
|
757
|
-
simulate_evaluate range_end, scope if range_end
|
758
|
-
KatakataIrb::Types::
|
564
|
+
beg_type = simulate_evaluate range_beg, scope if range_beg
|
565
|
+
end_type = simulate_evaluate range_end, scope if range_end
|
566
|
+
elem = (KatakataIrb::Types::UnionType[*[beg_type, end_type].compact]).nonnillable
|
567
|
+
KatakataIrb::Types::InstanceType.new Range, { Elem: elem }
|
759
568
|
in [:top_const_ref, [:@const, name,]]
|
760
|
-
|
569
|
+
KatakataIrb::BaseScope.type_of { Object.const_get name }
|
570
|
+
in [:string_concat, a, b]
|
571
|
+
simulate_evaluate a, scope
|
572
|
+
simulate_evaluate b, scope
|
573
|
+
KatakataIrb::Types::STRING
|
574
|
+
in [:defined, expression]
|
575
|
+
scope.conditional { simulate_evaluate expression, _1 }
|
576
|
+
KatakataIrb::Types::UnionType[KatakataIrb::Types::STRING, KatakataIrb::Types::NIL]
|
761
577
|
else
|
762
578
|
KatakataIrb.log_puts
|
763
579
|
KatakataIrb.log_puts :NOMATCH
|
@@ -767,7 +583,7 @@ class KatakataIrb::TypeSimulator
|
|
767
583
|
end
|
768
584
|
|
769
585
|
def match_pattern(target, pattern, scope)
|
770
|
-
breakable = -> { scope.
|
586
|
+
breakable = -> { scope.terminate_with KatakataIrb::Scope::PATTERNMATCH_BREAK, KatakataIrb::Types::NIL }
|
771
587
|
types = target.types
|
772
588
|
case pattern
|
773
589
|
in [:var_field, [:@ident, name,]]
|
@@ -779,12 +595,12 @@ class KatakataIrb::TypeSimulator
|
|
779
595
|
breakable.call
|
780
596
|
in [:binary, lpattern, :|, rpattern]
|
781
597
|
match_pattern target, lpattern, scope
|
782
|
-
scope.conditional { match_pattern target, rpattern,
|
598
|
+
scope.conditional { match_pattern target, rpattern, _1 }
|
783
599
|
breakable.call
|
784
600
|
in [:binary, lpattern, :'=>', [:var_field, [:@ident, name,]] => rpattern]
|
785
601
|
if lpattern in [:var_ref, [:@const, _const_name,]]
|
786
602
|
const_value = simulate_evaluate lpattern, scope
|
787
|
-
if (
|
603
|
+
if const_value.is_a?(KatakataIrb::Types::SingletonType) && const_value.module_or_class.is_a?(Class)
|
788
604
|
scope[name] = KatakataIrb::Types::InstanceType.new const_value.module_or_class
|
789
605
|
else
|
790
606
|
scope[name] = KatakataIrb::Types::OBJECT
|
@@ -796,7 +612,7 @@ class KatakataIrb::TypeSimulator
|
|
796
612
|
end
|
797
613
|
in [:aryptn, _unknown, items, splat, post_items]
|
798
614
|
# TODO: deconstruct keys
|
799
|
-
array_types = types.select { (
|
615
|
+
array_types = types.select { _1.is_a?(KatakataIrb::Types::InstanceType) && _1.klass == Array }
|
800
616
|
elem = KatakataIrb::Types::UnionType[*array_types.filter_map { _1.params[:Elem] }]
|
801
617
|
items&.each do |item|
|
802
618
|
match_pattern elem, item, scope
|
@@ -810,7 +626,7 @@ class KatakataIrb::TypeSimulator
|
|
810
626
|
end
|
811
627
|
in [:hshptn, _unknown, items, splat]
|
812
628
|
# TODO: deconstruct keys
|
813
|
-
hash_types = types.select { (
|
629
|
+
hash_types = types.select { _1.is_a?(KatakataIrb::Types::InstanceType) && _1.klass == Hash }
|
814
630
|
key_type = KatakataIrb::Types::UnionType[*hash_types.filter_map { _1.params[:K] }]
|
815
631
|
value_type = KatakataIrb::Types::UnionType[*hash_types.filter_map { _1.params[:V] }]
|
816
632
|
items&.each do |key_pattern, value_pattern|
|
@@ -839,7 +655,7 @@ class KatakataIrb::TypeSimulator
|
|
839
655
|
def evaluate_mrhs(sexp, scope)
|
840
656
|
args, kwargs, = retrieve_method_args sexp
|
841
657
|
values = args.filter_map do |t|
|
842
|
-
if t
|
658
|
+
if t.is_a? KatakataIrb::Types::Splat
|
843
659
|
simulate_evaluate t.item, scope
|
844
660
|
# TODO
|
845
661
|
nil
|
@@ -866,17 +682,31 @@ class KatakataIrb::TypeSimulator
|
|
866
682
|
[values, kw]
|
867
683
|
end
|
868
684
|
|
869
|
-
def
|
870
|
-
|
871
|
-
|
872
|
-
|
685
|
+
def sized_splat(value, method, size)
|
686
|
+
array_elem, non_array = partition_to_array value, method
|
687
|
+
values = [KatakataIrb::Types::UnionType[*array_elem, *non_array]]
|
688
|
+
values += [array_elem] * (size - 1) if array_elem && size >= 1
|
689
|
+
values
|
873
690
|
end
|
874
691
|
|
875
|
-
def
|
876
|
-
|
877
|
-
|
878
|
-
|
692
|
+
def partition_to_array(value, method)
|
693
|
+
arrays, non_arrays = value.types.partition { _1.is_a?(KatakataIrb::Types::InstanceType) && _1.klass == Array }
|
694
|
+
non_arrays.select! do |type|
|
695
|
+
to_array_result = simulate_call type, method, [], nil, nil, name_match: false
|
696
|
+
if to_array_result.is_a?(KatakataIrb::Types::InstanceType) && to_array_result.klass == Array
|
697
|
+
arrays << to_array_result
|
698
|
+
false
|
699
|
+
else
|
700
|
+
true
|
701
|
+
end
|
879
702
|
end
|
703
|
+
array_elem = arrays.empty? ? nil : KatakataIrb::Types::UnionType[*arrays.map { _1.params[:Elem] || KatakataIrb::Types::OBJECT }]
|
704
|
+
non_array = non_arrays.empty? ? nil : KatakataIrb::Types::UnionType[*non_arrays]
|
705
|
+
[array_elem, non_array]
|
706
|
+
end
|
707
|
+
|
708
|
+
def evaluate_massign(sexp, values, scope)
|
709
|
+
values = sized_splat values, :to_ary, sexp.size unless values.is_a? Array
|
880
710
|
|
881
711
|
rest_index = sexp.find_index { _1 in [:rest_param, ]}
|
882
712
|
if rest_index
|
@@ -894,7 +724,7 @@ class KatakataIrb::TypeSimulator
|
|
894
724
|
in [:@ident, name,]
|
895
725
|
# block arg mlhs
|
896
726
|
scope[name] = value || KatakataIrb::Types::OBJECT
|
897
|
-
in [:var_field, [:@
|
727
|
+
in [:var_field, [:@gvar | :@ivar | :@cvar | :@ident | :@const, name,]]
|
898
728
|
# massign
|
899
729
|
scope[name] = value || KatakataIrb::Types::OBJECT
|
900
730
|
in [:mlhs, *mlhs]
|
@@ -916,12 +746,12 @@ class KatakataIrb::TypeSimulator
|
|
916
746
|
keys = []
|
917
747
|
values = []
|
918
748
|
kwargs.each do |kv|
|
919
|
-
if kv
|
749
|
+
if kv.is_a? KatakataIrb::Types::Splat
|
920
750
|
hash = simulate_evaluate kv.item, scope
|
921
|
-
unless (
|
751
|
+
unless hash.is_a?(KatakataIrb::Types::InstanceType) && hash.klass == Hash
|
922
752
|
hash = simulate_call hash, :to_hash, [], nil, nil
|
923
753
|
end
|
924
|
-
if (
|
754
|
+
if hash.is_a?(KatakataIrb::Types::InstanceType) && hash.klass == Hash
|
925
755
|
keys << hash.params[:K] if hash.params[:K]
|
926
756
|
values << hash.params[:V] if hash.params[:V]
|
927
757
|
end
|
@@ -934,37 +764,29 @@ class KatakataIrb::TypeSimulator
|
|
934
764
|
KatakataIrb::Types::InstanceType.new(Hash, K: KatakataIrb::Types::UnionType[*keys], V: KatakataIrb::Types::UnionType[*values])
|
935
765
|
end
|
936
766
|
|
937
|
-
def self.type_of(fallback: KatakataIrb::Types::OBJECT)
|
938
|
-
begin
|
939
|
-
KatakataIrb::Types.type_from_object yield
|
940
|
-
rescue
|
941
|
-
fallback
|
942
|
-
end
|
943
|
-
end
|
944
|
-
|
945
767
|
def retrieve_method_call(sexp)
|
946
|
-
|
768
|
+
optional = -> { _1 in [:@op, '&.',] }
|
947
769
|
case sexp
|
948
770
|
in [:fcall | :vcall, [:@ident | :@const | :@kw | :@op, method,]] # hoge
|
949
771
|
[nil, method, [], [], nil, false]
|
950
772
|
in [:call, receiver, [:@period,] | [:@op, '&.',] | :'::' => dot, :call]
|
951
|
-
[receiver, :call, [], [], nil,
|
773
|
+
[receiver, :call, [], [], nil, optional[dot]]
|
952
774
|
in [:call, receiver, [:@period,] | [:@op, '&.',] | :'::' => dot, method]
|
953
775
|
method => [:@ident | :@const | :@kw | :@op, method,] unless method == :call
|
954
|
-
[receiver, method, [], [], nil,
|
776
|
+
[receiver, method, [], [], nil, optional[dot]]
|
955
777
|
in [:command, [:@ident | :@const | :@kw | :@op, method,], args] # hoge 1, 2
|
956
778
|
args, kwargs, block = retrieve_method_args args
|
957
779
|
[nil, method, args, kwargs, block, false]
|
958
780
|
in [:command_call, receiver, [:@period,] | [:@op, '&.',] | :'::' => dot, [:@ident | :@const | :@kw | :@op, method,], args] # a.hoge 1; a.hoge 1, 2;
|
959
781
|
args, kwargs, block = retrieve_method_args args
|
960
|
-
[receiver, method, args, kwargs, block,
|
782
|
+
[receiver, method, args, kwargs, block, optional[dot]]
|
961
783
|
in [:method_add_arg, call, args]
|
962
|
-
receiver, method, _arg, _kwarg, _block,
|
784
|
+
receiver, method, _arg, _kwarg, _block, opt = retrieve_method_call call
|
963
785
|
args, kwargs, block = retrieve_method_args args
|
964
|
-
[receiver, method, args, kwargs, block,
|
786
|
+
[receiver, method, args, kwargs, block, opt]
|
965
787
|
in [:method_add_block, call, block]
|
966
|
-
receiver, method, args, kwargs,
|
967
|
-
[receiver, method, args, kwargs, block,
|
788
|
+
receiver, method, args, kwargs, opt = retrieve_method_call call
|
789
|
+
[receiver, method, args, kwargs, block, opt]
|
968
790
|
end
|
969
791
|
end
|
970
792
|
|
@@ -1017,6 +839,8 @@ class KatakataIrb::TypeSimulator
|
|
1017
839
|
[args, [], nil]
|
1018
840
|
in [:arg_paren, args]
|
1019
841
|
args ? retrieve_method_args(args) : [[], [], nil]
|
842
|
+
in [[:command | :command_call, ] => command_arg] # method(a b, c), method(a.b c, d)
|
843
|
+
[[command_arg], [], nil]
|
1020
844
|
else
|
1021
845
|
[[], [], nil]
|
1022
846
|
end
|
@@ -1033,13 +857,14 @@ class KatakataIrb::TypeSimulator
|
|
1033
857
|
params_type = method.block.type.required_positionals.map do |func_param|
|
1034
858
|
KatakataIrb::Types.from_rbs_type func_param.type, receiver, vars
|
1035
859
|
end
|
1036
|
-
|
860
|
+
self_type = KatakataIrb::Types.from_rbs_type method.block.self_type, receiver, vars if method.block.self_type
|
861
|
+
block_response, breaks = block.call params_type, self_type
|
1037
862
|
block_called = true
|
1038
863
|
vars.merge! KatakataIrb::Types.match_free_variables(free_vars - vars.keys.to_set, [method.block.type.return_type], [block_response])
|
1039
864
|
end
|
1040
865
|
[KatakataIrb::Types.from_rbs_type(method.type.return_type, receiver, vars || {}), breaks]
|
1041
866
|
end
|
1042
|
-
block&.call [] unless block_called
|
867
|
+
block&.call [], nil unless block_called
|
1043
868
|
types = type_breaks.map(&:first)
|
1044
869
|
breaks = type_breaks.map(&:last).compact
|
1045
870
|
types << OBJECT_METHODS[method_name.to_sym] if name_match && OBJECT_METHODS.has_key?(method_name.to_sym)
|
@@ -1095,11 +920,7 @@ class KatakataIrb::TypeSimulator
|
|
1095
920
|
values = values.dup
|
1096
921
|
params => [:params, pre_required, optional, rest, post_required, _keywords, keyrest, block]
|
1097
922
|
size = (pre_required&.size || 0) + (optional&.size || 0) + (post_required&.size || 0) + (rest ? 1 : 0)
|
1098
|
-
if values.size == 1 && size >= 2
|
1099
|
-
value = values.first
|
1100
|
-
array_value = to_array value, :to_ary if value
|
1101
|
-
values = [array_value.params[:Elem] || KatakataIrb::Types::OBJECT] * size if array_value
|
1102
|
-
end
|
923
|
+
values = sized_splat values.first, :to_ary, size if values.size == 1 && size >= 2
|
1103
924
|
pre_values = values.shift pre_required.size if pre_required
|
1104
925
|
post_values = values.pop post_required.size if post_required
|
1105
926
|
opt_values = values.shift optional.size if optional
|
@@ -1149,7 +970,7 @@ class KatakataIrb::TypeSimulator
|
|
1149
970
|
name.match?(/\A_[1-9]\z/) ? name[1..].to_i : 0
|
1150
971
|
else
|
1151
972
|
sexp.filter_map do |s|
|
1152
|
-
max_numbered_params s if s
|
973
|
+
max_numbered_params s if s.is_a? Array
|
1153
974
|
end.max || 0
|
1154
975
|
end
|
1155
976
|
end
|
@@ -1158,7 +979,7 @@ class KatakataIrb::TypeSimulator
|
|
1158
979
|
dig_targets = DigTarget.new(parents, target) do |_types, scope|
|
1159
980
|
return scope
|
1160
981
|
end
|
1161
|
-
scope = Scope.from_binding(binding)
|
982
|
+
scope = KatakataIrb::Scope.from_binding(binding)
|
1162
983
|
new(dig_targets).simulate_evaluate parents[0], scope
|
1163
984
|
scope
|
1164
985
|
end
|
@@ -1167,7 +988,7 @@ class KatakataIrb::TypeSimulator
|
|
1167
988
|
dig_targets = DigTarget.new(parents, receiver) do |type, _scope|
|
1168
989
|
return type
|
1169
990
|
end
|
1170
|
-
new(dig_targets).simulate_evaluate parents[0], Scope.from_binding(binding)
|
991
|
+
new(dig_targets).simulate_evaluate parents[0], KatakataIrb::Scope.from_binding(binding)
|
1171
992
|
KatakataIrb::Types::NIL
|
1172
993
|
end
|
1173
994
|
end
|