forked from CS429-S2022/CI-Lab-Student
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval-ref-week1.c
More file actions
262 lines (243 loc) · 9.43 KB
/
Copy patheval-ref-week1.c
File metadata and controls
262 lines (243 loc) · 9.43 KB
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
#include "ci.h"
extern bool is_binop(token_t);
extern bool is_unop(token_t);
char *strrev(char *str);
static void infer_type(node_t *nptr) {
if (nptr == NULL) return;
for (int i = 0; i < 3; ++i) {
infer_type(nptr->children[i]);
}
// check running status
if (terminate || ignore_input) return;
switch (nptr->node_type) {
// For each week, you will also need to include error checking for each type.
// Week 2 TODO: Extend type inference to handle operators on string types.
// Week 3 TODO: Implement type evaluation for variables.
// You will need to modify or extend some of the cases here.
case NT_INTERNAL:
switch (nptr->tok) {
// For reference, the identity (do nothing) operator is implemented for you.
case TOK_IDENTITY:
nptr->type = nptr->children[0]->type;
break;
case TOK_PLUS:
if (nptr->children[0]->type != INT_TYPE || nptr->children[1]->type != INT_TYPE) {
handle_error(ERR_TYPE);
return;
}
nptr->type = nptr->children[0]->type;
break;
case TOK_BMINUS:
case TOK_DIV:
case TOK_MOD:
if (nptr->children[0]->type != INT_TYPE || nptr->children[1]->type != INT_TYPE) {
handle_error(ERR_TYPE);
return;
}
nptr->type = nptr->children[0]->type;
break;
case TOK_TIMES:
if (nptr->children[0]->type != INT_TYPE || nptr->children[1]->type != INT_TYPE) {
handle_error(ERR_TYPE);
return;
}
nptr->type = nptr->children[0]->type;
break;
case TOK_AND:
case TOK_OR:
if (nptr->children[0]->type != BOOL_TYPE || nptr->children[1]->type != BOOL_TYPE) {
handle_error(ERR_TYPE);
return;
}
nptr->type = nptr->children[0]->type;
break;
case TOK_LT:
case TOK_GT:
case TOK_EQ:
if (nptr->children[0]->type != nptr->children[1]->type ||
nptr->children[0]->type == BOOL_TYPE || nptr->children[1]->type == BOOL_TYPE) {
handle_error(ERR_TYPE);
return;
}
nptr->type = BOOL_TYPE;
break;
case TOK_UMINUS:
if (nptr->children[0]->type == BOOL_TYPE) {
handle_error(ERR_TYPE);
return;
}
nptr->type = nptr->children[0]->type;
break;
case TOK_NOT:
if (nptr->children[0]->type != BOOL_TYPE) {
handle_error(ERR_TYPE);
return;
}
nptr->type = nptr->children[0]->type;
break;
case TOK_QUESTION:
if (nptr->children[0]->type != BOOL_TYPE || (nptr->children[1]->type != nptr->children[2]->type)) {
handle_error(ERR_TYPE);
return;
}
nptr->type = nptr->children[1]->type;
break;
default:
logging(LOG_ERROR, "unsupported token type for internal node");
break;
}
case NT_LEAF:
break;
default:
break;
}
return;
}
static void infer_root(node_t *nptr) {
if (nptr == NULL) return;
// check running status
if (terminate || ignore_input) return;
// check for assignment
if (nptr->type == ID_TYPE) {
infer_type(nptr->children[1]);
} else {
for (int i = 0; i < 3; ++i) {
infer_type(nptr->children[i]);
}
nptr->type = nptr->children[0]->type;
}
return;
}
static void eval_node(node_t *nptr) {
if (nptr == NULL) return;
// ternary expression has unique eval priority
if (nptr->tok != TOK_QUESTION) {
for (int i = 0; i < 2; ++i) {
eval_node(nptr->children[i]);
}
}
// check running status
if (terminate || ignore_input) return;
switch (nptr->node_type) {
case NT_INTERNAL:
// Week 2 TODO: Extend evaluation to handle operators on string types.
// You will need to modify some cases for this.
if (is_unop(nptr->tok)) {
switch (nptr->tok) {
case TOK_UMINUS:
nptr->val.ival = - nptr->children[0]->val.ival; break;
case TOK_NOT:
nptr->val.bval = ! nptr->children[0]->val.bval; break;
default:
break;
}
}
if (is_binop(nptr->tok)) {
switch (nptr->tok) {
case TOK_PLUS:
nptr->val.ival = nptr->children[0]->val.ival + nptr->children[1]->val.ival; break;
case TOK_BMINUS:
nptr->val.ival = nptr->children[0]->val.ival - nptr->children[1]->val.ival; break;
case TOK_TIMES:
nptr->val.ival = nptr->children[0]->val.ival * nptr->children[1]->val.ival; break;
case TOK_DIV:
// check divide by zero
if (nptr->children[1]->val.ival == 0) {
handle_error(ERR_EVAL);
return;
}
nptr->val.ival = nptr->children[0]->val.ival / nptr->children[1]->val.ival; break;
case TOK_MOD:
// check divide by zero
if (nptr->children[1]->val.ival == 0) {
handle_error(ERR_EVAL);
return;
}
nptr->val.ival = nptr->children[0]->val.ival % nptr->children[1]->val.ival; break;
case TOK_AND:
nptr->val.bval = nptr->children[0]->val.bval && nptr->children[1]->val.bval; break;
case TOK_OR:
nptr->val.bval = nptr->children[0]->val.bval || nptr->children[1]->val.bval; break;
case TOK_LT:
nptr->val.bval = nptr->children[0]->val.ival < nptr->children[1]->val.ival; break;
case TOK_GT:
nptr->val.bval = nptr->children[0]->val.ival > nptr->children[1]->val.ival; break;
case TOK_EQ:
nptr->val.bval = nptr->children[0]->val.ival == nptr->children[1]->val.ival; break;
default:
break;
}
}
if (nptr->tok == TOK_QUESTION) {
// short circuit eval strategy
eval_node(nptr->children[0]);
if (terminate || ignore_input) return;
if (nptr->children[0]->val.bval) {
eval_node(nptr->children[1]);
if (terminate || ignore_input) return;
nptr->val.ival = nptr->children[1]->val.ival;
} else {
eval_node(nptr->children[2]);
if (terminate || ignore_input) return;
nptr->val.ival = nptr->children[2]->val.ival;
}
}
if (nptr->tok == TOK_IDENTITY) {
nptr->val.ival = nptr->children[0]->val.ival;
}
break;
case NT_LEAF:
break;
default:
break;
}
return;
}
void eval_root(node_t *nptr) {
if (nptr == NULL) return;
// check running status
if (terminate || ignore_input) return;
// check for assignment
if (nptr->type == ID_TYPE) {
eval_node(nptr->children[1]);
if (terminate || ignore_input) return;
put(nptr->children[0]->val.sval, nptr->children[1]);
return;
}
for (int i = 0; i < 2; ++i) {
eval_node(nptr->children[i]);
}
if (terminate || ignore_input) return;
if (nptr->type == STRING_TYPE) {
(nptr->val).sval = (char *) malloc(strlen(nptr->children[0]->val.sval) + 1);
if (! nptr->val.sval) {
logging(LOG_FATAL, "failed to allocate string");
return;
}
strcpy(nptr->val.sval, nptr->children[0]->val.sval);
} else {
nptr->val.ival = nptr->children[0]->val.ival;
}
return;
}
void infer_and_eval(node_t *nptr) {
infer_root(nptr);
eval_root(nptr);
return;
}
/* strrev() - helper function to reverse a given string
* Parameter: The string to reverse.
* Return value: The reversed string. The input string is not modified.
* (STUDENT TODO)
*/
char *strrev(char *str) {
// Week 2 TODO: Implement copying and reversing the string.
return NULL;
}
/* cleanup() - frees the space allocated to the AST
* Parameter: The node to free.
*/
void cleanup(node_t *nptr) {
// Week 2 TODO: Recursively free each node in the AST
return;
}