Skip to content

Commit

Permalink
Rename TypeSimulator -> TypeAnalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Oct 18, 2023
1 parent b89e273 commit 562802e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/katakata_irb/completor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ def self.analyze(code, binding = Object::TOPLEVEL_BINDING)
*parents, target_node = find_target ast, code.bytesize - name.bytesize
return unless target_node

calculate_scope = -> { KatakataIrb::TypeSimulator.calculate_target_type_scope(binding, parents, target_node).last }
calculate_type_scope = ->(node) { KatakataIrb::TypeSimulator.calculate_target_type_scope binding, [*parents, target_node], node }
calculate_scope = -> { KatakataIrb::TypeAnalyzer.calculate_target_type_scope(binding, parents, target_node).last }
calculate_type_scope = ->(node) { KatakataIrb::TypeAnalyzer.calculate_target_type_scope binding, [*parents, target_node], node }

if target_node.is_a?(Prism::StringNode) || target_node.is_a?(Prism::InterpolatedStringNode)
args_node = parents[-1]
Expand Down
26 changes: 13 additions & 13 deletions lib/katakata_irb/type_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require_relative 'scope'
require 'prism'

class KatakataIrb::TypeSimulator
class KatakataIrb::TypeAnalyzer
class DigTarget
def initialize(parents, receiver, &block)
@dig_ids = parents.to_h { [_1.__id__, true] }
Expand Down Expand Up @@ -174,7 +174,7 @@ def evaluate_hash(node, scope)
when Prism::AssocSplatNode
hash = evaluate assoc.value, scope
unless hash.is_a?(KatakataIrb::Types::InstanceType) && hash.klass == Hash
hash = simulate_call hash, :to_hash, [], nil, nil, scope
hash = method_call hash, :to_hash, [], nil, nil, scope
end
if hash.is_a?(KatakataIrb::Types::InstanceType) && hash.klass == Hash
keys << hash.params[:K] if hash.params[:K]
Expand Down Expand Up @@ -248,7 +248,7 @@ def evaluate_call_node(node, scope)
else
call_block_proc = ->(block_args, _self_type) do
block_receiver, *rest = block_args
block_receiver ? simulate_call(block_receiver || KatakataIrb::Types::OBJECT, block_sym, rest, nil, nil, scope) : KatakataIrb::Types::OBJECT
block_receiver ? method_call(block_receiver || KatakataIrb::Types::OBJECT, block_sym, rest, nil, nil, scope) : KatakataIrb::Types::OBJECT
end
end
elsif node.block.is_a? Prism::BlockNode
Expand Down Expand Up @@ -280,7 +280,7 @@ def evaluate_call_node(node, scope)
elsif has_block
call_block_proc = ->(_block_args, _self_type) { KatakataIrb::Types::OBJECT }
end
simulate_call receiver_type, node.name, args_types, kwargs_types, call_block_proc, scope
method_call receiver_type, node.name, args_types, kwargs_types, call_block_proc, scope
end
if node.call_operator == '&.'
result = scope.conditional { evaluate_method.call _1 }
Expand Down Expand Up @@ -316,13 +316,13 @@ def evaluate_call_write(node, scope, operator)
block_sym = block_sym_node.value
call_block_proc = ->(block_args, _self_type) do
block_receiver, *rest = block_args
block_receiver ? simulate_call(block_receiver || KatakataIrb::Types::OBJECT, block_sym, rest, nil, nil, scope) : KatakataIrb::Types::OBJECT
block_receiver ? method_call(block_receiver || KatakataIrb::Types::OBJECT, block_sym, rest, nil, nil, scope) : KatakataIrb::Types::OBJECT
end
elsif has_block
call_block_proc = ->(_block_args, _self_type) { KatakataIrb::Types::OBJECT }
end
method = node.write_name.to_s.delete_suffix('=')
left = simulate_call receiver_type, method, args_types, kwargs_types, call_block_proc, scope
left = method_call receiver_type, method, args_types, kwargs_types, call_block_proc, scope
case operator
when :and
right = scope.conditional { evaluate node.value, _1 }
Expand All @@ -332,14 +332,14 @@ def evaluate_call_write(node, scope, operator)
KatakataIrb::Types::UnionType[left, right]
else
right = evaluate node.value, scope
simulate_call left, node.operator, [right], nil, nil, scope, name_match: false
method_call left, node.operator, [right], nil, nil, scope, name_match: false
end
end

def evaluate_variable_operator_write(node, scope)
left = scope[node.name.to_s] || KatakataIrb::Types::OBJECT
right = evaluate node.value, scope
scope[node.name.to_s] = simulate_call left, node.operator, [right], nil, nil, scope, name_match: false
scope[node.name.to_s] = method_call left, node.operator, [right], nil, nil, scope, name_match: false
end
alias evaluate_global_variable_operator_write_node evaluate_variable_operator_write
alias evaluate_local_variable_operator_write_node evaluate_variable_operator_write
Expand Down Expand Up @@ -368,7 +368,7 @@ def evaluate_variable_or_write(node, scope)
def evaluate_constant_operator_write_node(node, scope)
left = scope[node.name.to_s] || KatakataIrb::Types::OBJECT
right = evaluate node.value, scope
scope[node.name.to_s] = simulate_call left, node.operator, [right], nil, nil, scope, name_match: false
scope[node.name.to_s] = method_call left, node.operator, [right], nil, nil, scope, name_match: false
end

def evaluate_constant_and_write_node(node, scope)
Expand All @@ -385,7 +385,7 @@ def evaluate_constant_or_write_node(node, scope)
def evaluate_constant_path_operator_write_node(node, scope)
left, receiver, _parent_module, name = evaluate_constant_node_info node.target, scope
right = evaluate node.value, scope
value = simulate_call left, node.operator, [right], nil, nil, scope, name_match: false
value = method_call left, node.operator, [right], nil, nil, scope, name_match: false
const_path_write receiver, name, value, scope
value
end
Expand Down Expand Up @@ -675,7 +675,7 @@ def evaluate_for_node(node, scope)
node.statements
collection = evaluate node.collection, scope
inner_scope = KatakataIrb::Scope.new scope, { KatakataIrb::Scope::BREAK_RESULT => nil }
ary_type = simulate_call collection, :to_ary, [], nil, nil, nil, name_match: false
ary_type = method_call collection, :to_ary, [], nil, nil, nil, name_match: false
element_types = ary_type.types.filter_map do |ary|
ary.params[:Elem] if ary.is_a?(KatakataIrb::Types::InstanceType) && ary.klass == Array
end
Expand Down Expand Up @@ -1089,7 +1089,7 @@ def sized_splat(value, method, size)
def partition_to_array(value, method)
arrays, non_arrays = value.types.partition { _1.is_a?(KatakataIrb::Types::InstanceType) && _1.klass == Array }
non_arrays.select! do |type|
to_array_result = simulate_call type, method, [], nil, nil, nil, name_match: false
to_array_result = method_call type, method, [], nil, nil, nil, name_match: false
if to_array_result.is_a?(KatakataIrb::Types::InstanceType) && to_array_result.klass == Array
arrays << to_array_result
false
Expand All @@ -1102,7 +1102,7 @@ def partition_to_array(value, method)
[array_elem, non_array]
end

def simulate_call(receiver, method_name, args, kwargs, block, scope, name_match: true)
def method_call(receiver, method_name, args, kwargs, block, scope, name_match: true)
methods = KatakataIrb::Types.rbs_methods receiver, method_name.to_sym, args, kwargs, !!block
block_called = false
type_breaks = methods.map do |method, given_params, method_params|
Expand Down
4 changes: 2 additions & 2 deletions test/test_katakata_irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TestKatakataIrb < Minitest::Test
def test_analyze_does_not_raise_error
verbose, $VERBOSE = $VERBOSE, nil
original_output = KatakataIrb.log_output
KatakataIrb::TypeSimulator::DigTarget.class_eval do
KatakataIrb::TypeAnalyzer::DigTarget.class_eval do
alias_method :original_dig?, :dig?
def dig?(node) = !!node
end
Expand All @@ -28,7 +28,7 @@ def output.puts(*)
ensure
$VERBOSE = verbose
KatakataIrb.log_output = original_output
KatakataIrb::TypeSimulator::DigTarget.class_eval do
KatakataIrb::TypeAnalyzer::DigTarget.class_eval do
undef_method :dig?
alias_method :dig?, :original_dig?
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_type_analyze.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def test_vars_with_branch_termination
assert_call('a=1; ->{ if cond; a=:a; break; a=""; end; a=// }; a.', include: [Integer, Symbol, Regexp], exclude: String)
assert_call('a=1; ->{ if cond; a=:a; break; a=""; else; break; end; a=// }; a.', include: [Integer, Symbol], exclude: [String, Regexp])

# continue simulation on terminated branch
# continue evaluation on terminated branch
assert_call('a=1; tap{ a=1.0; break; a=// if cond; a.', include: [Regexp, Float], exclude: Integer)
assert_call('a=1; tap{ a=1.0; next; a=// if cond; a.', include: [Regexp, Float], exclude: Integer)
assert_call('a=1; ->{ a=1.0; break; a=// if cond; a.', include: [Regexp, Float], exclude: Integer)
Expand Down

0 comments on commit 562802e

Please sign in to comment.