Module: Jade::Frontend::ForwardDeclaration::Helper

Included in:
Body, FunctionDeclaration, Implementation, InterfaceDeclaration, InteropImportDeclaration, Module, StructDeclaration, TypeDeclaration
Defined in:
lib/jade/frontend/forward_declaration/helper.rb

Instance Method Summary collapse

Instance Method Details

#deep_declare_node(ast, entry, registry) ⇒ Object



9
10
11
# File 'lib/jade/frontend/forward_declaration/helper.rb', line 9

def deep_declare_node(ast, entry, registry)
  ForwardDeclaration.deep_declare_node(ast, entry, registry)
end

#figure_out_type(entry, node) ⇒ Object



13
14
15
16
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
56
57
58
59
60
61
62
63
# File 'lib/jade/frontend/forward_declaration/helper.rb', line 13

def figure_out_type(entry, node)
  case node
  in AST::TypeVar(type:)
    Ok[Symbol.var(type, node.range)]

  in AST::TypeName(type:)
    require_type(entry, type, node.range)
      .map { Symbol.type_application(it.to_ref, []) }

  in AST::TypeApplication(constructor:, args:)
    constructor_r =
      case constructor
      in AST::TypeName
        require_type(entry, constructor.type, constructor.range)

      in AST::QualifiedTypeName(path:)
        *module_parts, type_name = path
        require_qualified_type(entry, module_parts.join('.'), type_name, constructor.range)
      end

    args
      .map { figure_out_type(entry, it) }
      .then { Results.sequence(it) }
      .and_then { |resolved_args| constructor_r.map { Symbol.type_application(it.to_ref, resolved_args, node.range) } }

  in AST::TypeFunction(params:, return_type:)
    params
      .map { figure_out_type(entry, it) }
      .then { Results.sequence(it) }
      .and_then { |resolved_params| figure_out_type(entry, return_type).map { Symbol.function_type(resolved_params, it) } }

  in AST::TypeTuple(items:) if items.length > Error::TupleArityOverflow::MAX_ARITY
    Err[Error::TupleArityOverflow.new(entry&.name, node.range, arity: items.length)]

  in AST::TypeTuple(items:)
    type_name = Stdlib::Tuple.constructor_by_arity(items.length)

    items
      .map { figure_out_type(entry, it) }
      .then { Results.sequence(it) }
      .map { Symbol.type_application(Symbol.type_ref(*type_name.split('.')), it, node.range) }

  in AST::TypeRecord(fields:, row_var:)
    row = row_var&.then { |row| Symbol.var(row.name, row.range) }

    fields
      .map { |k, v| figure_out_type(entry, v).map { [k, it] } }
      .then { Results.sequence(it) }
      .map { Symbol.record_type(it.to_h, row) }
  end
end

#shallow_declare_node(ast, registry, entry) ⇒ Object



5
6
7
# File 'lib/jade/frontend/forward_declaration/helper.rb', line 5

def shallow_declare_node(ast, registry, entry)
  ForwardDeclaration.shallow_declare_node(ast, registry, entry)
end