Class: TypedRb::Types::TypingContext

Inherits:
Object
  • Object
show all
Defined in:
lib/typed/typing_context.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ TypingContext

Returns a new instance of TypingContext.



168
169
170
171
# File 'lib/typed/typing_context.rb', line 168

def initialize(parent = nil)
  @parent = parent
  @bindings = {}
end

Class Attribute Details

.function_contextObject (readonly)

Returns the value of attribute function_context.



33
34
35
# File 'lib/typed/typing_context.rb', line 33

def function_context
  @function_context
end

Class Method Details

.add_constraint(variable, relation, type) ⇒ Object



97
98
99
# File 'lib/typed/typing_context.rb', line 97

def add_constraint(variable, relation, type)
  type_variables_register.add_constraint(variable, relation, type)
end

.all_constraintsObject



85
86
87
# File 'lib/typed/typing_context.rb', line 85

def all_constraints
  type_variables_register.all_constraints
end

.all_variablesObject



89
90
91
# File 'lib/typed/typing_context.rb', line 89

def all_variables
  type_variables_register.all_variables
end

.bound_generic_type_var?(type_variable) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/typed/typing_context.rb', line 113

def bound_generic_type_var?(type_variable)
  type_variables_register.bound_generic_type_var?(type_variable)
end

.clear(type) ⇒ Object



140
141
142
# File 'lib/typed/typing_context.rb', line 140

def clear(type)
  @type_variables_register = Polymorphism::TypeVariableRegister.new(type)
end

.constraints_for(variable) ⇒ Object



101
102
103
# File 'lib/typed/typing_context.rb', line 101

def constraints_for(variable)
  type_variables_register.constraints[variable] || []
end

.duplicate(within_context) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/typed/typing_context.rb', line 105

def duplicate(within_context)
  current_parent = type_variables_register.parent
  type_variables_register.parent = nil
  duplicated = Marshal.load(Marshal.dump(within_context))
  type_variables_register.parent = current_parent
  duplicated
end

.empty_typing_contextObject



39
40
41
# File 'lib/typed/typing_context.rb', line 39

def empty_typing_context
  Polymorphism::TypeVariableRegister.new(nil, :local)
end

.find_namespace(constant, namespace = self.namespace) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/typed/typing_context.rb', line 18

def find_namespace(constant, namespace = self.namespace)
  return Class.for_name(constant) if constant.start_with?('::')
  Class.for_name(namespace.join('::') + '::' + constant)
rescue NameError => e
  if namespace.empty?
    raise e
  else
    find_namespace(constant, namespace.take(namespace.size - 1))
  end
end

.function_context_popObject



35
36
37
# File 'lib/typed/typing_context.rb', line 35

def function_context_pop
  @function_context = nil
end

.function_context_push(type, message, args) ⇒ Object



29
30
31
# File 'lib/typed/typing_context.rb', line 29

def function_context_push(type, message, args)
  @function_context = [type, message, args]
end

.include?(variable) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/typed/typing_context.rb', line 93

def include?(variable)
  type_variables_register.include?(variable)
end

.local_type_variableObject



81
82
83
# File 'lib/typed/typing_context.rb', line 81

def local_type_variable
  type_variables_register.local_type_variable
end

.namespaceObject



5
6
7
# File 'lib/typed/typing_context.rb', line 5

def namespace
  @namespace ||= []
end

.namespace_popObject



14
15
16
# File 'lib/typed/typing_context.rb', line 14

def namespace_pop
  @namespace.pop
end

.namespace_push(constant) ⇒ Object



9
10
11
12
# File 'lib/typed/typing_context.rb', line 9

def namespace_push(constant)
  parts = constant.split('::')
  @namespace += parts.reject { |part| namespace.include?(part) }
end

.pop_contextObject



124
125
126
127
128
129
130
# File 'lib/typed/typing_context.rb', line 124

def pop_context
  fail StandardError, 'Empty typing context stack, impossible to pop' if @type_variables_register.nil?
  last_register = type_variables_register
  @type_variables_register = @type_variables_register.parent
  @type_variables_register.children.reject! { |child| child == last_register }
  last_register
end

.push_context(type) ⇒ Object



117
118
119
120
121
122
# File 'lib/typed/typing_context.rb', line 117

def push_context(type)
  new_register = Polymorphism::TypeVariableRegister.new(type_variables_register, type)
  @type_variables_register.children << new_register
  @type_variables_register = new_register
  new_register
end

.top_levelObject

work with types



164
165
166
# File 'lib/typed/typing_context.rb', line 164

def self.top_level
  TypingContext.new.add_binding!(:self, TyTopLevelObject.new)
end

.type_variable_for(type, variable, hierarchy) ⇒ Object



47
48
49
# File 'lib/typed/typing_context.rb', line 47

def type_variable_for(type, variable, hierarchy)
  type_variables_register.type_variable_for(type, variable, hierarchy)
end

.type_variable_for_abstraction(abs_kind, variable, context) ⇒ Object



59
60
61
# File 'lib/typed/typing_context.rb', line 59

def type_variable_for_abstraction(abs_kind, variable, context)
  type_variables_register.type_variable_for_abstraction(abs_kind, variable, context)
end

.type_variable_for_function_type(type_var) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/typed/typing_context.rb', line 63

def type_variable_for_function_type(type_var)
  type_variables_register.type_variable_for_generic_type(type_var, true)
  { :lt => :upper_bound, :gt => :lower_bound }.each do |relation, bound|
    if type_var.send(bound)
      value = if type_var.send(bound).is_a?(TyGenericSingletonObject)
                type_var.send(bound).clone
              else
                type_var.send(bound)
              end
      type_var.compatible?(value, relation)
    end
  end
end

.type_variable_for_generic_type(type_var) ⇒ Object



77
78
79
# File 'lib/typed/typing_context.rb', line 77

def type_variable_for_generic_type(type_var)
  type_variables_register.type_variable_for_generic_type(type_var)
end

.type_variable_for_global(variable) ⇒ Object



51
52
53
# File 'lib/typed/typing_context.rb', line 51

def type_variable_for_global(variable)
  type_variables_register.type_variable_for_global(variable)
end

.type_variable_for_message(variable, message) ⇒ Object



55
56
57
# File 'lib/typed/typing_context.rb', line 55

def type_variable_for_message(variable, message)
  type_variables_register.type_variable_for_message(variable, message)
end

.type_variables_registerObject



43
44
45
# File 'lib/typed/typing_context.rb', line 43

def type_variables_register
  @type_variables_register ||= Polymorphism::TypeVariableRegister.new(nil, :top_level)
end

.vars_info(level) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/typed/typing_context.rb', line 144

def vars_info(level)
  method_registry = type_variables_register
  while !method_registry.nil? && method_registry.kind != level
    method_registry = method_registry.parent
  end

  if method_registry
    method_registry.type_variables_register.map do |(key, type_var)|
      type_var if key.first == :generic
    end.compact.each_with_object({}) do |type_var, acc|
      var_name = type_var.variable.split(':').last
      acc["[#{var_name}]"] = type_var
    end
  else
    {}
  end
end

.with_context(context) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/typed/typing_context.rb', line 132

def with_context(context)
  old_context = @type_variables_register
  @type_variables_register = context
  result = yield
  @type_variables_register = old_context
  result
end

Instance Method Details

#add_binding(val, type) ⇒ Object



173
174
175
# File 'lib/typed/typing_context.rb', line 173

def add_binding(val, type)
  TypingContext.new(self).push_binding(val, type)
end

#add_binding!(val, type) ⇒ Object



177
178
179
# File 'lib/typed/typing_context.rb', line 177

def add_binding!(val, type)
  push_binding(val, type)
end

#context_nameObject



194
195
196
# File 'lib/typed/typing_context.rb', line 194

def context_name
  get_self.to_s
end

#get_selfObject



190
191
192
# File 'lib/typed/typing_context.rb', line 190

def get_self
  get_type_for('self')
end

#get_type_for(val) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/typed/typing_context.rb', line 181

def get_type_for(val)
  type = @bindings[val.to_s]
  if type.nil?
    @parent.get_type_for(val) if @parent
  else
    type
  end
end