-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeCheck.c
237 lines (184 loc) · 6.54 KB
/
typeCheck.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
// Group Number: 31
// MADHUR PANWAR 2016B4A70933P
// TUSSANK GUPTA 2016B3A70528P
// SALMAAN SHAHID 2016B4A70580P
// APURV BAJAJ 2016B3A70549P
// HASAN NAQVI 2016B5A70452P
#include "symbolTableDef.h"
#include "symbolTable.h"
#include "astDef.h"
#include "error.h"
#include <stdio.h>
#include <stdlib.h>
#include "typeCheck.h"
#include "symbolTable.h"
bool rhsBoundsCheckIfStatic(ASTNode *idNode, ASTNode *idOrNumNode) {
symTableNode *arrinfoEntry = idNode->stNode ;
symVarInfo *arrinfo = &(arrinfoEntry->info.var);
if((arrinfo->vtype).vaType == STAT_ARR && idOrNumNode->gs == g_NUM) {
int idx = (idOrNumNode->tkinfo->value).num;
if(!((idx >= (arrinfo->vtype).si.vt_num) && (idx <= (arrinfo->vtype).ei.vt_num))){
//out of bounds
return false;
}
}
else if((arrinfo->vtype).vaType == DYN_L_ARR && idOrNumNode->gs == g_NUM) {
int idx = (idOrNumNode->tkinfo->value).num;
if(idx > arrinfo->vtype.ei.vt_num)
return false;
}
else if((arrinfo->vtype).vaType == DYN_R_ARR && idOrNumNode->gs == g_NUM) {
int idx = (idOrNumNode->tkinfo->value).num;
if(idx < arrinfo->vtype.si.vt_num)
return false;
}
else if((arrinfo->vtype).vaType == VARIABLE)
return false;
else if(idOrNumNode->gs == g_ID) {
symTableNode *stn = idOrNumNode->stNode;
if(stn == NULL)
return false;
else if(stn->info.var.vtype.baseType != g_INTEGER)
return false;
}
return true;
}
primitiveDataType getExpressionPrimitiveType(ASTNode *ptr) {
switch(ptr->gs) {
// Arithemetic operators, only binary forms called here
case g_PLUS:
case g_MINUS:
case g_MUL:
case g_DIV:
{
primitiveDataType t1 = getExpressionPrimitiveType(ptr->child);
primitiveDataType t2 = getExpressionPrimitiveType(ptr->child->next);
if (t1 == T_INTEGER && t2 == T_INTEGER)
return T_INTEGER;
if (t1 == T_REAL && t2 == T_REAL)
return T_REAL;
if(t1 == T_UNDEFINED || t2 == T_UNDEFINED)
return T_UNDEFINED;
return T_ERROR;
}
// Relational operators
case g_LT:
case g_LE:
case g_GT:
case g_GE:
case g_EQ:
case g_NE:
{
primitiveDataType t1 = getExpressionPrimitiveType(ptr->child);
primitiveDataType t2 = getExpressionPrimitiveType(ptr->child->next);
if (t1 == T_INTEGER && t2 == T_INTEGER)
return T_BOOLEAN;
if (t1 == T_REAL && t2 == T_REAL)
return T_BOOLEAN;
if(t1 == T_UNDEFINED || t2 == T_UNDEFINED)
return T_UNDEFINED;
return T_ERROR;
}
// Logical operators
case g_AND:
case g_OR:
{
primitiveDataType t1 = getExpressionPrimitiveType(ptr->child);
primitiveDataType t2 = getExpressionPrimitiveType(ptr->child->next);
if (t1 == T_BOOLEAN && t2 == T_BOOLEAN)
return T_BOOLEAN;
if(t1 == T_UNDEFINED || t2 == T_UNDEFINED)
return T_UNDEFINED;
return T_ERROR;
}
case g_u:
{
// t1 will be g_unary_op
primitiveDataType t2 = getExpressionPrimitiveType(ptr->child->next); // g_ of either arithmetic, relational or logical operators.
// Can only have real or integer expression with unary operator.
if(t2 == T_REAL || t2 == T_INTEGER || t2 == T_UNDEFINED)
return t2;
return T_ERROR;
}
case g_var_id_num:
return getExpressionPrimitiveType(ptr->child);
case g_NUM:
return T_INTEGER;
case g_RNUM:
return T_REAL;
case g_TRUE:
case g_FALSE:
return T_BOOLEAN;
case g_ID:
{
if (ptr->stNode == NULL)
return T_UNDEFINED;
// array[index]
if(ptr->next != NULL) {
if (rhsBoundsCheckIfStatic(ptr, ptr->next) == false)
return T_UNDEFINED;
}
primitiveDataType result;
if (ptr->stNode->info.var.vtype.baseType == g_INTEGER)
result = T_INTEGER;
else if (ptr->stNode->info.var.vtype.baseType == g_REAL)
result = T_REAL;
else if (ptr->stNode->info.var.vtype.baseType == g_BOOLEAN)
result = T_BOOLEAN;
// ID should either be a variable or it should it indexed.
if (ptr->stNode->info.var.vtype.vaType == VARIABLE ^ ptr->next != NULL)
return result;
return T_ERROR;
}
}
}
// DONE: handle memory leaks
varType* getDataType(ASTNode *ptr) {
switch(ptr->gs) {
case g_ID:
{
if (ptr->stNode == NULL)
return NULL;
varType *vt = malloc(sizeof(varType));
*vt = (ptr->stNode->info.var.vtype);
return vt;
}
case g_var_id_num:
{
if(ptr->child->gs == g_ID) {
if (ptr->child->stNode == NULL){
return NULL;
}
// Return array type for case A:=B
if (ptr->child->next == NULL){
varType *vt = malloc(sizeof(varType));
*vt = (ptr->child->stNode->info.var.vtype);
return vt;
}
}
// otherwise let it fall through the case stmt
}
default:
{
primitiveDataType expressionType = getExpressionPrimitiveType(ptr);
if (expressionType == T_UNDEFINED)
return NULL;
if(expressionType == T_ERROR) {
throwTypeError(E_EXPRESSION_ERROR, ptr->tkinfo->lno);
return NULL;
}
varType *vt = malloc(sizeof(varType));
if(expressionType == T_INTEGER)
vt->baseType = g_INTEGER;
if(expressionType == T_REAL)
vt->baseType = g_REAL;
if(expressionType == T_BOOLEAN)
vt->baseType = g_BOOLEAN;
vt->width = getSizeByType(vt->baseType);
vt->vaType = VARIABLE;
vt->si.vt_id = NULL;
vt->ei.vt_id = NULL;
return vt;
}
}
}