Class: RLSL::Prism::CollectionTypeResolver

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

Instance Method Summary collapse

Methods included from TypeShapes

array, array?, element_type

Constructor Details

#initialize(type_environment:, custom_functions:, register:) ⇒ CollectionTypeResolver

Returns a new instance of CollectionTypeResolver.



8
9
10
11
12
# File 'lib/rlsl/prism/type_inference/collection_type_resolver.rb', line 8

def initialize(type_environment:, custom_functions:, register:)
  @type_environment = type_environment
  @custom_functions = custom_functions
  @register = register
end

Instance Method Details

#assign_multiple_targets(node) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/rlsl/prism/type_inference/collection_type_resolver.rb', line 38

def assign_multiple_targets(node)
  value_type = node.value.type
  return assign_tuple_targets(node.targets, value_type.types) if value_type.is_a?(IR::TupleType)
  return assign_array_targets(node.targets, TypeShapes.element_type(value_type)) if TypeShapes.array?(value_type)
  return assign_custom_targets(node.targets, @custom_functions[node.value.name]) if custom_multi_return?(node.value)

  nil
end

#resolve_array_index(node) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/rlsl/prism/type_inference/collection_type_resolver.rb', line 19

def resolve_array_index(node)
  array_type = node.array.type
  return TypeShapes.element_type(array_type) if TypeShapes.array?(array_type)
  return @type_environment.array_element_type(node.array.name) || :float if node.array.is_a?(IR::VarRef)

  :float
end

#resolve_array_literal(node) ⇒ Object



14
15
16
17
# File 'lib/rlsl/prism/type_inference/collection_type_resolver.rb', line 14

def resolve_array_literal(node)
  element_type = node.elements.first&.type || :float
  TypeShapes.array(element_type)
end

#resolve_global_decl(node) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/rlsl/prism/type_inference/collection_type_resolver.rb', line 27

def resolve_global_decl(node)
  if node.initializer.is_a?(IR::ArrayLiteral)
    node.array_size ||= node.initializer.elements.length
    first_elem = node.initializer.elements.first
    node.element_type ||= first_elem&.type || :float
    return TypeShapes.array(node.element_type)
  end

  node.initializer&.type
end