-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.c
290 lines (251 loc) · 9.33 KB
/
ast.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
#include <stdio.h>
#include <string.h>
int INT_TYPE = 4;
int BOOL_TYPE = 5;
int STRING_TYPE = 6;
int VAR_STRUCT = 7;
int FIELD_STRUCT = 8;
int COMP_OP_R = 9; // Comparison op (Restrictive)
int COMP_OP_NR = 10; // Comparison op (Not Restrictive)
int M_OP = 11; // Maths op
int BEXP = 12; // Binary op
int UEXP = 13; // Unary op
int NEXP = 14; // Not an op
// typedef struct Expression {
// int evaluates_to_type;
// int op;
// int exp_type;
// struct Expression *lexp;
// struct Expression *rexp;
// } Expression;
Expression* add_expression (int type, int op, int exp, Expression *lexp, Expression *rexp, char *sname) {
//printf("adding %d\n",type);
Expression* next_expression = (Expression *)malloc(sizeof(Expression));
next_expression->evaluates_to_type = type;
next_expression->exp_type = exp;
next_expression->op = op;
next_expression->sname = sname;
next_expression->rexp = (Expression *)malloc(sizeof(Expression));
next_expression->rexp = rexp;
next_expression->lexp = (Expression *)malloc(sizeof(Expression));
next_expression->lexp = lexp;
//printf("done adding\n");
return next_expression;
}
void print (Expression *node) {
printf ("\nAST:\n");
printtree (0,node);
printf ("\n");
}
void printtree (int indent, Expression *exp) {
//traverse down
if (exp != NULL){
for (int i = 0; i < indent; i++){ printf(" ");}
if (exp->exp_type == BEXP){
if (exp->op == M_OP){
printf("Binary Maths Expression\n");
}
else{
printf("Binary Boolean Expression\n");
}
}
else if (exp->exp_type == UEXP){
printf("Unary Expression\n");
}
else if (exp->exp_type == NEXP){
if (exp->evaluates_to_type == INT_TYPE){
printf("Int\n");
}
if (exp->evaluates_to_type == BOOL_TYPE){
printf("Bool\n");
}
if (exp->evaluates_to_type == STRING_TYPE){
printf("String\n");
}
}
}
if (exp->lexp != NULL){
printtree(indent+1,exp->lexp);}
if (exp->rexp != NULL){
printtree(indent+1,exp->rexp);}
}
// Pass in null to start, returns the return type of the thing below it
int verify (Expression *node) {
// Do all verification steps - work to bottom of all expressions until two literals, work back up
//printf("Verifying:\n");
//print(node);
//printf("\n\n");
int left_verif;
int right_verif;
left_verif = -1;
right_verif = -1;
if (node->rexp != NULL && node->rexp->exp_type == NEXP){
right_verif = node->rexp->evaluates_to_type;
//printf("Right is not expression, right evaluates to %d\n",right_verif);
}
else if (node->rexp != NULL && (node->rexp->exp_type == UEXP || node->rexp->exp_type == BEXP)){
//printf("Verifying right....\n");
right_verif = verify(node->rexp);
}
if (node->lexp != NULL && node->lexp->exp_type == NEXP){
left_verif = node->lexp->evaluates_to_type;
//printf("Left is not expression, left evaluates to %d\n",left_verif);
}
else if (node->lexp != NULL && (node->lexp->exp_type == UEXP || node->lexp->exp_type == BEXP)){
//printf("Verifying left....\n");
left_verif = verify(node->lexp);
}
if (node->exp_type == UEXP) {
if ( right_verif != -1){
//printf("Left and right verified\n");
if (node->op == M_OP)
{
if (right_verif == INT_TYPE){
//printf("Left and right of maths operator evaluate to ints, returning %d\n",node->evaluates_to_type);
return node->evaluates_to_type;
}
}
else if (node->op == COMP_OP_R)
{
if (right_verif == BOOL_TYPE){
return node->evaluates_to_type;
//printf("Left and right of restrictive comparison evaluate to same, returning %d\n",node->evaluates_to_type);
}
}
}
}
else if (node->exp_type == BEXP) {
if (left_verif != -1 && right_verif != -1){
if (node->op == M_OP)
{
if (left_verif == INT_TYPE && right_verif == INT_TYPE){
//printf("Left and right of maths operator evaluate to ints, returning %d\n",node->evaluates_to_type);
return node->evaluates_to_type;
}
}
else if (node->op == COMP_OP_R)
{
if (left_verif == right_verif && (right_verif == INT_TYPE || right_verif == BOOL_TYPE || left_verif == STRING_TYPE || left_verif== VAR_STRUCT)){
return node->evaluates_to_type;
//printf("Left and right of restrictive comparison evaluate to same, returning %d\n",node->evaluates_to_type);
}
}
else if (node->op == COMP_OP_NR)
{
if ((right_verif == INT_TYPE || right_verif == BOOL_TYPE || right_verif == STRING_TYPE || right_verif == VAR_STRUCT) && (left_verif == INT_TYPE || left_verif == BOOL_TYPE || left_verif == STRING_TYPE || left_verif== VAR_STRUCT)){
return node->evaluates_to_type;
}
}
}
}
if (node->exp_type == NEXP){
return node->evaluates_to_type;
}
//printf ("Verification failed, returning -1\n\n");
return -1;
}
// Pass in null to start, returns the return type of the thing below it
ExpType* verify_dyn (Expression *node) {
// Do all verification steps - work to bottom of all expressions until two literals, work back up
//printf("Verifying:\n");
//print(node);
ExpType* ret = (ExpType *)malloc(sizeof(ExpType));
ExpType* left_verif = (ExpType *)malloc(sizeof(ExpType));
ExpType* right_verif = (ExpType *)malloc(sizeof(ExpType));
left_verif->type = -1;
right_verif->type = -1;
if (node->rexp != NULL && node->rexp->exp_type == NEXP){
right_verif->type = node->rexp->evaluates_to_type;
right_verif->sname = node->rexp->sname;
//printf("Right is not expression, right evaluates to %d\n",right_verif);
}
else if (node->rexp != NULL && (node->rexp->exp_type == UEXP || node->rexp->exp_type == BEXP)){
//printf("Verifying right....\n");
right_verif = verify_dyn(node->rexp);
}
if (node->lexp != NULL && node->lexp->exp_type == NEXP){
left_verif->type = node->lexp->evaluates_to_type;
left_verif->sname = node->lexp->sname;
//printf("Left is not expression, left evaluates to %d\n",left_verif);
}
else if (node->lexp != NULL && (node->lexp->exp_type == UEXP || node->lexp->exp_type == BEXP)){
//printf("Verifying left....\n");
left_verif = verify_dyn(node->lexp);
}
ret->type = node->evaluates_to_type;
ret->sname = node->sname;
if (node->exp_type == BEXP) {
if (left_verif->type != NULL && right_verif->type != NULL){
//printf("Left and right verified\n");
if (node->op == M_OP)
{
if (left_verif->type == INT_TYPE && right_verif->type == INT_TYPE){
//printf("Left and right of maths operator evaluate to ints, returning %d\n",node->evaluates_to_type);
return ret;
}
}
else if (node->op == COMP_OP_R)
{
if (left_verif->type == right_verif->type && (right_verif->type == INT_TYPE || right_verif->type == BOOL_TYPE || left_verif->type == STRING_TYPE || right_verif->type == VAR_STRUCT)){
return ret;
//printf("Left and right of restrictive comparison evaluate to same, returning %d\n",node->evaluates_to_type);
}
}
else if (node->op == COMP_OP_NR)
{
if ((right_verif->type == INT_TYPE|| right_verif->type == BOOL_TYPE || right_verif->type == STRING_TYPE || right_verif->type == VAR_STRUCT) && (left_verif->type == INT_TYPE || left_verif->type == BOOL_TYPE || left_verif->type == STRING_TYPE || left_verif->type == VAR_STRUCT)){
return ret;
}
}
}
}
else if (node->exp_type == UEXP) {
if ( right_verif->type != NULL){
//printf("Left and right verified\n");
if (node->op == M_OP)
{
if (right_verif->type == INT_TYPE){
//printf("Left and right of maths operator evaluate to ints, returning %d\n",node->evaluates_to_type);
return ret;
}
}
else if (node->op == COMP_OP_R)
{
if (right_verif->type == BOOL_TYPE){
return ret;
//printf("Left and right of restrictive comparison evaluate to same, returning %d\n",node->evaluates_to_type);
}
}
}
}
if (node->exp_type == NEXP){
return ret;
}
ret->type = -1;
ret->sname = NULL;
//printf ("Verification failed, returning -1\n\n");
return ret;
}
int check_compatibility ( int type_expected, Expression *root ) {
//printf("checking compatibility with type %d and expression (return type evaluated to be %d)\nVerifying:\n", type_expected, verify(root));
//print(root);
if (verify(root) == type_expected){
return 1;
}
return 0;
// use the verify command, get the return type of the tree root
}
int check_compatibility_dyn ( ExpType *type_expected, Expression *root, int overridesname) {
int type = verify_dyn(root)->type;
//printf("checking compatibility with type %d and expression (return type evaluated to be %d)\nVerifying:\n", type, verify_dyn(root)->type);
char *name = verify_dyn(root)->sname;
//printf ("Actual type is: %d , %s \n",type,name);
printf ("Expected type is: %d , %s\n",type_expected->type, type_expected->sname);
if ( (type == type_expected->type ) && ((type == 7 && (overridesname == 1 || (name != NULL && strcmp( type_expected->sname, name)== 0) )) || type != 7)){
//printf("returning 1\n");
return 1;
}
printf("returning 0 for compatibility\n");
return 0;
// use the verify command, get the return type of the tree root
}