summaryrefslogtreecommitdiff
path: root/BaseTools/Source/C/VfrCompile/Pccts/h/ast.c
blob: 9326ae16ae7599bd58941b88870c59fd2a72b9cf (plain)
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
/* Abstract syntax tree manipulation functions
 *
 * SOFTWARE RIGHTS
 *
 * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
 * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
 * company may do whatever they wish with source code distributed with
 * PCCTS or the code generated by PCCTS, including the incorporation of
 * PCCTS, or its output, into commerical software.
 *
 * We encourage users to develop software with PCCTS.  However, we do ask
 * that credit is given to us for developing PCCTS.  By "credit",
 * we mean that if you incorporate our source code into one of your
 * programs (commercial product, research project, or otherwise) that you
 * acknowledge this fact somewhere in the documentation, research report,
 * etc...  If you like PCCTS and have developed a nice tool with the
 * output, please mention that you developed it using PCCTS.  In
 * addition, we ask that this header remain intact in our source code.
 * As long as these guidelines are kept, we expect to continue enhancing
 * this system and expect to make other tools available as they are
 * completed. 
 *
 * ANTLR 1.33
 * Terence Parr
 * Parr Research Corporation
 * with Purdue University and AHPCRC, University of Minnesota
 * 1989-2000
 */

#include "pcctscfg.h"

#ifdef PCCTS_USE_STDARG
#include "pccts_stdarg.h"
#else
#include <varargs.h>
#endif

/* ensure that tree manipulation variables are current after a rule
 * reference
 */

void
#ifdef __USE_PROTOS
zzlink(AST **_root, AST **_sibling, AST **_tail)
#else
zzlink(_root, _sibling, _tail)
AST **_root, **_sibling, **_tail;
#endif
{
	if ( *_sibling == NULL ) return;
	if ( *_root == NULL ) *_root = *_sibling;
	else if ( *_root != *_sibling ) (*_root)->down = *_sibling;
	if ( *_tail==NULL ) *_tail = *_sibling;
	while ( (*_tail)->right != NULL ) *_tail = (*_tail)->right;
}

AST *
#ifdef __USE_PROTOS
zzastnew(void)
#else
zzastnew()
#endif
{
	AST *p = (AST *) calloc(1, sizeof(AST));
	if ( p == NULL ) fprintf(stderr,"%s(%d): cannot allocate AST node\n",__FILE__,__LINE__);
	return p;
}

/* add a child node to the current sibling list */
void
#ifdef __USE_PROTOS
zzsubchild(AST **_root, AST **_sibling, AST **_tail)
#else
zzsubchild(_root, _sibling, _tail)
AST **_root, **_sibling, **_tail;
#endif
{
	AST *n;
	zzNON_GUESS_MODE {
	n = zzastnew();
#ifdef DEMAND_LOOK
	zzcr_ast(n, &(zzaCur), LA(0), LATEXT(0));
#else
	zzcr_ast(n, &(zzaCur), LA(1), LATEXT(1));
#endif
	zzastPush( n );
	if ( *_tail != NULL ) (*_tail)->right = n;
	else {
		*_sibling = n;
		if ( *_root != NULL ) (*_root)->down = *_sibling;
	}
	*_tail = n;
	if ( *_root == NULL ) *_root = *_sibling;
	}
}

/* make a new AST node.  Make the newly-created
 * node the root for the current sibling list.  If a root node already
 * exists, make the newly-created node the root of the current root.
 */
void
#ifdef __USE_PROTOS
zzsubroot(AST **_root, AST **_sibling, AST **_tail)
#else
zzsubroot(_root, _sibling, _tail)
AST **_root, **_sibling, **_tail;
#endif
{
	AST *n;
	zzNON_GUESS_MODE {
	n = zzastnew();
#ifdef DEMAND_LOOK
	zzcr_ast(n, &(zzaCur), LA(0), LATEXT(0));
#else
	zzcr_ast(n, &(zzaCur), LA(1), LATEXT(1));
#endif
	zzastPush( n );
	if ( *_root != NULL )
		if ( (*_root)->down == *_sibling ) *_sibling = *_tail = *_root;
	*_root = n;
	(*_root)->down = *_sibling;
	}
}

/* Apply function to root then each sibling
 * example: print tree in child-sibling LISP-format (AST has token field)
 *
 *	void show(tree)
 *	AST *tree;
 *	{
 *		if ( tree == NULL ) return;
 *		printf(" %s", zztokens[tree->token]);
 *	}
 *
 *	void before() { printf(" ("); }
 *	void after()  { printf(" )"); }
 *
 *	LISPdump() { zzpre_ast(tree, show, before, after); }
 *
 */
void
#ifdef __USE_PROTOS
zzpre_ast(
	  AST *tree,
	  void (*func)(AST *),   /* apply this to each tree node */
	  void (*before)(AST *), /* apply this to root of subtree before preordering it */
	  void (*after)(AST *))  /* apply this to root of subtree after preordering it */
#else
zzpre_ast(tree, func, before, after)
AST *tree;
void (*func)(),   /* apply this to each tree node */
	 (*before)(), /* apply this to root of subtree before preordering it */
	 (*after)();  /* apply this to root of subtree after preordering it */
#endif
{
	while ( tree!= NULL )
	{
		if ( tree->down != NULL ) (*before)(tree);
		(*func)(tree);
		zzpre_ast(tree->down, func, before, after);
		if ( tree->down != NULL ) (*after)(tree);
		tree = tree->right;
	}
}

/* free all AST nodes in tree; apply func to each before freeing */

#if 0
////void
////#ifdef __USE_PROTOS
////zzfree_ast(AST *tree)
////#else
////zzfree_ast(tree)
////AST *tree;
////#endif
////{
////	if ( tree == NULL ) return;
////	zzfree_ast( tree->down );
////	zzfree_ast( tree->right );
////	zztfree( tree );
////}
#endif

/*
   MR19 Optimize freeing of the following structure to limit recursion
   SAKAI Kiyotaka (ksakai@isr.co.jp)
*/

/*
         NULL o
             / \
           NULL o
               / \
            NULL NULL
*/

/*
   MR21 Another refinement to replace recursion with iteration
   NAKAJIMA Mutsuki (muc@isr.co.jp).
*/

void
#ifdef __USE_PROTOS
zzfree_ast(AST *tree)
#else
zzfree_ast(tree)
AST *tree;
#endif
{

    AST *otree;

    if (tree == NULL) return;

    while (tree->down == NULL || tree->right == NULL) {

        if (tree->down == NULL && tree->right == NULL) {
            zztfree(tree);
            return;
        }

        otree = tree;
        if (tree->down == NULL) {
            tree = tree->right;
        } else {
            tree = tree->down;
        }
        zztfree( otree );
    }

    while (tree != NULL) {
        zzfree_ast(tree->down);
        otree = tree;
        tree = otree->right;
        zztfree(otree);
    }
}

/* build a tree (root child1 child2 ... NULL)
 * If root is NULL, simply make the children siblings and return ptr
 * to 1st sibling (child1).  If root is not single node, return NULL.
 *
 * Siblings that are actually siblins lists themselves are handled
 * correctly.  For example #( NULL, #( NULL, A, B, C), D) results
 * in the tree ( NULL A B C D ).
 *
 * Requires at least two parameters with the last one being NULL.  If
 * both are NULL, return NULL.
 */
#ifdef PCCTS_USE_STDARG
AST *zztmake(AST *rt, ...)
#else
AST *zztmake(va_alist)
va_dcl
#endif
{
	va_list ap;
	register AST *child, *sibling=NULL, *tail=NULL /* MR20 */, *w;
	AST *root;

#ifdef PCCTS_USE_STDARG
	va_start(ap, rt);
	root = rt;
#else
	va_start(ap);
	root = va_arg(ap, AST *);
#endif

	if ( root != NULL )
		if ( root->down != NULL ) return NULL;
	child = va_arg(ap, AST *);
	while ( child != NULL )
	{
		for (w=child; w->right!=NULL; w=w->right) {;} /* find end of child */
		if ( sibling == NULL ) {sibling = child; tail = w;}
		else {tail->right = child; tail = w;}
		child = va_arg(ap, AST *);
	}
	if ( root==NULL ) root = sibling;
	else root->down = sibling;
	va_end(ap);
	return root;
}

/* tree duplicate */
AST *
#ifdef __USE_PROTOS
zzdup_ast(AST *t)
#else
zzdup_ast(t)
AST *t;
#endif
{
	AST *u;
	
	if ( t == NULL ) return NULL;
	u = zzastnew();
	*u = *t;
#ifdef zzAST_DOUBLE
	u->up = NULL;		/* set by calling invocation */
	u->left = NULL;
#endif
	u->right = zzdup_ast(t->right);
	u->down = zzdup_ast(t->down);
#ifdef zzAST_DOUBLE
	if ( u->right!=NULL ) u->right->left = u;
	if ( u->down!=NULL ) u->down->up = u;
#endif
	return u;
}

void
#ifdef __USE_PROTOS
zztfree(AST *t)
#else
zztfree(t)
AST *t;
#endif
{
#ifdef zzd_ast
	zzd_ast( t );
#endif
	free( t );
}

#ifdef zzAST_DOUBLE
/*
 * Set the 'up', and 'left' pointers of all nodes in 't'.
 * Initial call is double_link(your_tree, NULL, NULL).
 */
void
#ifdef __USE_PROTOS
zzdouble_link(AST *t, AST *left, AST *up)
#else
zzdouble_link(t, left, up)
AST *t, *left, *up;
#endif
{
	if ( t==NULL ) return;
	t->left = left;
	t->up = up;
	zzdouble_link(t->down, NULL, t);
	zzdouble_link(t->right, t, up);
}
#endif