Module: Jade::Frontend::SemanticAnalysis::Helper

Instance Method Summary collapse

Instance Method Details

#analyze_duplicate_fields(fields, entry) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/jade/frontend/semantic_analysis/helper.rb', line 52

def analyze_duplicate_fields(fields, entry)
  fields
    .group_by(&:key)
    .select { |_, v| v.size > 1 }
    .map do |k, v|
      first, *rest = v
      Error::DuplicateRecordField
        .new(entry.name, first.range, field_name: k, duplicate_spans: rest.map(&:range))
    end
end

#analyze_in_parallel(nodes, registry, scope, entry) ⇒ Object

Independent: every child analyzed with the input scope; the returned scope is the input scope (no bindings leak). Use when children don’t see each other — record fields, list items, function-call args, if/case branches.



47
48
49
50
# File 'lib/jade/frontend/semantic_analysis/helper.rb', line 47

def analyze_in_parallel(nodes, registry, scope, entry)
  results = nodes.map { analyze_node(it, registry, scope, entry) }
  Result[results.map(&:node), results.flat_map(&:errors), scope]
end

#analyze_in_sequence(nodes, registry, scope, entry) ⇒ Object

Threaded: child N analyzed with the scope produced by child N-1; the returned scope is the last child’s. Use when later siblings depend on bindings introduced by earlier ones — body statements, lambda/function params, sequential pattern bindings.



36
37
38
39
40
41
# File 'lib/jade/frontend/semantic_analysis/helper.rb', line 36

def analyze_in_sequence(nodes, registry, scope, entry)
  nodes.reduce(Result[[], [], scope]) do |acc, node|
    r = analyze_node(node, registry, acc.scope, entry)
    Result[acc.node + [r.node], acc.errors + r.errors, r.scope]
  end
end

#analyze_node(node, registry, scope, entry) ⇒ Object



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

def analyze_node(node, registry, scope, entry)
  SemanticAnalysis.send(:analyze_node, node, registry, scope, entry)
end

#bind(scope, symbol, entry) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jade/frontend/semantic_analysis/helper.rb', line 9

def bind(scope, symbol, entry)
  name = symbol.name
  if scope.lookup(name)
    Error::ShadowingError
      .new(entry.name, symbol.decl_span, name:)
      .then { Result[nil, [it], scope] }

  else
    Result[nil, [], scope.bind(name, symbol)]
  end
end

#collect_vars(symbol, registry) ⇒ Object



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
# File 'lib/jade/frontend/semantic_analysis/helper.rb', line 63

def collect_vars(symbol, registry)
  case symbol
  in Symbol::TypeRef | Symbol::ValueRef
    registry
      .lookup(symbol)
      .then { collect_vars(it, registry) }

  in Symbol::Constructor(args:)
    args
      .flat_map { collect_vars(it, registry) }

  in Symbol::Variable
    [symbol]

  in Symbol::TypeApplication | Symbol::PartialApplication
    symbol
      .args
      .flat_map { collect_vars(it, registry) }

  in Symbol::FunctionType | Symbol::InterfaceFunction
    symbol
      .params
      .flat_map { collect_vars(it, registry) } +
        collect_vars(symbol.return_type, registry)

  in Symbol::RecordType(row_var:)
    row_var.nil? ? [] : [row_var]
  end
end

#lookup(scope, name, entry, span) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/jade/frontend/semantic_analysis/helper.rb', line 21

def lookup(scope, name, entry, span)
  if (decl = scope.lookup(name))
    Result[decl, [], scope]

  else
    Error::UndefinedVariable
      .new(entry.name, span, var_ref: name)
      .then { Result[nil, [it], scope] }
  end
end

#validate_type_symbol(symbol, registry, entry) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/jade/frontend/semantic_analysis/helper.rb', line 93

def validate_type_symbol(symbol, registry, entry)
  case symbol
  in Symbol::Union(variants:, type_params:)
    variants.flat_map { validate_type_symbol(it, registry, entry) } +
      type_params.flat_map { validate_type_symbol(it, registry, entry) }

  in Symbol::Constructor(args:)
    args.flat_map { validate_type_symbol(it, registry, entry) }

  in Symbol::TypeRef
    registry.lookup(symbol)
      .then { validate_type_symbol(it, registry, entry) }

  in Symbol::ValueRef
    registry.lookup(symbol)
      .then { validate_type_symbol(it, registry, entry) }

  in Symbol::Variable
    []

  in Symbol::PartialApplication(constructor:, args:)
    args.flat_map { validate_type_symbol(it, registry, entry) }

  in Symbol::TypeApplication(constructor:, args:)
    constructor_symbol = registry.lookup(constructor)

    if constructor_symbol.type_params.size != args.size
      [Error::TypeArgsMismatch.new(
          entry.name,
          symbol.span,
          type_name: constructor.name,
          expected: constructor_symbol.type_params.size,
          actual: args.size
      )]
    else
      []
    end + args.flat_map { validate_type_symbol(it, registry, entry) }

  in Symbol::FunctionType | Symbol::InterfaceFunction | Symbol::InteropFunction
    validate_type_symbol(symbol.return_type, registry, entry) +
      symbol.params.flat_map { validate_type_symbol(it, registry, entry) }

  in Symbol::Function(params:, return_type:)
    validate_type_symbol(return_type, registry, entry) +
      params.values.flat_map { validate_type_symbol(it, registry, entry) }

  in Symbol::RecordType(fields:)
    fields.reduce([]) do |acc, (k, v)|
      acc + validate_type_symbol(v, registry, entry)
    end

  in Symbol::Struct(type_params:, record_type:)
    validate_type_symbol(record_type, registry, entry) +
      type_params.flat_map { validate_type_symbol(it, registry, entry) }
  end
end