-
Notifications
You must be signed in to change notification settings - Fork 1
/
TypeChecker.cs
519 lines (392 loc) · 19.3 KB
/
TypeChecker.cs
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
using System.Collections.Generic;
using System.Linq;
using Parsley;
using Rook.Compiling.Types;
using Rook.Core.Collections;
namespace Rook.Compiling.Syntax
{
public class TypeChecker
{
public readonly TypeVariableSource typeVariables;
private readonly TypeUnifier unifier;
private readonly TypeRegistry typeRegistry;
private readonly List<CompilerError> errorLog;
public TypeChecker()
{
unifier = new TypeUnifier();
typeRegistry = new TypeRegistry();
errorLog = new List<CompilerError>();
typeVariables = new TypeVariableSource();
}
public TypeVariable CreateGenericTypeVariable()
{
return typeVariables.CreateGenericTypeVariable();
}
public TypeVariable CreateNonGenericTypeVariable()
{
return typeVariables.CreateNonGenericTypeVariable();
}
public Vector<CompilerError> Errors { get { return errorLog.ToVector(); } }
public bool HasErrors { get { return errorLog.Any(); } }
//TODO: This property is deprecated. Once TypeRegistry can discover .NET types via reflection,
//rephrase unit test usages of this so they don't have to manually prepare the TypeRegistry.
public TypeRegistry TypeRegistry { get { return typeRegistry; } }
public CompilationUnit TypeCheck(CompilationUnit compilationUnit)
{
var Position = compilationUnit.Position;
var Classes = compilationUnit.Classes;
var Functions = compilationUnit.Functions;
foreach (var @class in Classes)//TODO: Test coverage.
typeRegistry.Register(@class);
var scope = new GlobalScope(this);
foreach (var @class in Classes)
if (!scope.TryIncludeUniqueBinding(@class))
{
LogError(CompilerError.DuplicateIdentifier(@class.Position, @class));
return null;
}
foreach (var function in Functions)
if (!scope.TryIncludeUniqueBinding(function))
{
LogError(CompilerError.DuplicateIdentifier(function.Position, function));
return null;
}
var typeCheckedClasses = TypeCheckAll(Classes, scope);
var typeCheckedFunctions = TypeCheckAll(Functions, scope);
if (typeCheckedClasses.Any(x => x == null) || typeCheckedFunctions.Any(x => x == null))
return null;
return new CompilationUnit(Position, typeCheckedClasses, typeCheckedFunctions);
}
public Class TypeCheck(Class @class, Scope scope)
{
var Position = @class.Position;
var Name = @class.Name;
var Methods = @class.Methods;
var localScope = new LocalScope(scope);
foreach (var method in Methods)
if (!localScope.TryIncludeUniqueBinding(method))
{
LogError(CompilerError.DuplicateIdentifier(method.Position, method));
return null;
}
var typeCheckedMethods = TypeCheckAll(Methods, localScope);
if (typeCheckedMethods.Any(x => x == null))
return null;
return new Class(Position, Name, typeCheckedMethods);
}
public Function TypeCheck(Function function, Scope scope)
{
var Position = function.Position;
var ReturnType = function.ReturnType;
var Name = function.Name;
var Parameters = function.Parameters;
var Body = function.Body;
var DeclaredType = function.DeclaredType;
var localScope = new LocalScope(scope);
foreach (var parameter in Parameters)
if (!localScope.TryIncludeUniqueBinding(parameter))
{
LogError(CompilerError.DuplicateIdentifier(parameter.Position, parameter));
return null;
}
var typeCheckedBody = TypeCheck(Body, localScope);
if (typeCheckedBody == null)
return null;
if (!Unify(typeCheckedBody.Position, ReturnType, typeCheckedBody.Type))
return null;
return new Function(Position, ReturnType, Name, Parameters, typeCheckedBody, DeclaredType);
}
public Expression TypeCheck(Expression expression, Scope scope)
{
return expression.WithTypes(this, scope);
}
public Expression TypeCheck(Name name, Scope scope)
{
var Position = name.Position;
var Identifier = name.Identifier;
DataType type;
if (scope.TryGet(Identifier, out type))
return new Name(Position, Identifier, FreshenGenericTypeVariables(type));
LogError(CompilerError.UndefinedIdentifier(name));
return null;
}
private DataType FreshenGenericTypeVariables(DataType type)
{
var substitutions = new Dictionary<TypeVariable, DataType>();
var genericTypeVariables = type.FindTypeVariables().Where(x => x.IsGeneric);
foreach (var genericTypeVariable in genericTypeVariables)
substitutions[genericTypeVariable] = typeVariables.CreateGenericTypeVariable();
return type.ReplaceTypeVariables(substitutions);
}
public Expression TypeCheck(Block block, Scope scope)
{
var Position = block.Position;
var VariableDeclarations = block.VariableDeclarations;
var InnerExpressions = block.InnerExpressions;
var localScope = new LocalScope(scope);
var typedVariableDeclarations = new List<VariableDeclaration>();
foreach (var variable in VariableDeclarations)
{
var typeCheckedValue = TypeCheck(variable.Value, localScope);
if (typeCheckedValue == null)
return null;
var binding = variable;
if (variable.IsImplicitlyTyped())
binding = new VariableDeclaration(variable.Position, /*Replaces implicit type.*/ typeCheckedValue.Type, variable.Identifier, variable.Value);
if (!localScope.TryIncludeUniqueBinding(binding))
{
LogError(CompilerError.DuplicateIdentifier(binding.Position, binding));
return null;
}
typedVariableDeclarations.Add(new VariableDeclaration(variable.Position,
binding.Type,
variable.Identifier,
typeCheckedValue));
if (!Unify(typeCheckedValue.Position, binding.Type, typeCheckedValue.Type))
return null;
}
var typeCheckedInnerExpressions = TypeCheckAll(InnerExpressions, localScope);
if (typeCheckedInnerExpressions.Any(x => x == null))
return null;
var blockType = typeCheckedInnerExpressions.Last().Type;
return new Block(Position, typedVariableDeclarations.ToVector(), typeCheckedInnerExpressions, blockType);
}
public Expression TypeCheck(Lambda lambda, Scope scope)
{
var Position = lambda.Position;
var Parameters = lambda.Parameters;
var Body = lambda.Body;
var lambdaScope = new LambdaScope(scope);
var typedParameters = ReplaceImplicitTypesWithNewNonGenericTypeVariables(Parameters);
foreach (var parameter in typedParameters)
{
if (!lambdaScope.TryIncludeUniqueBinding(parameter))
{
LogError(CompilerError.DuplicateIdentifier(parameter.Position, parameter));
return null;
}
}
var typeCheckedBody = TypeCheck(Body, lambdaScope);
if (typeCheckedBody == null)
return null;
var normalizedParameters = NormalizeTypes(typedParameters);
var parameterTypes = normalizedParameters.Select(p => p.Type).ToArray();
return new Lambda(Position, normalizedParameters, typeCheckedBody, NamedType.Function(parameterTypes, typeCheckedBody.Type));
}
private Parameter[] ReplaceImplicitTypesWithNewNonGenericTypeVariables(IEnumerable<Parameter> parameters)
{
var decoratedParameters = new List<Parameter>();
var typeVariables = new List<TypeVariable>();
foreach (var parameter in parameters)
{
if (parameter.IsImplicitlyTyped())
{
var typeVariable = this.typeVariables.CreateNonGenericTypeVariable();
typeVariables.Add(typeVariable);
decoratedParameters.Add(new Parameter(parameter.Position, typeVariable, parameter.Identifier));
}
else
{
decoratedParameters.Add(parameter);
}
}
return decoratedParameters.ToArray();
}
private Vector<Parameter> NormalizeTypes(IEnumerable<Parameter> typedParameters)
{
return typedParameters.Select(p => new Parameter(p.Position, unifier.Normalize(p.Type), p.Identifier)).ToVector();
}
public Expression TypeCheck(If conditional, Scope scope)
{
var Position = conditional.Position;
var Condition = conditional.Condition;
var BodyWhenTrue = conditional.BodyWhenTrue;
var BodyWhenFalse = conditional.BodyWhenFalse;
var typeCheckedCondition = TypeCheck(Condition, scope);
var typeCheckedWhenTrue = TypeCheck(BodyWhenTrue, scope);
var typeCheckedWhenFalse = TypeCheck(BodyWhenFalse, scope);
if (typeCheckedCondition == null || typeCheckedWhenTrue == null || typeCheckedWhenFalse == null)
return null;
var unifiedA = Unify(typeCheckedCondition.Position, NamedType.Boolean, typeCheckedCondition.Type);
var unifiedB = Unify(typeCheckedWhenFalse.Position, typeCheckedWhenTrue.Type, typeCheckedWhenFalse.Type);
if (!unifiedA || !unifiedB)
return null;
return new If(Position, typeCheckedCondition, typeCheckedWhenTrue, typeCheckedWhenFalse, typeCheckedWhenTrue.Type);
}
public Expression TypeCheck(Call call, Scope scope)
{
var Position = call.Position;
var Callable = call.Callable;
var Arguments = call.Arguments;
var IsOperator = call.IsOperator;
var typeCheckedCallable = TypeCheck(Callable, scope);
var typeCheckedArguments = TypeCheckAll(Arguments, scope);
if (typeCheckedCallable == null || typeCheckedArguments.Any(x => x == null))
return null;
var calleeType = typeCheckedCallable.Type;
var calleeFunctionType = calleeType as NamedType;
if (calleeFunctionType == null || calleeFunctionType.Name != "System.Func")
{
LogError(CompilerError.ObjectNotCallable(Position));
return null;
}
var returnType = calleeType.InnerTypes.Last();
var argumentTypes = typeCheckedArguments.Select(x => x.Type).ToVector();
if (!Unify(Position, calleeType, NamedType.Function(argumentTypes, returnType)))
return null;
var callType = unifier.Normalize(returnType);
return new Call(Position, typeCheckedCallable, typeCheckedArguments, IsOperator, callType);
}
public Expression TypeCheck(MethodInvocation methodInvocation, Scope scope)
{
var Position = methodInvocation.Position;
var Instance = methodInvocation.Instance;
var MethodName = methodInvocation.MethodName;
var Arguments = methodInvocation.Arguments;
var typeCheckedInstance = TypeCheck(Instance, scope);
if (typeCheckedInstance == null)
return null;
var instanceType = typeCheckedInstance.Type;
var instanceNamedType = instanceType as NamedType;
if (instanceNamedType == null)
{
LogError(CompilerError.AmbiguousMethodInvocation(Position));
return null;
}
Vector<Binding> typeMembers;
if (typeRegistry.TryGetMembers(instanceNamedType, out typeMembers))
{
Scope typeMemberScope = new TypeMemberScope(typeMembers);
//This block is SUSPICIOUSLY like all of TypeCheck(Call, Scope), but Callable/MethodName is evaluated in a special scope and the successful return is different.
var Callable = MethodName;
//EXPERIMENTAL - TRY EXTENSION METHOD WHEN WE FAILED TO FIND THE METHOD IN THE TYPE MEMBER SCOPE
if (!typeMemberScope.Contains(Callable.Identifier))
{
var extensionMethodCall = TypeCheck(new Call(Position, MethodName, new[] { Instance }.Concat(Arguments)), scope);
if (extensionMethodCall != null)
return extensionMethodCall;
}
var typeCheckedCallable = TypeCheck(Callable, typeMemberScope);
//END EXPERIMENT
var typeCheckedArguments = TypeCheckAll(Arguments, scope);
if (typeCheckedCallable == null || typeCheckedArguments.Any(x => x == null))
return null;
var calleeType = typeCheckedCallable.Type;
var calleeFunctionType = calleeType as NamedType;
if (calleeFunctionType == null || calleeFunctionType.Name != "System.Func")
{
LogError(CompilerError.ObjectNotCallable(Position));
return null;
}
var returnType = calleeType.InnerTypes.Last();
var argumentTypes = typeCheckedArguments.Select(x => x.Type).ToVector();
if (!Unify(Position, calleeType, NamedType.Function(argumentTypes, returnType)))
return null;
var callType = unifier.Normalize(returnType);
//End of suspiciously duplicated code.
var typedMethodName = (Name)typeCheckedCallable;
return new MethodInvocation(Position, typeCheckedInstance, typedMethodName, typeCheckedArguments, callType);
}
else
{
//HACK: Because TypeRegistry cannot yet look up members for concretions of generic types like int*,
// we have to double-check whether this is an extension method call for a regular built-in generic function.
// Once TypeRegistry lets you look up the members for a type like int*, this block should be removed.
if (scope.Contains(MethodName.Identifier))
{
var typeCheckedCallable = TypeCheck(MethodName, scope);
if (typeCheckedCallable != null)
{
var extensionMethodCall = TypeCheck(new Call(Position, MethodName, new[] { Instance }.Concat(Arguments)), scope);
if (extensionMethodCall != null)
return extensionMethodCall;
}
}
//END HACK
LogError(CompilerError.UndefinedType(Instance.Position, instanceNamedType));
return null;
}
}
public Expression TypeCheck(New @new, Scope scope)
{
var Position = @new.Position;
var TypeName = @new.TypeName;
var typeCheckedTypeName = TypeCheck(TypeName, scope);
if (typeCheckedTypeName == null)
return null;
var typedTypeName = (Name)typeCheckedTypeName;
var constructorType = typedTypeName.Type as NamedType;
if (constructorType == null || constructorType.Name != "Rook.Core.Constructor")
{
LogError(CompilerError.TypeNameExpectedForConstruction(TypeName.Position, TypeName));
return null;
}
var constructedType = constructorType.InnerTypes.Last();
return new New(Position, typedTypeName, constructedType);
}
public Expression TypeCheck(BooleanLiteral booleanLiteral, Scope scope)
{
return booleanLiteral;
}
public Expression TypeCheck(IntegerLiteral integerLiteral, Scope scope)
{
var Position = integerLiteral.Position;
var Digits = integerLiteral.Digits;
int value;
if (int.TryParse(Digits, out value))
return new IntegerLiteral(Position, Digits, NamedType.Integer);
LogError(CompilerError.InvalidConstant(Position, Digits));
return null;
}
public Expression TypeCheck(StringLiteral stringLiteral, Scope scope)
{
return stringLiteral;
}
public Expression TypeCheck(Null nullLiteral, Scope scope)
{
var Position = nullLiteral.Position;
return new Null(Position, NamedType.Nullable(typeVariables.CreateGenericTypeVariable()));
}
public Expression TypeCheck(VectorLiteral vectorLiteral, Scope scope)
{
var Position = vectorLiteral.Position;
var Items = vectorLiteral.Items;
var typeCheckedItems = TypeCheckAll(Items, scope);
if (typeCheckedItems.Any(typeCheckedItem => typeCheckedItem == null))
return null;
var firstItemType = typeCheckedItems.First().Type;
var unificationResults = typeCheckedItems.Select(typedItem => Unify(typedItem.Position, firstItemType, typedItem.Type)).ToArray();
if (unificationResults.Any(x => x == false))
return null;
return new VectorLiteral(Position, typeCheckedItems, NamedType.Vector(firstItemType));
}
private Vector<Expression> TypeCheckAll(Vector<Expression> expressions, Scope scope)
{
return expressions.Select(x => TypeCheck(x, scope)).ToVector();
}
private Vector<Function> TypeCheckAll(Vector<Function> functions, Scope scope)
{
return functions.Select(x => TypeCheck(x, scope)).ToVector();
}
private Vector<Class> TypeCheckAll(Vector<Class> classes, Scope scope)
{
return classes.Select(x => TypeCheck(x, scope)).ToVector();
}
private bool Unify(Position position, DataType typeA, DataType typeB)
{
var errors = unifier.Unify(typeA, typeB).Select(error => new CompilerError(position, error)).ToVector();
var succeeded = !errors.Any();
if (!succeeded)
LogErrors(errors);
return succeeded;
}
private void LogErrors(IEnumerable<CompilerError> errors)
{
errorLog.AddRange(errors);
}
private void LogError(CompilerError error)
{
errorLog.Add(error);
}
}
}