-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcUtils.ts
310 lines (284 loc) · 8.14 KB
/
tcUtils.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import { BinOp, Stmt, Type, UniOp } from "./ast";
/**
* Map of BinOps to their accepted types and return type.
*
* For example, EQ will accept two ints or two bools, and returns an int.
*/
export const binopTypes: Map<BinOp, [Type[][], Type]> = new Map([
[BinOp.ADD, [[["int", "int"]], "int"]],
[BinOp.SUB, [[["int", "int"]], "int"]],
[BinOp.MUL, [[["int", "int"]], "int"]],
[BinOp.DIV, [[["int", "int"]], "int"]],
[BinOp.MOD, [[["int", "int"]], "int"]],
[BinOp.LT, [[["int", "int"]], "bool"]],
[BinOp.LE, [[["int", "int"]], "bool"]],
[BinOp.GT, [[["int", "int"]], "bool"]],
[BinOp.GE, [[["int", "int"]], "bool"]],
[BinOp.IS, [[["none", "none"]], "bool"]], // This was a bad approach for IS, handled in typechecker directly
[
BinOp.EQ,
[
[
["int", "int"],
["bool", "bool"],
],
"bool",
],
],
[
BinOp.NE,
[
[
["int", "int"],
["bool", "bool"],
],
"bool",
],
],
]);
/**
* Map of UniOps to their accepted types and return types.
*/
export const uniOpTypes: Map<UniOp, [Type[], Type]> = new Map([
[UniOp.NOT, [["bool"], "bool"]],
[UniOp.NEG, [["int"], "int"]],
]);
/**
* Map of builtin functions and their type signatures.
*
* For example, min takes in two ints and returns an int.
*/
export const builtinTypes: Map<string, [Type[], Type]> = new Map([
["print_num", [["int"], "int"]],
["print_bool", [["bool"], "bool"]],
["print_none", [["none"], "int"]],
["abs", [["int"], "int"]],
["min", [["int", "int"], "int"]],
["max", [["int", "int"], "int"]],
["pow", [["int", "int"], "int"]],
]);
/**
* Check if t is an object.
*
* @param t Type
* @returns if it is an object
*/
export function isObject(t: Type): t is { tag: "object"; class: string } {
return typeof t !== "string" && t.tag === "object";
}
/**
* Return if `currType` can be assigned to `goalType`.
*
* Ex, a `None` can be assigned to any type `Object`.
*
* @param currType Type we have
* @param goalType Type we want
* @return if `currType` is assignable to `goalType`
*/
export function assignableTo(currType: Type, goalType: Type): boolean {
if (!isObject(goalType))
// No deep rules for these primitive types, either it's right or it's wrong
return currType === goalType;
// Now we know that goalType is of type object.
// We must either be none, or have the same class.
if (currType === "int" || currType === "bool") return false;
if (currType === "none") return true;
return currType.class === goalType.class;
}
/**
* Checks a grouping of statements to ensure any returns matches the expected
* return type, throws otherwise.
*
* @param stmts Collection of statements in a block
* @param expectedRet the type this block is expected to return
* @throws if any statement returns an incorrect type
*/
export function checkForInvalidReturns(stmts: Stmt<Type>[], expectedRet: Type) {
const extras = stmts
.filter((s) => s.tag === "return" && !assignableTo(s.a, expectedRet))
.map((s) => s.a);
if (extras.length !== 0) {
throwNotExpectedType(expectedRet, extras[0]);
}
}
// ALL TYPE-CHECKING ERRORS
/**
* Stringify the type.
*
* @param t Type
* @returns Name of Type
*/
function typeStr(t: Type): string {
return isObject(t) ? t.class : t;
}
/**
* Throw an error when trying to assign to an invalid type.
*
* @param s Name of type
*/
export function throwInvalidType(s: string): never {
throw new Error(
`TYPE ERROR: Invalid type annotation; there is no class named: ${s}`
);
}
/**
* Throw an error for when a variable is declared twice in the same scope.
*
* @param s the name of the variable
* @throws yes ;)
*/
export function throwDupDecl(s: string): never {
throw new Error(
`TYPE ERROR: Duplicate declaration of identifier in same scope: ${s}`
);
}
/**
* Throws an error when something attempts to use an existing class name.
*
* @param className class name being reused
*/
export function throwShadowClass(className: string): never {
throw new Error(`TYPE ERROR: Cannot shadow class name: ${className}`);
}
/**
* Throw an error when the first parameter of a method is not `self`.
*
* @param method method that doesn't have correct first parameter
*/
export function throwMethodNeedsSelf(method: string): never {
throw new Error(
`TYPE ERROR: First parameter of the following method must be of the enclosing class: ${method}`
);
}
/**
* Throw an error for when an expression's type is not the expected type (ex,
* assigning a bool to a variable that was declared to be an int)
*
* @param expected The expected type
* @param got The type that was actually received
* @throws yes ;)
*/
export function throwNotExpectedType(expected: Type, got: Type): never {
throw new Error(
`TYPE ERROR: Expected type \`${typeStr(expected)}\`; got type \`${typeStr(
got
)}\``
);
}
/**
* Throw an error during a function call when the provided argument is not the
* expected type for that parameter.
*
* @param expected The expected type
* @param got The type that was actually received
* @param pos the parameter position where the error occured
* @throws yes ;)
*/
export function throwNotExpectedTypeParam(
expected: Type,
got: Type,
pos: number
): never {
throw new Error(
`TYPE ERROR: Expected type \`${typeStr(expected)}\`; got type \`${typeStr(
got
)}\` in parameter ${pos}`
);
}
/**
* Throw an error when a function doesn't return on all execution branches.
* @param funcName name of the function where the error occured
* @throws yes ;)
*/
export function throwMustReturn(funcName: string): never {
throw new Error(
`TYPE ERROR: All paths in this function/method must have a return statement: ${funcName}`
);
}
/**
* Throw an error when a function tries to use a variable name that doesn't
* exist in its env.
*
* @param name name of nonexistant var
* @throws yes ;)
*/
export function throwNotAVar(name: string): never {
throw new Error(`TYPE ERROR: Not a variable: ${name}`);
}
/**
* Throw an error when an expr tries to call a nonexistant function.
*
* @param name name of nonexistant function
* @throws yes ;)
*/
export function throwNotAFunc(name: string): never {
throw new Error(`TYPE ERROR: Not a function or class: ${name}`);
}
/**
* Throws an error when a condition in an if/while is not a bool.
*
* @param actualType Type that was there instead of bool
* @throws yes ;)
*/
export function throwCondNotBool(actualType: Type): never {
throw new Error(
`TYPE ERROR: Condition expression cannot be of type \`${typeStr(
actualType
)}\``
);
}
/**
* Throws an error when a function call has the wrong number of arguments.
*
* @param expected expected num of args
* @param got number of args received
*/
export function throwWrongNumArgs(expected: number, got: number): never {
throw new Error(`TYPE ERROR: Expected ${expected} arguments; got ${got}`);
}
export function throwWrongUniopArg(op: UniOp, arg: Type): never {
throw new Error(
`TYPE ERROR: Cannot apply operator \`${op}\` on type \`${typeStr(arg)}\``
);
}
/**
* Throw an error when a BinOp is given the wrong argument types.
*
* @param op BinOp with wrong types
* @param left type on left
* @param right type on right
*/
export function throwWrongBinopArgs(op: BinOp, left: Type, right: Type): never {
throw new Error(
`TYPE ERROR: Cannot apply operator \`${op}\` on types \`${typeStr(
left
)}\` and \`${typeStr(right)}\``
);
}
/**
* Throw an error when a field that doesn't exist on this class is searched for.
*
* @param typ Type being accessed
* @param attr The attribute that doesn't exist for that type
*/
export function throwNoAttr(typ: Type, attr: string): never {
throw new Error(
`TYPE ERROR: There is no attribute named \`${attr}\` in class \`${typeStr(
typ
)}\``
);
}
/**
* Throws an error when a method that doesn't exist on this class is being
* searched for.
*
* @param typ Type being accessed
* @param method The attribute that doesn't exist for that type
*/
export function throwNoMethod(typ: Type, method: string): never {
throw new Error(
`TYPE ERROR: There is no method named \`${method}\` in class \`${typeStr(
typ
)}\``
);
}