forked from coolsidd/Compiler-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assign_helpers.c
365 lines (348 loc) · 15.6 KB
/
assign_helpers.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
/*
Group 36
2017B4A70495P Manan Soni
2017B4A70549P Siddharth Singh
2017B4A70636P Nayan Khanna
2017B4A70740P Aditya Tulsyan
*/
#include "assign_helpers.h"
// <fact> -> <var> | LPAREN <arithmeticexpr> RPAREN
type_expression *get_type_of_fact(type_exp_table *txp_table, Parse_tree_node* p){
if(p->child->tok->lexeme.s == var){
return get_type_of_var(txp_table, p->child);
}
else if(p->last_child->tok->lexeme.s == RPAREN){
return get_type_of_arithm_expr(txp_table, getNodeFromIndex(p->child, 1));
}
else{
return NULL;
}
}
// <arithmeticexpr>-><term> PLUS <arithmeticexpr> | <term> MINUS <arithmeticexpr> | <term> OR <arithmeticexpr> | <term>
type_expression *get_type_of_arithm_expr(type_exp_table* txp_table, Parse_tree_node* p){
type_expression* txp = get_type_of_term(txp_table, p->child);
if(p->last_child->tok->lexeme.s == term){
return txp;
}
else if(p->last_child->tok->lexeme.s == arithmeticexpr){
type_expression* txp_1 = get_type_of_arithm_expr(txp_table, p->last_child);
char* t1 = str_type(txp);
char* t2 = str_type(txp_1);
char* lexeme1 = "term";
char* lexeme2 = "arithmeticexpr";
if (!(txp && txp_1))
return NULL;
switch(getNodeFromIndex(p->child, 1)->tok->lexeme.s){
case (OR):
{
bool flag = true;
flag &= assert_debug(txp_1->variable_type == PRIMITIVE_TYPE && txp->variable_type == PRIMITIVE_TYPE,
"OR with Non-Boolean Operands", p, t1, t2, "OR", lexeme1, lexeme2);
if(flag){
flag &= assert_debug(txp_1->union_to_be_named.primitive_data == t_BOOLEAN &&
txp->union_to_be_named.primitive_data == t_BOOLEAN,"OR with Non-Boolean Operands",
p, t1, t2, "OR", lexeme1, lexeme2);
}
return get_bool_type();
break;
}
default:
{
char *operator= toStringSymbol(getNodeFromIndex(p->child, 1)->tok->lexeme);
bool flag = true;
flag &= assert_debug(txp_1->variable_type == txp->variable_type,
"Different Left & Right operand",
p, t1, t2, operator, lexeme1, lexeme2);
if (!flag)
return txp;
switch(txp_1->variable_type){
case(PRIMITIVE_TYPE):
{
flag &= assert_debug(txp_1->union_to_be_named.primitive_data ==
txp->union_to_be_named.primitive_data,"Different Left & Right Operand",
p, t1, t2, operator, lexeme1, lexeme2);
flag &= assert_debug(txp_1->union_to_be_named.primitive_data!=t_BOOLEAN,
"ADD or SUB with Bool Operand",
p, t1, t2, operator, lexeme1, lexeme2);
if(flag && txp_1->union_to_be_named.primitive_data==t_INTEGER){
return get_integer_type();
}
else if(flag && txp_1->union_to_be_named.primitive_data==t_REAL){
return get_real_type();
}
else{
return txp; // TODO Default type is of LHS
}
break;
}
case(RECT_ARRAY):
{
bool flag = check_rect_dimensions(txp->union_to_be_named.rect_array_data,
txp_1->union_to_be_named.rect_array_data, p, t1, t2, operator,
lexeme1, lexeme2);
return txp; // TODO Default type if of LHS
break;
}
case(JAGGED_ARRAY):
{
bool flag = check_jagged_dimensions(txp->union_to_be_named.jagged_array_data,
txp_1->union_to_be_named.jagged_array_data, p, t1, t2, operator,
lexeme1, lexeme2);
return txp; // TODO Default type if of LHS
break;
}
}
}
}
}
else{
return NULL;
}
}
// TODO Return txp as done above (get_type_of_arithexp)
type_expression* get_type_of_term(type_exp_table* txp_table, Parse_tree_node* p){
type_expression *txp = get_type_of_fact(txp_table, p->child);
if (p->last_child->tok->lexeme.s == fact)
{
return txp;
}
else if (p->last_child->tok->lexeme.s == term)
{
type_expression *txp_1 = get_type_of_term(txp_table, p->last_child);
char *t1 = str_type(txp);
char *t2 = str_type(txp_1);
char *lexeme1 = "fact";
char *lexeme2 = "term";
if(!(txp && txp_1))
return NULL;
switch (getNodeFromIndex(p->child, 1)->tok->lexeme.s)
{
case (AND):
{
bool flag = true;
flag &= assert_debug(txp_1->variable_type == PRIMITIVE_TYPE && txp->variable_type == PRIMITIVE_TYPE,
"AND with Non-Bool Operand",
p, t1, t2, "AND", lexeme1, lexeme2);
if(flag){
flag &= assert_debug(txp_1->union_to_be_named.primitive_data == t_BOOLEAN &&
txp->union_to_be_named.primitive_data == t_BOOLEAN,
"AND with Non-Bool Operand",
p, t1, t2, "AND", lexeme1, lexeme2);
}
return get_bool_type();
break;
}
case (MULT):
{
bool flag = true;
flag &= assert_debug(txp_1->variable_type == txp->variable_type,
"Different Left & Right Operand", p,
t1, t2, "MULT", lexeme1, lexeme2);
if(!flag) return txp;
switch (txp_1->variable_type)
{
case (PRIMITIVE_TYPE):
{
flag &= assert_debug(txp_1->union_to_be_named.primitive_data ==
txp->union_to_be_named.primitive_data,
"Different Left & Right Operand",
p, t1, t2, "MULT", lexeme1, lexeme2);
flag &= assert_debug(txp_1->union_to_be_named.primitive_data != t_BOOLEAN,
"MULT with Bool Operand",
p, t1, t2, "MULT", lexeme1, lexeme2);
if (flag && txp_1->union_to_be_named.primitive_data == t_INTEGER){
return get_integer_type();
}
else if (flag && txp_1->union_to_be_named.primitive_data == t_REAL){
return get_real_type();
}
else{
return txp; // TODO Default type is of LHS
}
break;
}
case (RECT_ARRAY):
{
bool flag = check_rect_dimensions(txp->union_to_be_named.rect_array_data,
txp_1->union_to_be_named.rect_array_data, p,
t1, t2, "MULT", lexeme1, lexeme2);
return txp; // TODO Default type is of LHS
break;
}
case (JAGGED_ARRAY):
{
bool flag = check_jagged_dimensions(txp->union_to_be_named.jagged_array_data,
txp_1->union_to_be_named.jagged_array_data, p,
t1, t2, "MULT", lexeme1, lexeme2);
return txp; // TODO Default type is of LHS
break;
}
}
}
case (DIV):
{
bool flag = true;
flag &= assert_debug(txp_1->variable_type == txp->variable_type,
"Different Left & Right Operand",
p, t1, t2, "DIV", lexeme1, lexeme2);
if (!flag)
return txp;
switch (txp_1->variable_type)
{
case (PRIMITIVE_TYPE):
{
flag &= assert_debug(txp_1->union_to_be_named.primitive_data ==
txp->union_to_be_named.primitive_data,
"Different Left & Right Operand",
p, t1, t2, "DIV", lexeme1, lexeme2);
flag &= assert_debug(txp_1->union_to_be_named.primitive_data != t_BOOLEAN,
"DIV of Bool Operands",
p, t1, t2, "DIV", lexeme1, lexeme2);
if (flag && (txp_1->union_to_be_named.primitive_data == t_INTEGER ||
txp_1->union_to_be_named.primitive_data == t_REAL))
return get_real_type();
else
return txp; // TODO Default type is of LHS
break;
}
case (RECT_ARRAY):
{
bool flag = check_rect_dimensions(txp->union_to_be_named.rect_array_data,
txp_1->union_to_be_named.rect_array_data, p,
t1, t2, "DIV", lexeme1, lexeme2);
return txp; // TODO Default type is of LHS
break;
}
case (JAGGED_ARRAY):
{
bool flag = check_jagged_dimensions(txp->union_to_be_named.jagged_array_data,
txp_1->union_to_be_named.jagged_array_data, p,
t1, t2, "DIV", lexeme1, lexeme2);
return txp; // TODO Default type is of LHS
break;
}
}
}
}
}
else
{
return NULL;
}
return NULL;
}
type_expression* get_type_exp_of_expr(type_exp_table* txp_table, Parse_tree_node* p){
return get_type_of_arithm_expr(txp_table, p->child);
}
type_expression* get_type_of_var_lhs(type_exp_table* txp_table, Parse_tree_node* p){
type_expression *txp = (type_expression*) calloc(1, sizeof(type_expression));
if (p->last_child->tok->lexeme.s == SQBC)
{
type_expression *txp = get_type_of_var(txp_table, getNodeFromIndex(p->child, 2)->child);
if(!assert_debug(txp!=NULL, "Var used before Declaration", p, "***", "***", "***", "***", "***"))
return NULL;
linked_list *bounds = get_type_of_index_list(txp_table, getNodeFromIndex(p->child, 2));
bool flag = do_bound_checking(txp_table, p->child, bounds);
return get_integer_type();
}
else if (p->last_child->tok->lexeme.s == ID)
{
txp = get_type_expression(txp_table, p->last_child->tok->token_name);
bool flag = assert_debug(txp != NULL, "Var used before Declaration", p, "***", "***", "***", "***", "***");
return txp;
}
return NULL;
}
bool check_rect_dimensions(rect_array_type r1, rect_array_type r2, Parse_tree_node* p,
char *t1, char *t2, char *operator, char *lexeme1, char *lexeme2)
{
bool flag = true;
flag &= assert_debug(r1.dimensions == r2.dimensions, "RectArray Dimensions Mismatch",
p, t1, t2, operator, lexeme1, lexeme2);
if(!flag)
return flag;
else{
ll_node* temp_1 = r2.array_ranges->head;
for (ll_node *temp = r1.array_ranges->head;temp;temp=temp->next){
rect_array_range range1 = *(rect_array_range*)(temp->data);
rect_array_range range2 = *(rect_array_range*)(temp_1->data);
flag &= assert_debug(range1.upper_bound - range1.lower_bound ==
range2.upper_bound - range2.lower_bound,
"RectArray Dimensions Mismatch",
p, t1, t2, operator, lexeme1, lexeme2);
temp_1 = temp_1->next;
if(!flag)
break;
}
}
return flag;
}
bool check_jagged_dimensions(jagged_array_type j1, jagged_array_type j2, Parse_tree_node* p,
char* t1, char* t2, char* operator, char* lexeme1, char* lexeme2){
bool flag = true;
flag &= assert_debug(j1.dimensions == j2.dimensions,
"JgdArray Dimensions Mismatch",
p, t1, t2, operator, lexeme1, lexeme2);
if(!flag){
return flag;
}
switch(j1.dimensions)
{
case 2:
{
jagged_2d temp = j1.array_type.j2d;
jagged_2d temp_1 = j2.array_type.j2d;
flag &= assert_debug(temp.upper_bound - temp.lower_bound ==
temp_1.upper_bound - temp_1.lower_bound,
"JgdArray Dimensions Mismatch",
p, t1, t2, operator, lexeme1, lexeme2);
if (!flag)
{
return flag;
}
ll_node* j2_sizes = temp.row_sizes.sizes->head;
for (ll_node *j1_sizes = temp.row_sizes.sizes->head; j1_sizes; j1_sizes = j1_sizes->next){
flag &= assert_debug(*((int *)(j1_sizes->data)) == *((int *)(j2_sizes->data)),
"JgdArray Dimensions Mismatch",
p, t1, t2, operator, lexeme1, lexeme2);
j2_sizes = j2_sizes->next;
if(!flag)
break;
}
return flag;
break;
}
case 3:
{
jagged_3d temp = j1.array_type.j3d;
jagged_3d temp_1 = j2.array_type.j3d;
flag &= assert_debug(temp.upper_bound - temp.lower_bound ==
temp_1.upper_bound - temp_1.lower_bound,
"JgdArray Dimensions Mismatch",
p, t1, t2, operator, lexeme1, lexeme2);
if (!flag)
{
return flag;
}
ll_node *j1_sizes = temp.row_sizes->head;
for (ll_node *j2_sizes = temp.row_sizes->head; j2_sizes; j2_sizes = j2_sizes->next)
{
ll_node* ll_t1 = ((linked_list*)j2_sizes->data)->head;
for (ll_node *ll_t2 = ((linked_list *)j2_sizes->data)->head; ll_t2; ll_t2=ll_t2->next)
{
flag &= assert_debug(*((int *)(ll_t1->data)) == *((int *)(ll_t2->data)),
"JgdArray Dimensions Mismatch",
p, t1, t2, operator, lexeme1, lexeme2);
if (!flag)
break;
ll_t1 = ll_t1->next;
}
if(!flag)
break;
j1_sizes = j1_sizes->next;
}
return flag;
break;
}
}
}