Class: Steep::TypeInference::CaseWhen::WhenPatterns
- Includes:
- NodeHelper
- Defined in:
- lib/steep/type_inference/case_when.rb
Instance Attribute Summary collapse
-
#initial_constr ⇒ Object
readonly
Returns the value of attribute initial_constr.
-
#logic ⇒ Object
readonly
Returns the value of attribute logic.
-
#pattern_results ⇒ Object
readonly
Returns the value of attribute pattern_results.
-
#unreachable_clause ⇒ Object
readonly
Returns the value of attribute unreachable_clause.
Instance Method Summary collapse
- #add_pattern(pat) ⇒ Object
- #assignment_node ⇒ Object
- #body_result ⇒ Object
- #falsy_result ⇒ Object
-
#initialize(logic, initial_constr, unreachable_clause, assignment_node) ⇒ WhenPatterns
constructor
A new instance of WhenPatterns.
- #latest_result ⇒ Object
Methods included from NodeHelper
#clone_node, #deconstruct_case_node, #deconstruct_case_node!, #deconstruct_if_node, #deconstruct_if_node!, #deconstruct_resbody_node, #deconstruct_resbody_node!, #deconstruct_rescue_node, #deconstruct_rescue_node!, #deconstruct_send_node, #deconstruct_send_node!, #deconstruct_sendish_and_block_nodes, #deconstruct_when_node, #deconstruct_when_node!, #deconstruct_whileish_node, #deconstruct_whileish_node!, #each_child_node, #each_descendant_node, #private_send?, #test_case_node, #test_if_node, #test_resbody_node, #test_rescue_node, #test_send_node, #test_when_node, #test_whileish_node, #value_node?
Constructor Details
#initialize(logic, initial_constr, unreachable_clause, assignment_node) ⇒ WhenPatterns
Returns a new instance of WhenPatterns.
9 10 11 12 13 14 15 16 |
# File 'lib/steep/type_inference/case_when.rb', line 9 def initialize(logic, initial_constr, unreachable_clause, assignment_node) @logic = logic @initial_constr = initial_constr @unreachable_clause = unreachable_clause @assignment_node = assignment_node @pattern_results = [] end |
Instance Attribute Details
#initial_constr ⇒ Object (readonly)
Returns the value of attribute initial_constr.
7 8 9 |
# File 'lib/steep/type_inference/case_when.rb', line 7 def initial_constr @initial_constr end |
#logic ⇒ Object (readonly)
Returns the value of attribute logic.
7 8 9 |
# File 'lib/steep/type_inference/case_when.rb', line 7 def logic @logic end |
#pattern_results ⇒ Object (readonly)
Returns the value of attribute pattern_results.
7 8 9 |
# File 'lib/steep/type_inference/case_when.rb', line 7 def pattern_results @pattern_results end |
#unreachable_clause ⇒ Object (readonly)
Returns the value of attribute unreachable_clause.
7 8 9 |
# File 'lib/steep/type_inference/case_when.rb', line 7 def unreachable_clause @unreachable_clause end |
Instance Method Details
#add_pattern(pat) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/steep/type_inference/case_when.rb', line 18 def add_pattern(pat) test_node = pat.updated(:send, [pat, :===, assignment_node]) latest_constr, unreachable_pattern = latest_result type, constr = yield(test_node, latest_constr, unreachable_pattern) truthy_result, falsy_result = logic.eval(env: latest_constr.context.type_env, node: test_node) pattern_results << [pat, truthy_result, falsy_result] end |
#assignment_node ⇒ Object
63 64 65 |
# File 'lib/steep/type_inference/case_when.rb', line 63 def assignment_node() clone_node(@assignment_node) end |
#body_result ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/steep/type_inference/case_when.rb', line 40 def body_result raise if pattern_results.empty? type_envs = pattern_results.map {|_, truthy, _| truthy.env } env = initial_constr.context.type_env.join(*type_envs) env = yield(env) || env [ initial_constr.update_type_env { env }, unreachable_clause || pattern_results.all? {|_, truthy, _| truthy.unreachable } ] end |
#falsy_result ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/steep/type_inference/case_when.rb', line 54 def falsy_result (_, _, falsy = pattern_results.last) or raise [ initial_constr.update_type_env { falsy.env }, unreachable_clause || falsy.unreachable ] end |
#latest_result ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/steep/type_inference/case_when.rb', line 29 def latest_result if (_, truthy, falsy = pattern_results.last) [ initial_constr.update_type_env { falsy.env }, falsy.unreachable ] else [initial_constr, unreachable_clause] end end |