Class: RLSL::Prism::TypeEnvironment

Inherits:
Object
  • Object
show all
Includes:
TypeShapes
Defined in:
lib/rlsl/prism/type_inference/type_environment.rb

Constant Summary collapse

ARRAY_ELEMENT_SUFFIX =
"_element_type"

Instance Method Summary collapse

Methods included from TypeShapes

array, array?, element_type

Constructor Details

#initializeTypeEnvironment

Returns a new instance of TypeEnvironment.



10
11
12
13
# File 'lib/rlsl/prism/type_inference/type_environment.rb', line 10

def initialize
  @value_scopes = ScopeStack.new
  @array_element_scopes = ScopeStack.new
end

Instance Method Details

#array_element_type(name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/rlsl/prism/type_inference/type_environment.rb', line 43

def array_element_type(name)
  normalized_name = name.to_sym
  explicit_type = @array_element_scopes.lookup(normalized_name)
  return explicit_type if explicit_type

  value_type = @value_scopes.lookup(normalized_name)
  return TypeShapes.element_type(value_type) if TypeShapes.array?(value_type)

  nil
end

#lookup(name) ⇒ Object



36
37
38
39
40
41
# File 'lib/rlsl/prism/type_inference/type_environment.rb', line 36

def lookup(name)
  normalized_name = name.to_sym
  return array_element_type((normalized_name)) if (normalized_name)

  @value_scopes.lookup(normalized_name)
end

#popObject



27
28
29
30
# File 'lib/rlsl/prism/type_inference/type_environment.rb', line 27

def pop
  @value_scopes.pop
  @array_element_scopes.pop
end

#push(initial_scope = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rlsl/prism/type_inference/type_environment.rb', line 15

def push(initial_scope = {})
  value_scope = {}
  array_scope = {}

  normalize(initial_scope).each do |name, type|
    register_in_scopes(value_scope, array_scope, name, type)
  end

  @value_scopes.push(value_scope)
  @array_element_scopes.push(array_scope)
end

#register(name, type) ⇒ Object



32
33
34
# File 'lib/rlsl/prism/type_inference/type_environment.rb', line 32

def register(name, type)
  register_in_scopes(@value_scopes, @array_element_scopes, name, type)
end

#to_hObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rlsl/prism/type_inference/type_environment.rb', line 54

def to_h
  values = @value_scopes.to_h
  elements = @array_element_scopes.to_h.each_with_object({}) do |(name, type), memo|
    memo[(name)] = type
  end

  values.each do |name, type|
    next unless TypeShapes.array?(type)

    elements[(name)] ||= TypeShapes.element_type(type)
  end

  values.merge(elements)
end