-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathctree.c
477 lines (403 loc) · 13.7 KB
/
ctree.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* ctree -- generic tree implementation with nodes capable of having
* arbitrary number of children and siblings stored in a circularly linked
* list for fast insertion/deletion/moving
*
* chillu
* Date: Sunday November, 15 2009
* Time: 10:18:44 PM IST
*/
#include <stdlib.h>
#include "ctree.h"
/*
* creates and returns pointer to an empty root node
* root nodes cannot have siblings
*/
struct Node*
create_tree (void* data) {
struct Node* root = (struct Node *) malloc(sizeof(struct Node));
root->data = data;
root->dataType = (void*) -1;
root->name = (void*) -1;
root->parent = root->firstchild = (struct Node *) NULL;
root->prevsibling = root->nextsibling = (struct Node *) NULL;
return root;
}
/*
* insert a given node under a target node
* as a child of the target. If target node
* already has children the node is added at
* the end.
*/
int
insert_node_under (struct Node* node, struct Node* targetparent) {
struct Node* lastchild = (struct Node *) NULL;
node->parent = targetparent;
if (!targetparent->firstchild) {
targetparent->firstchild = node;
node->nextsibling = node->prevsibling = node;
} else {
lastchild = targetparent->firstchild->prevsibling;
lastchild->nextsibling = node;
node->prevsibling = lastchild;
node->nextsibling = targetparent->firstchild;
targetparent->firstchild->prevsibling = node;
}
return 1;
}
/*
* creates and returns poiner to a child under a given node
*/
//struct Node* create_node_under (struct Node* node, void* data, void* dataType, void* name, void* dataReal);
struct Node*
create_node_under (struct Node* node, void* data, void* dataType, void* name, void* dataReal) {
struct Node* newchild = (struct Node *) malloc(sizeof(struct Node));
newchild->data = data;
newchild->dataReal = dataReal;
newchild->dataType = dataType;
newchild->name = name;
newchild->firstchild = (struct Node *) NULL;
insert_node_under (newchild, node);
return newchild;
}
/*
* insert a given node next to a target node
* as a sibling of the target.
*/
int
insert_node_next_to (struct Node* node, struct Node* targetsibling) {
struct Node* next = targetsibling->nextsibling;
node->parent = targetsibling->parent;
/* take care of sibling links */
targetsibling->nextsibling = node;
next->prevsibling = node;
node->nextsibling = next;
node->prevsibling = targetsibling;
return 1;
}
/*
* creates and inserts a sibling node next to a given node
* returning pointer to the newly created sibling
*/
struct Node*
create_node_next_to (struct Node* node, void* data) {
struct Node* newsibling = (struct Node *) malloc(sizeof(struct Node));
newsibling->data = data;
newsibling->dataReal = (void *) -1;
newsibling->dataType = (void *) -1;
newsibling->name = (void *) -1;
newsibling->firstchild = (struct Node *) NULL;
insert_node_next_to (newsibling, node);
return newsibling;
}
static void
_traverse_node (struct Node* node, int depth,
void (*print_data)(void*, int, int, unsigned int*)) {
struct Node *start, *next;
start = next = node->firstchild;
static unsigned int bitmask = 0;
int islastchild = node->nextsibling == ((node->parent) ? node->parent->firstchild : NULL);
/* If a new traversal reset bitmask */
if (!depth)
bitmask = 0;
/* call printing function */
print_data (node->data, depth, islastchild, &bitmask);
/* update bitmask if lastchild is past */
if (islastchild) {
bitmask ^= (1 << (depth));
}
/* if the node has a child */
if (start) {
_traverse_node (next, depth + 1, print_data);
/* recurse without going round in circles */
while ((next = next->nextsibling) != start) {
_traverse_node (next, depth + 1, print_data);
}
}
}
/*
* traverse recursively all the nodes under the given node
* callback function is called with useful information like:
*
* data: The data stored in the node
* depth: Distance from the root node
* islastchild: Boolean is 0 if there are more siblings to this node
* bitmask: Flags for each depth level, with 0 indicating more
* siblings are to come on that depth and 1 indicating
* that all siblings on that level are past. Thus, it
* starts out with all 0s.
*
* NOTE: The bitmask is only 32 bits wide, so
* depth information is available only from 0-31
*/
void
traverse_node (struct Node* node,
void (*print_data)(void*, int, int, unsigned int*)) {
_traverse_node(node, 0, print_data);
}
struct Node*
pre_order (struct Node* node, VoidNode ** ast) {
static int counter = 0;
static int rel_counter = 0;
static int skip_constant = 0;
char Tn [3];
struct Node *start, *next, *temp;
start = next = node->firstchild;
// if(statement_list) {
// printf("{sdasdsad]")
// line_counter++;
// statement_list = false;
// }
if( eq ( (char *)node->data, "PROGRAM" ) ) {
} else if ( eq ( (char *)node->data, "#" ) ) {
} else if ( eq ( (char *)node->data, "MAIN_DEF" ) ) {
} else if ( eq ( (char *)node->data, "DECLARATION" ) ) {
} else if ( eq ( (char *)node->data, "VAR_TYPE" ) ) {
} else if ( eq ( (char *)node->data, "VAR_LIST" ) ) {
} else if ( eq ( (char *)node->data, "VAR_ITEM" ) ) {
} else if ( eq ( (char *)node->data, "FUNCTION_BODY" ) ) {
} else if ( eq ( (char *)node->data, "INTERNAL_DECLARATIONS" ) ) {
} else if ( eq ( (char *)node->data, "STATEMENT_LIST" ) ) {
//statement_list = true;
//pre_order(node);
} else if ( eq ( (char *)node->data, "STATEMENT" ) ) {
} else if ( eq ( (char *)node->data, "EXPRESSION" ) ) {
} else if ( eq ( (char *)node->data, "IF_STATEMENT" ) ) {
printf("[IFT, T%d, GOTO L%d]\n", rel_counter, rel_counter);
} else if ( eq ( (char *)node->data, "WHILE_STATEMENT" ) ) {
printf("[LABEL, , , L%d]\n", ++rel_counter);
} else if ( eq ( (char *)node->data, "RETURN_STATEMENT" ) ) {
} else if ( eq ( (char *)node->data, "ASSIGN_EXP" ) ) {
//node->firstchild->prevsibling->data = Tn;
//sprintf(node->firstchild->prevsibling->data, Tn)
//node->prevsibling = targetsibling;
} else if ( eq ( (char *)node->data, "BINARY_EXP" ) ) {
//printf("HIJOS: %d\n",count_children(node));
} else if ( eq ( (char *)node->data, "BINARY_OP" ) ) {
skip_constant = 1;
//todo, checar el caso numero + numero
//tod, +,-,* instead of arith_op
sprintf(Tn, "T%d", counter++);
printf("[%s, %s, %s, T%d]\n", (char*) node->firstchild->data, (char *)node->prevsibling->firstchild->data, (char *)node->nextsibling->firstchild->firstchild->data, counter);
if ( eq ( (char *)node->data, "rel_op" ) ) {
rel_counter = counter - 1;
}
} else if ( eq ( (char *)node->data, "PRIMARY_EXPR" ) ) {
} else if ( eq ( (char *)node->data, "CONSTANT" ) ) {
//sprintf(Tn, "T%d", counter++);
if(!skip_constant) {
printf("[=, %s, , %s]\n", (char *) node->firstchild->dataReal, (char*) node->firstchild->name);
} else {
skip_constant = 0;
}
} else {
addFirst(ast, node->data);
}
//printf("\nWTF %d",(int) node->dataType);
if (!node->data ) {
return NULL;
} else if((int) node->dataType != -1) {
//printf ("Dato del Nodo:%s, Nombre de Dato:%s, Valor de Dato:%s\n", (char *)node->data, (char *) node->name,(char *)node->dataReal);
} else {
//printf ("Dato del Nodo: %s\n", (char *)node->data);
}
if (start) {
if ( (temp = pre_order (start, ast)) )
return temp;
while ( (next = next->nextsibling) != start)
if ( (temp = pre_order (next, ast)) )
return temp;
}
return NULL;
}
/*
* detach (but don't delete) the node, preserving
* silbing and parent-child relationships
*
* NOTE: Assumes that node is not root
* It doesn't make sense to "remove" root
* from a tree as root _is_ the tree
*/
int
detach_node (struct Node* node) {
/* detach the node out of the list of
children of which the node is a part */
struct Node* prev = node->prevsibling;
struct Node* next = node->nextsibling;
if (node != next) {
/* has at least one other sibling */
prev->nextsibling = next;
next->prevsibling = prev;
}
/* if the node is not root then you have to
manage parent-child relationships */
if (node->parent) {
/* the only child is about to be deleted */
if (node->nextsibling == node) {
node->parent->firstchild = (struct Node *) NULL;
}
/* the first child of the parent is about to be deleted */
else if (node->parent->firstchild == node) {
node->parent->firstchild = node->nextsibling;
}
}
return 1;
}
static int
_delete_node (struct Node* node, int raw) {
struct Node* start = node->firstchild;
struct Node* curr = (struct Node *) NULL;
/* if deletion is raw then don't bother maintaining
the prev/next sibling relationships as these nodes
will anyway be deleted later. (i.e. part of a
larger recursive deletion)
raw is 0 only for the toplevel invocation of _delete_node */
if (!raw && node->parent) {
detach_node (node);
}
/* first delete all children of the node */
if (start) {
/* only one child */
if (start == start->nextsibling) {
_delete_node (start, 1);
}
/* more than one child */
else {
curr = start->nextsibling;
while (start != curr) {
curr = curr->nextsibling;
_delete_node (curr->prevsibling, 1);
}
_delete_node (start, 1);
}
}
/* delete the node itself */
free (node);
return 1;
}
int count_children(struct Node* node) {
int count = 0;
struct Node *start, *next;
start = next = node->firstchild;
if (start) {
count++;
while ((next = next->nextsibling) != start) {
count++;
}
}
return count;
}
/*
* recursively delete all the children under a given node
* maintaining the nextsibling relationships of the node
* Returns 1 for success, 0 for failure
*
* TODO: Currently always returns 1 => Do error checking.
*/
int
delete_node (struct Node* node) {
return _delete_node(node, 0);
}
/*
* move a given node next to a target node
* as a sibling of the target
* Does not check if the target is actually
* a child of the given node (in which case
* the results are undefined)
*/
int
move_node_next_to (struct Node* node, struct Node* targetsibling) {
return detach_node (node) && insert_node_next_to (node, targetsibling);
}
/*
* move a given node next to a target node
* as a child of the target
* Does not check if the target is actually
* a child of the given node (in which case
* the results are undefined)
*/
int
move_node_under (struct Node* node, struct Node* targetparent) {
return detach_node (node) && insert_node_under (node, targetparent);
}
/*
* returns a pointer to a new tree that is a
* recursive copy of the sub-tree under node
*
* NOTE: The new tree will still point to the same
* set of pointers that the old tree pointed to
*/
struct Node* shallow_copy (struct Node* node)
{
struct Node* root = (struct Node *) create_tree (node->data);
struct Node *start, *next;
start = next = node->firstchild;
if (start) {
insert_node_under (shallow_copy (start), root);
while ((next = next->nextsibling) != start) {
insert_node_under (shallow_copy (next), root);
}
}
return root;
}
/*
* returns a pointer to a new tree that is a
* recursive copy of the sub-tree under node
* with copyfunc allocating memory appropriately
*/
struct Node* deep_copy (struct Node* node, void* (*copyfunc)(void*)) {
struct Node* root = (struct Node *) create_tree (node->data);
root->data = copyfunc(root->data);
struct Node *start, *next;
start = next = node->firstchild;
if (start) {
insert_node_under (deep_copy (start, copyfunc), root);
while ((next = next->nextsibling) != start) {
insert_node_under (deep_copy (next, copyfunc), root);
}
}
return root;
}
/*
* traverse the tree and return the pointer to the first node for
* which the compare function returns 0; otherwise, return NULL
*/
struct Node*
search (struct Node* node, void* a, int (*compare)(void* a, void* b)) {
struct Node *start, *next, *temp;
start = next = node->firstchild;
if (!node->data)
return NULL;
if (compare(a, node->data) == 0)
return node;
if (start) {
if ((temp = search (start, a, compare)))
return temp;
while ((next = next->nextsibling) != start)
if ((temp = search (next, a, compare)))
return temp;
}
return NULL;
}
/*
* traverse the tree and return the pointer to the first node for
* which the compare function returns 0; otherwise, return NULL
*/
struct Node*
searchFirstLevel (struct Node* node, void* a, int (*compare)(void* a, void * b)) {
struct Node *start, *next;
start = next = node->firstchild;
if (start == NULL) {
return NULL;
}
if (!next->data)
return NULL;
if (compare(a, next->data) == 0)
return next;
do{
if (compare(a, next->data) == 0)
return next;
next = next->nextsibling;
}while (next != start);
return NULL;
}