Module: Jade::Frontend::TypeChecking::Definition

Extended by:
Definition
Included in:
Definition
Defined in:
lib/jade/frontend/type_checking/definition.rb

Instance Method Summary collapse

Instance Method Details

#constructor(name, parent_name, args) ⇒ Object



57
58
59
# File 'lib/jade/frontend/type_checking/definition.rb', line 57

def constructor(name, parent_name, args)
  ConstructorDef[name, parent_name, args]
end

#from_symbol(sym, var_gen = VarGen.new, registry) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jade/frontend/type_checking/definition.rb', line 17

def from_symbol(sym, var_gen = VarGen.new, registry)
  # TODO: Don't need a var gen here, definitions don't have identity. they just share the name
  case sym
  in Symbol::Struct
    type_params, type_params_map = sym
      .type_params
      .reduce([[], {}]) do |(types, local_map), sym|
        Type.send(:from_symbol_r, sym, registry, var_gen, local_map)
          .then { |(t, _, new_map)| [types + [t], new_map] }
      end

    Type
      .send(:from_symbol_r, sym.record_type, registry, var_gen, type_params_map)
      .first
      .then { Definition.struct(sym.qualified_name, type_params, it) }

  in Symbol::Union
    type = Type.from_symbol(sym, registry, var_gen).first

    sym
      .variants
      .map do |variant|
        args = case Type.from_symbol(variant, registry, var_gen).first
               in Type::Function(args:) then args
               else []
               end
        Definition.constructor(variant.qualified_name, sym.qualified_name, args)
      end
      .then { Definition.type(sym.qualified_name, type.args, it) }

  in Symbol::Interface(name:)
    InterfaceDef[name]

  in Symbol::TypeRef
    registry
      .lookup(sym)
      .then { from_symbol(it, var_gen, registry) }
  end
end

#struct(name, type_params, body) ⇒ Object



65
66
67
# File 'lib/jade/frontend/type_checking/definition.rb', line 65

def struct(name, type_params, body)
  StructDef[name, type_params, body]
end

#type(name, type_params, constructors) ⇒ Object



61
62
63
# File 'lib/jade/frontend/type_checking/definition.rb', line 61

def type(name, type_params, constructors)
  TypeDef[name, type_params, constructors]
end