-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
300 lines (271 loc) · 8.25 KB
/
stack.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
/* Austin Hester
CS 4280 sp18
C.Z. Janikow */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "stack.h"
#include "token.h"
#include "node.h"
#include "pop.h"
/*
There is so much recursion here that I would not
recommend even looking at this unless you enjoy
hurting your brain.
Note: build*stack(...) are for static semantics.
They are quite complicated. buildglobalstack() either
directly and/or indirectly calls every function except
the functions that begin with 'just'
// buildlocalstack is the most complicated.
// it checks for variables in all of the parent stacks
// as well as its own stack.
// It recursively calls itself recursively, recursively,
// if there is a block inside of a block. beware.
Also note: justbuild*stack(...) are for code gen.
They make much more sense. What they do not do is go
deeper into each block or statement.
*/
// Build the global var stack
// for static semantics
int
buildglobalstack(node_t* root, stack_t* stack)
{
if (root == NULL)
return 0;
// If we enter a block
if(strcmp(root->token->instance, "<block>") == 0) {
// Create a new local stack
stack_t* local = (stack_t*) malloc(4*sizeof(stack_t));
if (local == (stack_t*)NULL)
return -1;
local->varstack = (token_t**) malloc(128*sizeof(token_t*));
local->nvars = 0;
local->tos = -1;
// Build the local stack
// Also does checking for use of undefined vars
int ls = buildlocalstack(root, stack, local, 1, 0);
printf("Num Vars = %d \n", local->nvars);
int i;
for (i = 0; i < local->nvars; i++) {
displaytoken(local->varstack[i]);
}
free(local);
return ls;
}
// Add the new var
if(strcmp(root->token->id, "idTK") == 0) {
if (isinstack(root->token, stack) == -1) {
// add this identifier to stack
addtostack(root->token, stack);
} else {
// already defined
return 1;
}
}
int stk;
int i;
for (i = 0; i < root->num_children; i++) {
// recursion
stk = buildglobalstack(root->children[i], stack);
if (stk > 0)
return stk;
}
return 0;
}
// For code generation
int
justbuildglobalstack(node_t* root, stack_t* stack)
{
if (root == NULL)
return 0;
// If we enter a block
if(strcmp(root->token->instance, "<block>") == 0) {
fprintf(stderr, "[BLOC:] SHIT\n");
return 0;
}
// Add the new var
if(strcmp(root->token->id, "idTK") == 0) {
if (isinstack(root->token, stack) == -1) {
// add this identifier to stack
addtostack(root->token, stack);
} else {
// already defined
//addtostack(root->token, stack);
fprintf(stderr, "[BLOC:] NOOP\n");
return 1;
}
}
int stk;
int i;
for (i = 0; i < root->num_children; i++) {
// recursion
stk = justbuildglobalstack(root->children[i], stack);
if (stk > 0)
return stk;
}
return 0;
}
int
justbuildlocalstack(node_t* root, stack_t* stack)
{
if (root == NULL)
return 0;
// If we enter a block
if(strcmp(root->token->instance, "<block>") == 0) {
fprintf(stderr, "[BLOC:] SHIT\n");
return 0;
}
if( strcmp(root->token->instance, "<in>") == 0 ||
strcmp(root->token->instance, "<out>") == 0 ||
strcmp(root->token->instance, "<iff>") == 0 ||
strcmp(root->token->instance, "<iter>") == 0 ||
strcmp(root->token->instance, "<assign>") == 0) {
// Check for the use of vars
return 1;
}
// Add the new var
if(strcmp(root->token->id, "idTK") == 0) {
if (isinstack(root->token, stack) == -1) {
// add this identifier to stack
addtostack(root->token, stack);
} else {
// already defined
//addtostack(root->token, stack);
fprintf(stderr, "[BLOC:] NOOP\n");
return 1;
}
}
int stk;
int i;
for (i = 0; i < root->num_children; i++) {
// recursion
stk = justbuildlocalstack(root->children[i], stack);
if (stk > 0)
return stk;
}
return 0;
}
int
buildlocalstack(node_t* root, stack_t* stack, stack_t* local,
int first, int numborrowed)
{
if (root == NULL)
return 0;
if (root->token == NULL || root->token->instance == NULL) {
fprintf(stderr, "Memory error. Retry.\n");
return -1;
}
// If we enter an even deeper scope
if(strcmp(root->token->instance, "<block>") == 0 &&
!first) {
//printf("nother block!\n;");
// Create a new local stack
stack_t* newlocal = (stack_t*) malloc(4*sizeof(stack_t));
if (newlocal == (stack_t*)NULL)
return -1;
newlocal->varstack = (token_t**) malloc(128*sizeof(token_t*));
newlocal->nvars = 0;
newlocal->tos = -1;
// Add the current local stack
int j;
for (j = 0; j < local->nvars; j++) {
addtostack(local->varstack[j], newlocal);
// Don't worry, `numborrowed` allows overwriting
}
// Build the local stack
// Also does checking for use of undefined vars
/* meta-recursion */
int ls = buildlocalstack(root, stack, newlocal, 1, newlocal->nvars);
int i;
for (i = 0; i < newlocal->nvars; i++) {
displaytoken(newlocal->varstack[i]);
}
// Free the now useless stack
free(newlocal);
return ls;
}
// Check to make sure variables are declared.
if( strcmp(root->token->instance, "<in>") == 0 ||
strcmp(root->token->instance, "<out>") == 0 ||
strcmp(root->token->instance, "<iff>") == 0 ||
strcmp(root->token->instance, "<iter>") == 0 ||
strcmp(root->token->instance, "<assign>") == 0) {
// Check for the use of vars
// call to recursive function
int u = checkundeclared(root, stack, local);
return u;
}
if(strcmp(root->token->id, "idTK") == 0) {
// Check if its been added in this scope already
if (isinstack(root->token, local+numborrowed) == -1) {
// add this identifier to stack
addtostack(root->token, local);
} else {
// already defined, replace
fprintf(stderr, "\nvar %s already defined: line %d\n",
root->token->instance,
root->token->line_num);
return 1;
}
}
int ud;
int i;
for (i = 0; i < root->num_children; i++) {
// recursion
ud = buildlocalstack(root->children[i], stack, local, 0, numborrowed);
if (ud > 0)
return ud;
}
return 0;
}
// Check if variable is in either stack
int
checkundeclared(node_t* root, stack_t* stack, stack_t* local)
{
if (root == NULL)
return 0;
if(strcmp(root->token->id, "idTK") == 0) {
if (isinstack(root->token, local) == -1 &&
isinstack(root->token, stack) == -1) {
// variable undefined
fprintf(stderr, "\nvar %s undefined: line %d\n",
root->token->instance,
root->token->line_num);
return 2;
}
}
int ud;
int i;
for (i = 0; i < root->num_children; i++) {
// recursion
ud = checkundeclared(root->children[i], stack, local);
if (ud > 0)
return ud;
}
return 0;
}
// Add given token to given stack
void
addtostack(token_t* tk, stack_t* stack)
{
int n = stack->nvars;
stack->varstack[n] = (token_t*) malloc(sizeof(token_t));
copytoken(stack->varstack[n], tk);
stack->nvars++;
stack->tos = stack->nvars - 1;
return;
}
// return 1 if tk is in stack
// return 0 otherwise
int
isinstack(token_t* tk, stack_t* stack)
{
const char* varname = tk->instance;
int i;
for (i = 0; i < stack->nvars; i++) {
const char* t = stack->varstack[i]->instance;
if (strcmp(varname, t) == 0)
return i;
}
return -1;
}