Skip to content

Commit 93e5f35

Browse files
committed
Support arguments for break
1 parent cb793b8 commit 93e5f35

6 files changed

Lines changed: 35 additions & 7 deletions

File tree

doc/doc.ja.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,4 @@ Procオブジェクトは、ラムダ式(`-> { ... }`)やブロック仮引
131131
これらは抽象化されず、コード片と結びついた具体的な値として扱われます。
132132
これらに渡された引数や返された値によってRBS出力されます。
133133

134-
TBD
134+
TODO: write more

lib/typeprof/core/ast/call.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,15 @@ def install0(genv)
124124

125125
a_args = ActualArguments.new(positional_args, @splat_flags, keyword_args, blk_ty)
126126
box = @changes.add_method_call_box(genv, recv, @mid, a_args, !@recv)
127-
box.ret
127+
128+
if @block_body && @block_body.lenv.break_vtx
129+
ret = Vertex.new(self)
130+
@changes.add_edge(genv, box.ret, ret)
131+
@changes.add_edge(genv, @block_body.lenv.break_vtx, ret)
132+
ret
133+
else
134+
box.ret
135+
end
128136
end
129137

130138
def block_last_stmt_code_range

lib/typeprof/core/ast/control.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,17 @@ class UntilNode < LoopNode
176176
class BreakNode < Node
177177
def initialize(raw_node, lenv)
178178
super(raw_node, lenv)
179-
@arg = raw_node.arguments ? AST.create_node(raw_node.arguments.arguments.first, lenv) : nil
179+
@arg = AST.parse_return_arguments(raw_node, lenv, code_range)
180180
end
181181

182182
attr_reader :arg
183183

184184
def subnodes = { arg: }
185185

186186
def install0(genv)
187-
_arg = @arg ? @arg.install(genv) : Source.new(genv.nil_type)
188-
# TODO: implement!
187+
arg = @arg.install(genv)
188+
@changes.add_edge(genv, arg, @lenv.get_break_vtx)
189+
Source.new()
189190
end
190191
end
191192

lib/typeprof/core/env.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,12 @@ def initialize(path, cref, locals, return_boxes)
282282
@cref = cref
283283
@locals = locals
284284
@return_boxes = return_boxes
285+
@break_vtx = nil
285286
@next_boxes = []
286287
@filters = {}
287288
end
288289

289-
attr_reader :path, :cref, :locals, :return_boxes, :next_boxes
290+
attr_reader :path, :cref, :locals, :return_boxes, :break_vtx, :next_boxes
290291

291292
def new_var(name, node)
292293
@locals[name] = Vertex.new(node)
@@ -312,6 +313,11 @@ def add_next_box(box)
312313
@next_boxes << box
313314
end
314315

316+
def get_break_vtx
317+
@break_vtx ||= Vertex.new(:break_vtx)
318+
end
319+
320+
315321
def push_read_filter(name, type)
316322
(@filters[name] ||= []) << type
317323
end

lib/typeprof/core/graph/vertex.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def initialize(origin)
146146
when ValueEntity
147147
when ActualArguments
148148
when Array
149+
when Symbol
149150
else
150151
raise "unknown class: #{ origin.class }"
151152
end

scenario/control/break.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,17 @@ def bar
1616
## assert
1717
class Object
1818
def foo: { (Integer) -> Float } -> String
19-
def bar: -> String
19+
def bar: -> String?
20+
end
21+
22+
## update: test.rb
23+
def foo
24+
loop do
25+
break 1
26+
end
27+
end
28+
29+
## assert
30+
class Object
31+
def foo: -> Integer
2032
end

0 commit comments

Comments
 (0)