Skip to content
← Back

src/semantic/agreed_literal_parameters.ghul

1
namespace Semantic is
2
use Ghul.Pipes
3
4
use Types.Type
5
6
// Candidates can tie because a function literal argument has not been
7
// typed yet: its parameters have no written types, so its result is not
8
// known, and the candidates differ only in the result their function
9
// formal asks for. Where every candidate gives the literal the same
10
// parameters, the literal can be typed against those parameters alone,
11
// with its result left to its own body, and the candidates then compared
12
// against the result that body produces.
13
class AGREED_LITERAL_PARAMETERS is
14
// Per argument, the function type to type a literal argument
15
// against: the parameters every candidate's formal agrees on, and
16
// an error type in place of the result, which pushes nothing onto
17
// the literal's body. Absent for an argument that is not such a
18
// literal. Null where the candidates disagree on any literal's
19
// parameters, where a parameter is not settled, or where no
20
// argument is a literal still to be typed.
21
@suppress("presence-test-non-optional")
22
shapes(
23
candidates: Collections.List[Symbols.Function],
24
arguments: Collections.List[Type],
25
innate_symbol_lookup: Lookups.InnateSymbolLookup
26
) -> Collections.List[Type?]? static is
27
if candidates.count < 2 then
28
return null
29
fi
30
31
let result = Collections.LIST[Type?]()
32
let any_literal mut = false
33
34
for i in 0..arguments.count do
35
let argument = arguments[i]
36
37
if !argument? \/ !argument.is_function_with_any_implicit_argument_types then
38
result.add(null)
39
continue
40
fi
41
42
// A literal whose result is already known has nothing more
43
// to say once re-walked, and the tie stands.
44
if !argument.is_action /\ argument.arguments.count > 0 /\ argument.arguments[argument.arguments.count - 1].is_settled then
45
return null
46
fi
47
48
let shape = _agreed_shape(candidates, i, innate_symbol_lookup)
49
50
if !shape? then
51
return null
52
fi
53
54
result.add(shape)
55
any_literal = true
56
od
57
58
if !any_literal then
59
return null
60
fi
61
62
return result
63
si
64
65
@suppress("presence-test-non-optional")
66
_agreed_shape(
67
candidates: Collections.List[Symbols.Function],
68
index: int,
69
innate_symbol_lookup: Lookups.InnateSymbolLookup
70
) -> Type? static is
71
let first_candidate = candidates[0]
72
73
if index >= first_candidate.arguments.count then
74
return null
75
fi
76
77
let first = first_candidate.arguments[index]
78
79
if !first? \/ !first.is_function then
80
return null
81
fi
82
83
let count = ARGUMENT_PACK.parameter_count(first)
84
85
for p in 0..count do
86
if !first.arguments[p].is_settled \/ first.arguments[p].is_wild then
87
return null
88
fi
89
od
90
91
for candidate in candidates do
92
if index >= candidate.arguments.count then
93
return null
94
fi
95
96
let formal = candidate.arguments[index]
97
98
if
99
!formal? \/ !formal.is_function \/
100
ARGUMENT_PACK.parameter_count(formal) != count \/
101
formal.is_pure_function != first.is_pure_function
102
then
103
return null
104
fi
105
106
for p in 0..count do
107
if formal.arguments[p].compare(first.arguments[p]) != Types.MATCH.SAME then
108
return null
109
fi
110
od
111
od
112
113
let types = Collections.LIST[Type]()
114
115
for p in 0..count do
116
types.add(first.arguments[p])
117
od
118
119
types.add(Types.ERROR())
120
121
return innate_symbol_lookup.get_function_type(types, first.is_pure_function)
122
si
123
si
124
si