-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeChecker.cs
392 lines (290 loc) · 11.4 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace Compiler
{
class TypeChecker
{
public Environment GenerateGlobalEnvironment(List<MethodNode> lmnode)
{
Environment env = new Environment();
foreach (MethodNode mnode in lmnode)
{
string name = mnode.name;
List<type> ltype = mnode.inputs.Select(x => TypeNodeToType(x.t, env)).ToList();
env.Add(name, new FuncType(ltype, TypeNodeToType(mnode.t, env)));
}
return env;
}
public Environment GenerateMethodEnvironment(MethodNode methodNode, Environment env)
{
Environment newEnv = env.Duplicate();
methodNode.inputs.ForEach(x => newEnv.Add(x.name, TypeNodeToType(x.t, newEnv)));
return newEnv;
}
public bool IntermediateReturn(List<Expression> lmn)
{
for(int i=0;i<lmn.Count-1;i++)
{
if(lmn[i] is ReturnExpression)
{
return true;
}
}
return false;
}
public bool ValidReturnType(Expression expression, type lastType, type outputType, Environment env)
{
bool isVoid = outputType is Void;
bool endsWithReturn = expression is ReturnExpression;
if (isVoid && endsWithReturn)
throw new Exception("Void methods must not end with a return statement.");
if (!isVoid && !endsWithReturn)
throw new Exception("Non-void method must end with a return statement.");
if (!isVoid && !ExpressionMatches(outputType, lastType))
throw new Exception("The return expression must be of the same type as the input.");
return true;
}
public bool TypeCheckMethods(List<MethodNode> lmnode)
{
Environment env = GenerateGlobalEnvironment(lmnode);
foreach (MethodNode mnode in lmnode)
{
type outputType = TypeNodeToType(mnode.t, env);
Environment newEnv = GenerateMethodEnvironment(mnode,env);
if(IntermediateReturn(mnode.body))
throw new Exception("only last Expression can be a return statement");
Expression lastExpression = null;
type LastType = null;
foreach (Expression expr in mnode.body)
{
lastExpression = expr;
LastType = GetType(expr, newEnv);
}
ValidReturnType(lastExpression, LastType, outputType, env);
}
return true;
}
public bool IsFixedNumericType(type t)
{
return (t is FixedType ft) && (ft.simpleType is NumericType);
}
public bool IsFixedInt(type t)
{
return t is FixedType ft && ft.simpleType is IntType;
}
public bool ExpressionMatches(type t1, type t2)
{
if (t1 is ArrayType at1 && t2 is ArrayType at2)
return AssignementMatches(at1.variableType, at2.variableType);
if (t1 is FixedType ft)
return ExpressionMatches(ft.simpleType, t2);
if (t2 is FixedType ft2)
return ExpressionMatches(t1, ft2.simpleType);
if(t1 is PrimitiveType pt1 && t2 is PrimitiveType pt2)
{
return pt1.name == pt2.name;
}
return false;
}
public bool AssignementMatches(type t1, type t2)
{
if (t1 is FixedType && ! (t2 is FixedType))
throw new Exception("Cannot asign a non fixed value to a fixed variable");
return ExpressionMatches(t1,t2);
}
public type GetType(VariableNode vnode, Environment env)
{
string name = vnode.t;
return env.GetType(name);
}
public bool VerifyAssignement(SimpleAssignement assign, Environment env)
{
type t1 = env.GetType(assign.name);
type t2 = GetType(assign.tree,env);
return AssignementMatches(t1, t2);
}
public bool VerifyInitialization(GrammarNode gnode, Environment env)
{
Initialization initialization = (Initialization)gnode;
type t1 = TypeNodeToType(initialization.type, env);
type t2 = GetType(initialization.tree, env);
if (AssignementMatches(t1, t2))
{
env.Add(initialization.name,t1);
return true;
}
return false;
}
public type GetType(FuncNode fnode, Environment env)
{
FuncType funcType = (FuncType)env.GetType(fnode.identifier);
List<ITreeNode> lexp = fnode.arguments;
List<type> ltype = funcType.inputTypes;
if (lexp.Count != ltype.Count)
throw new Exception("wrong number of arguments to function" + fnode.identifier);
for(int i=0; i<ltype.Count;i++)
{
if (!ExpressionMatches(GetType(lexp[i],env), ltype[i]))
throw new Exception(lexp[i].ToString() + "is not of type " + ltype[i]);
}
return funcType.outputType;
}
public type GetType(ArrayNode anode, Environment env)
{
type t1 = GetType(anode.index, env);
if (!IsFixedNumericType(t1))
{
throw new Exception("Array lookup for " + anode.identifier + "is not a fixed int.");
}
ArrayType atype = (ArrayType)env.GetType(anode.identifier);
return atype.variableType;
}
public type GetType(BinaryOperationNode bon, Environment env)
{
type t1 = GetType(bon.left, env);
type t2 = GetType(bon.right, env);
if (bon.operation == "==")
{
return new BoolType();
}
if (ExpressionMatches(t1, t2))
{
if (t1 is FixedType && !(t2 is FixedType))
return t2;
return t1;
}
throw new Exception("type mismatch" + bon.ToString());
}
public type GetType(CastNode cnode, Environment env)
{
type t1 = TypeNodeToType(cnode.primitive, env);
type t2 = GetType(cnode.tnode,env);
bool b = t1 is NumericType && t2 is NumericType;
if (!b)
throw new Exception("Only numeric types can be casted");
return t1;
}
public type GetType(GrammarNode gnode, Environment env)
{
switch (gnode)
{
case IntegerNode inode: return new FixedType(new IntType());
case BoolNode bnode: return new FixedType(new BoolType());
case ByteNode bnode: return new FixedType(new ByteType());
case FuncNode fnode: return GetType(fnode, env);
case VariableNode vnode : return GetType(vnode, env);
case ArrayNode anode : return GetType(anode, env);
case BinaryOperationNode bon: return GetType(bon, env);
case ReturnExpression rexp: return GetType(rexp.node, env);
case CastNode cnode: return GetType(cnode, env);
case ArrayCreationNode anode: return GetType(anode, env);
default: break;
}
bool b1 = gnode is Initialization && VerifyInitialization((Initialization) gnode, env);
bool b2 = gnode is SimpleAssignement && VerifyAssignement((SimpleAssignement) gnode, env);
bool b3 = gnode is ArrayAssign && VerifyArrayAssignement((ArrayAssign) gnode, env);
bool b4 = gnode is LoopNode lnode && VerifyLoopNode(lnode, env);
bool b5 = gnode is DeclarationNode dnode && VerifyDeclarationNode(dnode,env);
if (b1 || b2 || b3 || b4 || b5)
return new Void();
throw new Exception(gnode + " does not type check.");
}
public bool VerifyDeclarationNode(DeclarationNode dnode, Environment env)
{
type t = TypeNodeToType(dnode.t,env);
env.Add(dnode.name, t);
return true;
}
public type GetType(ArrayCreationNode arrayCreationNode, Environment env)
{
if (!IsFixedNumericType(GetType(arrayCreationNode.size, env)))
throw new Exception("array length must be of fixed numeric size");
var type = TypeNodeToType(arrayCreationNode.primitiveTypeNode, env);
return new ArrayType((VariableType)type);
}
private bool VerifyLoopNode(LoopNode lnode, Environment env)
{
Func<GrammarNode, bool> f = gnode => !IsFixedInt(GetType(gnode, env));
string name = lnode.name;
type t = TypeNodeToType(lnode.typeNode, env);
List<Expression> lexp = lnode.lexpr;
Environment newEnv = env.Duplicate();
newEnv.Add(name, new FixedType(new IntType()));
if (f(lnode.init) || f(lnode.times))
return false;
foreach (Expression exp in lexp)
{
GetType(exp, newEnv);
}
return true;
}
private bool VerifyArrayAssignement(ArrayAssign assign, Environment env)
{
type t1 = GetType(assign.index, env);
if (!IsFixedInt(t1))
return false;
ArrayType atype = (ArrayType) env.GetType(assign.name);
return AssignementMatches(atype.variableType, GetType(assign.tree,env));
}
public type TypeNodeToType(TypeNode itype, Environment env)
{
if (itype is PrimitiveTypeNode p)
{
switch (p.type)
{
case "int": return new IntType();
case "bool": return new BoolType();
case "byte": return new ByteType();
}
}
switch (itype)
{
case FixedTypeNode ftn: return new FixedType((SimpleType)TypeNodeToType(ftn.simpleTypeNode,env));
case ArrayTypeNode atn: return new ArrayType((VariableType)TypeNodeToType(atn.type, env));
case VoidNode vn: return new Void();
}
throw new InvalidOperationException();
}
}
class Environment
{
Dictionary<string, type> dict;
public Environment()
{
dict = new Dictionary<string, type>();
}
public bool Undefined(string var)
{
return !dict.ContainsKey(var);
}
public type GetType(string name)
{
if (!dict.ContainsKey(name))
throw new Exception("variable " + name + " is undefined. ");
return dict[name];
}
public Environment Duplicate()
{
Environment clone = new Environment();
foreach(string str in dict.Keys)
{
clone.Add(str, dict[str]);
}
return clone;
}
public void Add(string str, type t)
{
if (dict.ContainsKey(str))
throw new Exception("variable " + str + " is already defined.");
dict.Add(str, t);
}
public void Remove(string str)
{
dict.Remove(str);
}
}
}