-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
overload-resolver.ts
160 lines (135 loc) · 4.56 KB
/
overload-resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
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
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
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
149
150
151
152
153
154
155
156
157
158
159
160
import { _IType, _ArgDefDetails, nil, DataType, IValue } from '../interfaces-private.ts';
import { Types } from '../datatypes/index.ts';
import { it } from '../utils.ts';
import { QueryError } from '../interfaces.ts';
export interface HasSig {
name: string;
args: _ArgDefDetails[];
argsVariadic?: _IType | nil;
}
export class OverloadResolver<T extends HasSig> {
private byName = new Map<string, OverloadNode<T>>();
constructor(private implicitCastOnly: boolean) { }
add(value: T, replaceIfExists: boolean) {
let ret = this.byName.get(value.name);
if (!ret) {
this.byName.set(value.name, ret = new OverloadNode<T>(Types.null, this.implicitCastOnly, 0));
}
ret.index(value, replaceIfExists);
}
getOverloads(name: string) {
const ovr = this.byName.get(name);
if (!ovr) {
return [];
}
return [...ovr.all()];
}
remove(value: T) {
this.byName.get(value.name)?.unindex(value);
}
resolve(name: string, args: IValue[]) {
return this.byName.get(name)?.resolve(args);
}
getExact(name: string, types: _IType[]): T | nil {
return this.byName.get(name)?.getExact(types);
}
}
class OverloadNode<T extends HasSig> {
private nexts = new Map<DataType, OverloadNode<T>[]>();
private leaf: T | nil;
constructor(readonly type: _IType, private implicitCastOnly: boolean, private at: number) {
}
*all(): IterableIterator<T> {
if (this.leaf) {
yield this.leaf;
}
for (const children of this.nexts.values()) {
for (const child of children) {
yield* child.all();
}
}
}
index(value: T, replaceIfExists: boolean) {
if (this.at >= value.args.length) {
if (this.leaf && !replaceIfExists) {
throw new QueryError('Function already exists: ' + value.name);
}
this.leaf = value;
return;
}
const arg = value.args[this.at];
const primary = arg.type.primary;
let lst = this.nexts.get(primary);
if (!lst) {
this.nexts.set(primary, lst = []);
}
// get or add corresponding node
let node = lst.find(x => x.type === arg.type);
if (!node) {
lst.push(node = new OverloadNode(arg.type, this.implicitCastOnly, this.at + 1));
}
// process arg list
node.index(value, replaceIfExists);
}
unindex(value: T) {
if (this.leaf === value) {
this.leaf = null;
return;
}
for (const children of this.nexts.values()) {
for (const child of children) {
child.unindex(value);
}
}
}
getExact(types: _IType[]): T | nil {
if (this.at >= types.length) {
return this.leaf;
}
const target = types[this.at];
const found = this.nexts.get(target.primary)
?.find(x => x.type == target);
return found?.getExact(types);
}
resolve(args: IValue[]): T | nil {
if (this.at >= args.length) {
return this.leaf;
}
// gets the child which type matches the current arg better
const arg = args[this.at];
const sigsToCheck = it(
this.nexts // perf tweak: search by primary type
.get(arg.type.primary)
?? it(this.nexts.values()).flatten() // else, search all registered overloads
);
const match = sigsToCheck.reduce<OverloadNode<T> | nil>((acc, x) => {
// check that arg can be converted to the target type
if (!this.compatible(arg, x.type)) {
return acc;
}
// first match
if (!acc) {
return x;
}
// returns the prefered type
return acc.type.prefer(x.type) === x.type ? x : acc;
}, null);
if (match) {
return match.resolve(args);
}
// handle variadic args
if (this.leaf && this.leaf.argsVariadic && this.compatible(arg, this.leaf.argsVariadic)) {
return this.leaf;
}
// not found
return null;
}
private compatible(givenArg: IValue, expectedArg: _IType) {
if (givenArg.type === expectedArg) {
return true;
}
return givenArg.isConstantLiteral
? givenArg.type.canCast(expectedArg)
: givenArg.type.canConvertImplicit(expectedArg) ?? givenArg.type.canCast(expectedArg);
}
}