Class: Steep::TypeInference::Logic

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/type_inference/logic.rb

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subtyping:) ⇒ Logic

Returns a new instance of Logic.



35
36
37
# File 'lib/steep/type_inference/logic.rb', line 35

def initialize(subtyping:)
  @subtyping = subtyping
end

Instance Attribute Details

#subtypingObject (readonly)

Returns the value of attribute subtyping.



33
34
35
# File 'lib/steep/type_inference/logic.rb', line 33

def subtyping
  @subtyping
end

Instance Method Details

#environments(truthy_vars:, falsey_vars:, lvar_env:) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/steep/type_inference/logic.rb', line 83

def environments(truthy_vars:, falsey_vars:, lvar_env:)
  truthy_hash = lvar_env.assigned_types.dup
  falsey_hash = lvar_env.assigned_types.dup

  (truthy_vars + falsey_vars).each do |var|
    type = lvar_env[var]
    truthy_type, falsey_type = partition_union(type)

    if truthy_vars.include?(var)
      truthy_hash[var] = LocalVariableTypeEnv::Entry.new(type: truthy_type)
    end

    if falsey_vars.include?(var)
      falsey_hash[var] = LocalVariableTypeEnv::Entry.new(type: falsey_type)
    end
  end

  [
    lvar_env.except(truthy_vars).update(assigned_types: truthy_hash),
    lvar_env.except(falsey_vars).update(assigned_types: falsey_hash)
  ]
end

#nodes(node:) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/steep/type_inference/logic.rb', line 39

def nodes(node:)
  case node.type
  when :lvasgn
    rhs = node.children[1]
    t, f = nodes(node: rhs)

    [
      t.merge([node]),
      f.merge([node])
    ]

  when :and
    lhs, rhs = node.children

    lt, _ = nodes(node: lhs)
    rt, _ = nodes(node: rhs)

    [
      Result.new([node]) + lt + rt,
      Result.new([node])
    ]

  when :or
    lhs, rhs = node.children

    _, lf = nodes(node: lhs)
    _, rf = nodes(node: rhs)

    [
      Result.new([node]),
      Result.new([node]) + lf + rf
    ]

  when :begin
    nodes(node: node.children.last)

  else
    [
      Result.new([node]),
      Result.new([node])
    ]
  end
end

#partition_union(type) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/steep/type_inference/logic.rb', line 106

def partition_union(type)
  case type
  when AST::Types::Union
    falsey_types, truthy_types = type.types.partition do |type|
      case type
      when AST::Types::Nil
        true
      when AST::Types::Literal
        type.value == false
      end
    end

    [
      truthy_types.empty? ? AST::Types::Bot.new : AST::Types::Union.build(types: truthy_types),
      falsey_types.empty? ? AST::Types::Bot.new : AST::Types::Union.build(types: falsey_types)
    ]
  when AST::Types::Any, AST::Types::Top, AST::Types::Boolean, AST::Types::Void
    [type, type]
  else
    [type, AST::Types::Bot.new]
  end
end