-
Notifications
You must be signed in to change notification settings - Fork 4
/
type.c
387 lines (385 loc) · 11.1 KB
/
type.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <stdio.h>
#include <string.h>
#include "util.h"
#include "symbol.h" /* symbol table data structures */
#include "absyn.h" /* abstract syntax data structures */
#include "type.h" /* function prototype */
void Ty_typeCheckExp(S_table table, A_exp v) {
A_ty type;
switch (v->kind) {
case A_varExp:
//Not used
break;
case A_nilExp:
break;
case A_numberExp:
if(v->dec_type==Ty_int) {printf("Entered int: %s\n",v->u.number);printf("Int: %d\n",IntBaseConverter(v->u.number));}
else {printf("Entered float: %s\n",v->u.number);printf("Float: %f\n",FloatBaseConverter(v->u.number));}
break;
case A_stringExp:
printf("String: %s\n",v->u.stringg);
break;
case A_callExp:
//Not used
break;
case A_opExp:
switch(v->u.op.oper) {
case A_plusOp:
Ty_typeCheckExp(table,v->u.op.left);
Ty_typeCheckExp(table,v->u.op.right);
if(v->u.op.left->dec_type == Ty_int && v->u.op.right->dec_type == Ty_int)
{
v->dec_type = Ty_int;
}
else if(v->u.op.left->dec_type == Ty_float && v->u.op.right->dec_type == Ty_float)
{
v->dec_type = Ty_float;
}
else
{
EM_error(v->pos,"Invalid types of left and right operand for +");
}
break;
case A_minusOp:
Ty_typeCheckExp(table,v->u.op.left);
Ty_typeCheckExp(table,v->u.op.right);
if(v->u.op.left->dec_type == Ty_int && v->u.op.right->dec_type == Ty_int)
{
v->dec_type = Ty_int;
}
else if(v->u.op.left->dec_type == Ty_float && v->u.op.right->dec_type == Ty_float)
{
v->dec_type = Ty_float;
}
else
{
EM_error(v->pos,"Invalid types of left and right operand for -");
}
break;
case A_timesOp:
Ty_typeCheckExp(table,v->u.op.left);
Ty_typeCheckExp(table,v->u.op.right);
if(v->u.op.left->dec_type == Ty_int && v->u.op.right->dec_type == Ty_int)
{
v->dec_type = Ty_int;
}
else if(v->u.op.left->dec_type == Ty_float && v->u.op.right->dec_type == Ty_float)
{
v->dec_type = Ty_float;
}
else
{
EM_error(v->pos,"Invalid types of left and right operand for *");
}
break;
case A_divideOp:
Ty_typeCheckExp(table,v->u.op.left);
Ty_typeCheckExp(table,v->u.op.right);
if(v->u.op.left->dec_type == Ty_int && v->u.op.right->dec_type == Ty_int)
{
v->dec_type = Ty_int;
}
else if(v->u.op.left->dec_type == Ty_float && v->u.op.right->dec_type == Ty_float)
{
v->dec_type = Ty_float;
}
else
{
EM_error(v->pos,"Invalid types of left and right operand for /");
}
break;
case A_eqOp:
Ty_typeCheckExp(table,v->u.op.left);
Ty_typeCheckExp(table,v->u.op.right);
if(v->u.op.left->dec_type == Ty_int && v->u.op.right->dec_type == Ty_int)
{
v->dec_type = Ty_int;
}
else if(v->u.op.left->dec_type == Ty_float && v->u.op.right->dec_type == Ty_float)
{
v->dec_type = Ty_float;
}
else
{
EM_error(v->pos,"Invalid types of left and right operand for =");
}
break;
case A_neqOp:
Ty_typeCheckExp(table,v->u.op.left);
Ty_typeCheckExp(table,v->u.op.right);
if(v->u.op.left->dec_type == Ty_int && v->u.op.right->dec_type == Ty_int)
{
v->dec_type = Ty_int;
}
else if(v->u.op.left->dec_type == Ty_float && v->u.op.right->dec_type == Ty_float)
{
v->dec_type = Ty_float;
}
else
{
EM_error(v->pos,"Invalid types of left and right operand for /=");
}
break;
case A_ltOp:
Ty_typeCheckExp(table,v->u.op.left);
Ty_typeCheckExp(table,v->u.op.right);
if(v->u.op.left->dec_type == Ty_int && v->u.op.right->dec_type == Ty_int)
{
v->dec_type = Ty_int;
}
else if(v->u.op.left->dec_type == Ty_float && v->u.op.right->dec_type == Ty_float)
{
v->dec_type = Ty_float;
}
else
{
EM_error(v->pos,"Invalid types of left and right operand for <");
}
break;
case A_leOp:
Ty_typeCheckExp(table,v->u.op.left);
Ty_typeCheckExp(table,v->u.op.right);
if(v->u.op.left->dec_type == Ty_int && v->u.op.right->dec_type == Ty_int)
{
v->dec_type = Ty_int;
}
else if(v->u.op.left->dec_type == Ty_float && v->u.op.right->dec_type == Ty_float)
{
v->dec_type = Ty_float;
}
else
{
EM_error(v->pos,"Invalid types of left and right operand for <=");
}
break;
case A_gtOp:
Ty_typeCheckExp(table,v->u.op.left);
Ty_typeCheckExp(table,v->u.op.right);
if(v->u.op.left->dec_type == Ty_int && v->u.op.right->dec_type == Ty_int)
{
v->dec_type = Ty_int;
}
else if(v->u.op.left->dec_type == Ty_float && v->u.op.right->dec_type == Ty_float)
{
v->dec_type = Ty_float;
}
else
{
EM_error(v->pos,"Invalid types of left and right operand for >");
}
break;
case A_geOp:
Ty_typeCheckExp(table,v->u.op.left);
Ty_typeCheckExp(table,v->u.op.right);
if(v->u.op.left->dec_type == Ty_int && v->u.op.right->dec_type == Ty_int)
{
v->dec_type = Ty_int;
}
else if(v->u.op.left->dec_type == Ty_float && v->u.op.right->dec_type == Ty_float)
{
v->dec_type = Ty_float;
}
else
{
EM_error(v->pos,"Invalid types of left and right operand for >=");
}
break;
case A_binAndOp:
break;
case A_modOp:
break;
case A_remOp:
Ty_typeCheckExp(table,v->u.op.left);
Ty_typeCheckExp(table,v->u.op.right);
if(v->u.op.left->dec_type == Ty_int && v->u.op.right->dec_type == Ty_int)
{
v->dec_type = Ty_int;
}
else if(v->u.op.left->dec_type == Ty_float && v->u.op.right->dec_type == Ty_float)
{
v->dec_type = Ty_float;
}
else
{
EM_error(v->pos,"Invalid types of left and right operand for rem");
}
break;
case A_tickOp:
case A_andOp:
case A_orOp:
case A_xorOp:
case A_inOp:
case A_notInOp:
case A_rangeOp:
case A_dotOp:
case A_expOp:
break;
default: assert(0);
}
break;
case A_unaryOpExp:
//Do later
break;
case A_recordExp:
//Not Used
break;
case A_seqExp:
Ty_typeCheckExpList(table,v->u.seq);
break;
case A_assignExp:
Ty_typeCheckExp(table,v->u.assign.name);
Ty_typeCheckExp(table,v->u.assign.exp);
if(v->u.assign.name->dec_type == v->u.assign.exp->dec_type)
v->dec_type = v->u.assign.exp->dec_type;
else
{
EM_error(v->pos,"Invalid types in assignment");
break;
}
break;
case A_ifExp:
Ty_typeCheckExpList(table,v->u.iff.cond_clauses);
Ty_typeCheckExpList(table,v->u.iff.elsee);
break;
case A_whileExp:
Ty_typeCheckExp(table,v->u.whilee.test);
break;
case A_forExp:
Ty_typeCheckExp(table,v->u.forr.var);
Ty_typeCheckExp(table,v->u.forr.reverse);
Ty_typeCheckExp(table,v->u.forr.range);
break;
case A_loopExp:
Ty_typeCheckExp(table,v->u.loop.labelopt);
Ty_typeCheckExp(table,v->u.loop.iteration);
Ty_typeCheckExpList(table,v->u.loop.basicloop);
Ty_typeCheckExp(table,v->u.loop.idopt);
break;
case A_condExp:
Ty_typeCheckExp(table,v->u.cond.test);
Ty_typeCheckExpList(table,v->u.cond.stmts);
break;
case A_breakExp:
//Nothing here
break;
case A_exitExp:
Ty_typeCheckExp(table,v->u.exit.exitname);
Ty_typeCheckExp(table,v->u.exit.exitcondition);
break;
case A_letExp:
//Not used
break;
case A_arrayExp:
//Not used
break;
case A_returnExp:
Ty_typeCheckExp(table,v->u.retval);
break;
case A_gotoExp:
Ty_typeCheckExp(table,v->u.gotolabel);
break;
case A_enumExp:
Ty_typeCheckExpList(table,v->u.enumids);
break;
case A_intdefExp:
Ty_typeCheckExp(table,v->u.intdef);
break;
case A_floatdefExp:
Ty_typeCheckExp(table,v->u.floatt.numdigits);
Ty_typeCheckExp(table,v->u.floatt.rangeopt);
break;
case A_fixeddefExp:
Ty_typeCheckExp(table,v->u.fixed.delta);
Ty_typeCheckExp(table,v->u.fixed.range);
break;
case A_fixeddefdigitExp:
Ty_typeCheckExp(table,v->u.fixeddig.delta);
Ty_typeCheckExp(table,v->u.fixeddig.numdigits);
Ty_typeCheckExp(table,v->u.fixeddig.rangeopt);
break;
case A_unconarraydefExp:
Ty_typeCheckExpList(table,v->u.unconarray.indexs);
Ty_typeCheckExp(table,v->u.unconarray.subtypeind);
break;
case A_conarraydefExp:
Ty_typeCheckExpList(table,v->u.conarray.iterindex);
Ty_typeCheckExp(table,v->u.conarray.subtypeind);
break;
case A_nullrecorddefExp:
//Nothing here
break;
case A_recorddefExp:
Ty_typeCheckExpList(table,v->u.recorddef.pragmas);
Ty_typeCheckExp(table,v->u.recorddef.complist);
break;
case A_pragma:
//Nothing here
break;
case A_pragmalist:
Ty_typeCheckExp(table,v->u.pragmalist.name);
Ty_typeCheckExpList(table,v->u.pragmalist.pragmaargs);
break;
case A_alternative:
Ty_typeCheckExpList(table,v->u.alternative.choices);
Ty_typeCheckExpList(table,v->u.alternative.stmts);
break;
case A_caseExp:
Ty_typeCheckExp(table,v->u.caseexp.header);
Ty_typeCheckExpList(table,v->u.caseexp.pragmas);
Ty_typeCheckExpList(table,v->u.caseexp.alternatives);
break;
case A_raiseExp:
Ty_typeCheckExp(table,v->u.nameopt);
break;
case A_procedure:
Ty_typeCheckExp(table,v->u.procedurename);
break;
case A_functionUse:
Ty_typeCheckExp(table,v->u.functionuse.name);
Ty_typeCheckExpList(table,v->u.functionuse.value);
break;
case A_compAssoc:
Ty_typeCheckExpList(table,v->u.compassoc.choices);
Ty_typeCheckExp(table,v->u.compassoc.expression);
break;
case A_compUnit:
Ty_typeCheckExpList(table,v->u.compunit.contextspec);
Ty_typeCheckExp(table,v->u.compunit.privatee);
Ty_typeCheckExp(table,v->u.compunit.unit);
Ty_typeCheckExpList(table,v->u.compunit.pragmas);
break;
case A_useclause:
Ty_typeCheckExp(table,v->u.useclause.type);
Ty_typeCheckExpList(table,v->u.useclause.names);
break;
case A_contextSpecwith:
Ty_typeCheckExpList(table,v->u.contextspecwith.withclause);
Ty_typeCheckExpList(table,v->u.contextspecwith.useclauseopt);
break;
case A_subprogSpec:
Ty_typeCheckExp(table,v->u.subprogSpec.name);
Ty_typeCheckExpList(table,v->u.subprogSpec.formalpart);
break;
case A_nameConstr:
Ty_typeCheckExp(table,v->u.nameconstr.name);
Ty_typeCheckExp(table,v->u.nameconstr.constraint);
break;
case A_decimalConstr:
Ty_typeCheckExp(table,v->u.decimalconstr.expression);
Ty_typeCheckExp(table,v->u.decimalconstr.rangeopt);
break;
case A_notImplemented:
break;
default:
assert(0);
}
}
void Ty_typeCheckExpList(S_table table, A_expList l)
{
A_expList e = l;
while(e!=NULL)
{
Ty_typeCheckExp(table,e->head);
e = e -> tail;
}
}