-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_check.c
368 lines (306 loc) · 8.79 KB
/
type_check.c
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
#include <stdlib.h>
#include <stdio.h>
#include "type_check.h"
#include "type.h"
#include "stmt.h"
#include "scope.h"
#include "hash_table.h"
#include "param_list.h"
extern int error_count;
static type* curr_function_type = NULL; //this is the currently typechecked function
static const char* curr_function_name = NULL;
extern struct hash_table* fundecls; //this is the table with function declarations
type* expr_typecheck(expr *e) {
if (e == NULL) return NULL;
type *lt = expr_typecheck(e->left);
type *rt = expr_typecheck(e->right);
type *result = NULL;
switch (e->kind) {
case EXPR_NAME:
//a bit arbitrary - if label is unknown then it is of void type
if (e->symbol == NULL) result = type_create(TYPE_VOID, NULL, NULL);
else result = type_copy(e->symbol->type);
break;
case EXPR_STR:
result = type_create(TYPE_STRING, NULL, NULL);
break;
case EXPR_INT:
result = type_create(TYPE_INTEGER, NULL, NULL);
break;
case EXPR_CHAR:
result = type_create(TYPE_CHARACTER, NULL, NULL);
break;
case EXPR_BOOL:
result = type_create(TYPE_BOOLEAN, NULL, NULL);
break;
case EXPR_ARR_SUBS:
if (lt->kind == TYPE_ARRAY) {
if (rt->kind != TYPE_INTEGER) {
printf("Error: non-integer index used when accessing array %s.\n", e->left->name);
++error_count;
}
result = type_copy(lt->subtype);
} else {
printf("Error: %s is not an array.\n", e->left->name);
++error_count;
result = type_copy(lt);
}
break;
case EXPR_FUN_CALL: {
const char* fname = NULL;
if (e->left != NULL && e->left->name != NULL) fname = e->left->name;
if (lt->kind != TYPE_FUNCTION) {
printf("Error: unknown function %s.\n", fname);
++error_count;
result = type_copy(lt);
break;
}
result = type_copy(lt->subtype);
if (lt->kind != TYPE_FUNCTION) {
printf("Error: unknown function %s.\n", fname);
++error_count;
break;
}
expr* arg = e->right;
type* argtype = NULL;
type* partype = NULL;
param_list* declared_param = lt->params;
while (arg != NULL) {
if (declared_param == NULL) {
printf("Error: too many arguments in call to %s.\n", fname);
++error_count;
break;
}
argtype = expr_typecheck(arg);
partype = declared_param->type;
if (!type_equals(argtype, partype)) {
printf("Error: parameter type mismatch in call to %s.\n", fname);
++error_count;
}
type_delete(argtype);
arg = arg->right;
declared_param = declared_param->next;
}
if (declared_param != NULL) {
printf("Error: too few arguments in call to %s.\n", fname);
++error_count;
}}
break;
case EXPR_EXP:
case EXPR_MUL:
case EXPR_DIV:
case EXPR_MOD:
case EXPR_ADD:
case EXPR_SUB:
if (lt->kind != TYPE_INTEGER || rt->kind != TYPE_INTEGER) {
printf("Error: arithmetic operation on non-integers.\n");
++error_count;
}
result = type_create(TYPE_INTEGER, NULL, NULL);
break;
case EXPR_UN_MIN:
case EXPR_INC:
case EXPR_DEC:
if (lt->kind != TYPE_INTEGER) {
printf("Error: (unary) arithmetic operation on non-integers.\n");
++error_count;
}
result = type_create(TYPE_INTEGER, NULL, NULL);
break;
case EXPR_LE:
case EXPR_LT:
case EXPR_GT:
case EXPR_GE:
if (lt->kind != TYPE_INTEGER || rt->kind != TYPE_INTEGER) {
printf("Error: arithmetic comparisons on non-integers.\n");
++error_count;
}
result = type_create(TYPE_BOOLEAN, NULL, NULL);
break;
case EXPR_EQ:
case EXPR_NEQ:
if (!type_equals(lt,rt)) {
printf("Error: comparing objects of different types.\n");
++error_count;
}
if (lt->kind==TYPE_VOID || lt->kind==TYPE_ARRAY || lt->kind==TYPE_FUNCTION) {
printf("Error: void/array/function type in comparison.\n");
++error_count;
}
result = type_create(TYPE_BOOLEAN, NULL, NULL);
break;
case EXPR_AND:
case EXPR_OR:
if (lt->kind != TYPE_BOOLEAN || rt->kind != TYPE_BOOLEAN) {
printf("Error: boolean operation on non-bools.\n");
++error_count;
}
result = type_create(TYPE_BOOLEAN, NULL, NULL);
break;
case EXPR_NEG:
if (lt->kind != TYPE_BOOLEAN) {
printf("Error: (unary) boolean operation on non-bools.\n");
++error_count;
}
result = type_create(TYPE_BOOLEAN, NULL, NULL);
break;
case EXPR_ARG:
result = type_copy(lt);
break;
case EXPR_ASSGN:
result = type_copy(lt);
if (e->left->kind != EXPR_NAME && e->left->kind != EXPR_ARR_SUBS) {
printf("Error: assignment to non-variable.\n");
++error_count;
} else {
const char* varname = NULL;
if (e->left->kind == EXPR_ARR_SUBS) varname = e->left->left->name;
else varname = e->left->name;
//detecting arrays in assignemnts, not pretty, should be done in AST
if (e->right->kind == EXPR_ARG) {
printf("Error: can't assign array to %s.\n", varname);
++error_count;
break;
}
if (!type_equals(lt,rt)) {
printf("Error: assigning object of wrong type to %s.\n", varname);
++error_count;
}
}
break;
}
type_delete(lt);
type_delete(rt);
return result;
}
void decl_typecheck(decl *d) {
if (d == NULL) return;
if (d->value != NULL) {//typecheck var decl
//typecheck array (again - it'd be better to deal with array detection in AST)
if (d->value->kind == EXPR_ARG) {
if (d->type->kind == TYPE_ARRAY) {
array_typecheck(d->value, d->type->subtype, d->name);
} else {
printf("Error: can't assign array to %s.\n", d->name);
++error_count;
}
}
else if (d->type->kind == TYPE_ARRAY) {
printf("Error: can't assign non-array init value to %s.\n", d->name);
++error_count;
}
else {//typecheck vars
type *t = expr_typecheck(d->value);
if (!type_equals(t, d->symbol->type)) {
printf("Error: mismatch in initialisation of var %s.\n", d->name);
++error_count;
}
type_delete(t);
}
} else {
//typecheck function declaration
//bind function type to name
type* registered_fun_type = hash_table_lookup(fundecls, d->name);
if (registered_fun_type != NULL) {
if (!type_equals(registered_fun_type, d->type)) {
printf("Error: error in defn/redeclaration of fun %s.\n", d->name);
++error_count;
}
}
else hash_table_insert(fundecls, d->name, d->type);
}
if (d->code != NULL) {//typecheck function definition
curr_function_type = type_copy(d->type->subtype);
curr_function_name = d->name;
stmt_typecheck(d->code);
curr_function_type = NULL;
type_delete(curr_function_type);
}
if (d->next != NULL) decl_typecheck(d->next);
}
void array_typecheck(expr *e, type* itemtype, const char* arrname) {
while (e != NULL) {
if (e->kind != EXPR_ARG) {
printf("Error: %s is not an array.\n", arrname);
++error_count;
return;
}
type* curritemtype = expr_typecheck(e->left);
if (!type_equals(itemtype, curritemtype)) {
printf("Error: array %s type init mismatch.\n", arrname);
++error_count;
}
e = e->right;
type_delete(curritemtype);
}
}
void stmt_typecheck(stmt *s) {
if (s == NULL) return;
type *t = NULL;
expr* e = NULL;
switch(s->kind) {
case STMT_DECL:
decl_typecheck(s->decl);
break;
case STMT_EXPR:
t = expr_typecheck(s->expr);
type_delete(t);
break;
case STMT_IF_ELSE:
t = expr_typecheck(s->expr);
if (t->kind != TYPE_BOOLEAN) {
printf("Error: nonboolean condition in if-else loop.\n");
++error_count;
}
type_delete(t);
stmt_typecheck(s->body);
stmt_typecheck(s->else_body);
break;
case STMT_FOR:
t = expr_typecheck(s->init_expr);
type_delete(t);
t = expr_typecheck(s->expr);
if (t != NULL && t->kind != TYPE_BOOLEAN) {
printf("Error: nonboolean condition in for loop.\n");
++error_count;
}
type_delete(t);
t = expr_typecheck(s->next_expr);
type_delete(t);
stmt_typecheck(s->body);
break;
case STMT_PRINT:
e = s->expr;
while (e != NULL) {
t = expr_typecheck(e->left);
type_delete(t);
e = e->right;
}
break;
case STMT_RETURN:
if (s->expr == NULL) {
if (curr_function_type->kind != TYPE_VOID) {
printf("Error: void return in non-void function.\n");
++error_count;
return;
}
return;
}
if (curr_function_type->kind == TYPE_VOID) {
printf("Error: non-void return in void function.\n");
++error_count;
return;
}
t = expr_typecheck(s->expr);
if (!type_equals(curr_function_type, t)){
printf("Error: return type mismatch in %s.\n", curr_function_name);
++error_count;
}
type_delete(t);
break;
case STMT_BLOCK:
stmt_typecheck(s->body);
break;
}
stmt_typecheck(s->next);
}