Class: RLSL::Prism::TypeInference

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

Constant Summary collapse

EXPRESSION_NODES =
{
  IR::VarRef => :infer_var_ref,
  IR::Literal => :infer_literal,
  IR::BoolLiteral => :infer_bool_literal,
  IR::BinaryOp => :infer_binary_op,
  IR::UnaryOp => :infer_unary_op,
  IR::FuncCall => :infer_func_call,
  IR::FieldAccess => :infer_field_access,
  IR::Swizzle => :infer_swizzle,
  IR::Parenthesized => :infer_parenthesized,
  IR::ArrayLiteral => :infer_array_literal,
  IR::ArrayIndex => :infer_array_index
}.freeze
DEFINITION_NODES =
{
  IR::VarDecl => :infer_var_decl,
  IR::Assignment => :infer_assignment,
  IR::GlobalDecl => :infer_global_decl,
  IR::MultipleAssignment => :infer_multiple_assignment
}.freeze
CONTROL_FLOW_NODES =
{
  IR::IfStatement => :infer_if_statement,
  IR::Ternary => :infer_ternary,
  IR::Return => :infer_return,
  IR::ForLoop => :infer_for_loop,
  IR::WhileLoop => :infer_while_loop,
  IR::FunctionDefinition => :infer_function_definition
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(uniforms = {}, custom_functions = {}, globals: {}) ⇒ TypeInference

Returns a new instance of TypeInference.



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
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rlsl/prism/type_inference.rb', line 50

def initialize(uniforms = {}, custom_functions = {}, globals: {})
  @types = TypeEnvironment.new
  @uniforms = uniforms
  @custom_functions = custom_functions
  @globals = globals
  @inferer_registry = InfererRegistry.new
  @call_validator = CallValidator.new
  @call_type_resolver = CallTypeResolver.new(
    custom_functions: @custom_functions,
    call_validator: @call_validator
  )
  @field_type_resolver = FieldTypeResolver.new(uniforms: @uniforms)
  @collection_type_resolver = CollectionTypeResolver.new(
    type_environment: @types,
    custom_functions: @custom_functions,
    register: method(:register)
  )
  @expression_inferer = ExpressionInferer.new(
    infer: method(:infer),
    lookup: method(:lookup),
    call_type_resolver: @call_type_resolver,
    field_type_resolver: @field_type_resolver,
    collection_type_resolver: @collection_type_resolver
  )
  @definition_inferer = DefinitionInferer.new(
    infer: method(:infer),
    register: method(:register),
    collection_type_resolver: @collection_type_resolver
  )
  @control_flow_inferer = ControlFlowInferer.new(
    infer: method(:infer),
    infer_child_scope: method(:infer_child_scope),
    infer_in_scope: method(:infer_in_scope)
  )
  register_inferers

  uniforms.each do |name, type|
    register(name, type)
  end

  globals.each do |name, type|
    register(name, type)
  end
end

Instance Method Details

#infer(node, scoped: false) ⇒ Object



111
112
113
114
# File 'lib/rlsl/prism/type_inference.rb', line 111

def infer(node, scoped: false)
  options = node.is_a?(IR::Block) ? { scoped: scoped } : {}
  @inferer_registry.infer(node, **options) || node
end

#lookup(name) ⇒ Object



107
108
109
# File 'lib/rlsl/prism/type_inference.rb', line 107

def lookup(name)
  @types.lookup(name)
end

#register(name, type) ⇒ Object



99
100
101
# File 'lib/rlsl/prism/type_inference.rb', line 99

def register(name, type)
  @types.register(name, type)
end

#register_function(name, returns:) ⇒ Object



103
104
105
# File 'lib/rlsl/prism/type_inference.rb', line 103

def register_function(name, returns:)
  @custom_functions[name.to_sym] = { returns: returns }
end

#symbol_tableObject



95
96
97
# File 'lib/rlsl/prism/type_inference.rb', line 95

def symbol_table
  @types.to_h
end