Skip to content

Commit

Permalink
Support multi_write_node: /(?<a>x)/ =~ "x"; a
Browse files Browse the repository at this point in the history
  • Loading branch information
mame committed Aug 22, 2024
1 parent 9534dca commit b386bd0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/typeprof/core/ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def self.create_node(raw_node, lenv, use_result = true)
rhs = AndNode.new(raw_node, read, raw_node.value, lenv)
CallWriteNode.new(raw_node, rhs, lenv)
when :multi_write_node then MultiWriteNode.new(raw_node, lenv)
when :match_write_node then MatchWriteNode.new(raw_node, lenv)

# value
when :self_node then SelfNode.new(raw_node, lenv)
Expand Down
23 changes: 23 additions & 0 deletions lib/typeprof/core/ast/misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ def retrieve_at(pos, &blk)
end
end

class MatchWriteNode < Node
def initialize(raw_node, lenv)
super(raw_node, lenv)
@call = AST.create_node(raw_node.call, lenv)
@targets = raw_node.targets.map do |raw_lhs|
AST.create_target_node(raw_lhs, lenv)
end
end

attr_reader :call, :targets
def subnodes = { call:, targets: }

def install0(genv)
ret = @call.install(genv)
@targets.each do |target|
target.install(genv)
target.rhs.ret || raise(target.rhs.inspect)
@changes.add_edge(genv, Source.new(Type::Instance.new(genv, genv.mod_str, [])), target.rhs.ret)
end
ret
end
end

class DefinedNode < Node
def initialize(raw_node, lenv)
super(raw_node, lenv)
Expand Down
3 changes: 2 additions & 1 deletion lib/typeprof/core/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def initialize
@mod_ary = resolve_cpath([:Array])
@mod_hash = resolve_cpath([:Hash])
@mod_range = resolve_cpath([:Range])
@mod_str = resolve_cpath([:String])

@cls_type = Type::Instance.new(self, @mod_class, [])
@mod_type = Type::Instance.new(self, @mod_module, [])
Expand All @@ -40,7 +41,7 @@ def initialize

attr_reader :type_table

attr_reader :mod_object, :mod_ary, :mod_hash, :mod_range
attr_reader :mod_object, :mod_ary, :mod_hash, :mod_range, :mod_str
attr_reader :cls_type, :mod_type
attr_reader :obj_type, :nil_type, :true_type, :false_type, :str_type, :int_type, :float_type
attr_reader :proc_type, :symbol_type, :set_type, :regexp_type, :complex_type
Expand Down
13 changes: 13 additions & 0 deletions scenario/misc/match_asgn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## update
def check
if /(?<a>foo)/ =~ "foo"
a
else
1
end
end

## assert
class Object
def check: -> (Integer | String)
end

0 comments on commit b386bd0

Please sign in to comment.