]>
Commit | Line | Data |
---|---|---|
30fdf114 LG |
1 | /* Abstract syntax tree manipulation functions\r |
2 | *\r | |
3 | * SOFTWARE RIGHTS\r | |
4 | *\r | |
5 | * We reserve no LEGAL rights to the Purdue Compiler Construction Tool\r | |
6 | * Set (PCCTS) -- PCCTS is in the public domain. An individual or\r | |
7 | * company may do whatever they wish with source code distributed with\r | |
8 | * PCCTS or the code generated by PCCTS, including the incorporation of\r | |
9 | * PCCTS, or its output, into commerical software.\r | |
10 | *\r | |
11 | * We encourage users to develop software with PCCTS. However, we do ask\r | |
12 | * that credit is given to us for developing PCCTS. By "credit",\r | |
13 | * we mean that if you incorporate our source code into one of your\r | |
14 | * programs (commercial product, research project, or otherwise) that you\r | |
15 | * acknowledge this fact somewhere in the documentation, research report,\r | |
16 | * etc... If you like PCCTS and have developed a nice tool with the\r | |
17 | * output, please mention that you developed it using PCCTS. In\r | |
18 | * addition, we ask that this header remain intact in our source code.\r | |
19 | * As long as these guidelines are kept, we expect to continue enhancing\r | |
20 | * this system and expect to make other tools available as they are\r | |
21 | * completed.\r | |
22 | *\r | |
23 | * ANTLR 1.33\r | |
24 | * Terence Parr\r | |
25 | * Parr Research Corporation\r | |
26 | * with Purdue University and AHPCRC, University of Minnesota\r | |
27 | * 1989-2000\r | |
28 | */\r | |
29 | \r | |
30 | #include "pcctscfg.h"\r | |
31 | \r | |
32 | #include "pccts_stdio.h"\r | |
33 | #include "pccts_stdarg.h"\r | |
34 | \r | |
35 | PCCTS_NAMESPACE_STD\r | |
36 | \r | |
37 | #define ANTLR_SUPPORT_CODE\r | |
38 | \r | |
39 | #include "ASTBase.h"\r | |
40 | \r | |
41 | /* ensure that tree manipulation variables are current after a rule\r | |
42 | * reference\r | |
43 | */\r | |
44 | void\r | |
45 | ASTBase::link(ASTBase **_root, ASTBase **_sibling, ASTBase **_tail)\r | |
46 | {\r | |
47 | if ( *_sibling == NULL ) return;\r | |
48 | if ( *_root == NULL ) *_root = *_sibling;\r | |
49 | else if ( *_root != *_sibling ) (*_root)->_down = *_sibling;\r | |
50 | if ( *_tail==NULL ) *_tail = *_sibling;\r | |
51 | while ( (*_tail)->_right != NULL ) *_tail = (*_tail)->_right;\r | |
52 | }\r | |
53 | \r | |
54 | /* add a child node to the current sibling list */\r | |
55 | void\r | |
56 | ASTBase::subchild(ASTBase **_root, ASTBase **_sibling, ASTBase **_tail)\r | |
57 | {\r | |
58 | if ( *_tail != NULL ) (*_tail)->_right = this;\r | |
59 | else {\r | |
60 | *_sibling = this;\r | |
61 | if ( *_root != NULL ) (*_root)->_down = *_sibling;\r | |
62 | }\r | |
63 | *_tail = this;\r | |
64 | if ( *_root == NULL ) *_root = *_sibling;\r | |
65 | }\r | |
66 | \r | |
67 | /* make a new AST node. Make the newly-created\r | |
68 | * node the root for the current sibling list. If a root node already\r | |
69 | * exists, make the newly-created node the root of the current root.\r | |
70 | */\r | |
71 | void\r | |
72 | ASTBase::subroot(ASTBase **_root, ASTBase **_sibling, ASTBase **_tail)\r | |
73 | {\r | |
74 | if ( *_root != NULL )\r | |
75 | if ( (*_root)->_down == *_sibling ) *_sibling = *_tail = *_root;\r | |
76 | *_root = this;\r | |
77 | (*_root)->_down = *_sibling;\r | |
78 | }\r | |
79 | \r | |
80 | /* Apply preorder_action(), etc.. to root then each sibling */\r | |
81 | //\r | |
82 | // 7-Apr-97 133MR1\r | |
83 | // Fix suggested by Ron House (house@helios.usq.edu.au)\r | |
84 | //\r | |
85 | void\r | |
86 | ASTBase::preorder(void* pData /*= NULL*/ /* MR23 */)\r | |
87 | {\r | |
88 | ASTBase *tree = this;\r | |
89 | \r | |
90 | while ( tree!= NULL )\r | |
91 | {\r | |
92 | if ( tree->_down != NULL ) {\r | |
93 | tree->preorder_before_action(pData); // MR1 \r | |
94 | };\r | |
95 | tree->preorder_action(pData);\r | |
96 | if ( tree->_down!=NULL )\r | |
97 | {\r | |
98 | tree->_down->preorder(pData);\r | |
99 | tree->preorder_after_action(pData); // MR1\r | |
100 | }\r | |
101 | tree = tree->_right;\r | |
102 | }\r | |
103 | }\r | |
104 | \r | |
105 | /* free all AST nodes in tree; apply func to each before freeing */\r | |
106 | void\r | |
107 | ASTBase::destroy()\r | |
108 | {\r | |
109 | ASTBase* tree = this;\r | |
110 | while (tree) {\r | |
111 | if (tree->_down) tree->_down->destroy();\r | |
112 | \r | |
113 | ASTBase* cur = tree;\r | |
114 | tree = tree->_right;\r | |
115 | delete cur;\r | |
116 | }\r | |
117 | }\r | |
118 | \r | |
119 | /* build a tree (root child1 child2 ... NULL)\r | |
120 | * If root is NULL, simply make the children siblings and return ptr\r | |
121 | * to 1st sibling (child1). If root is not single node, return NULL.\r | |
122 | *\r | |
123 | * Siblings that are actually siblins lists themselves are handled\r | |
124 | * correctly. For example #( NULL, #( NULL, A, B, C), D) results\r | |
125 | * in the tree ( NULL A B C D ).\r | |
126 | *\r | |
127 | * Requires at least two parameters with the last one being NULL. If\r | |
128 | * both are NULL, return NULL.\r | |
129 | */\r | |
130 | ASTBase *\r | |
131 | ASTBase::tmake(ASTBase *root, ...)\r | |
132 | {\r | |
133 | va_list ap;\r | |
134 | register ASTBase *child, *sibling=NULL, *tail=NULL /*MR23*/, *w;\r | |
135 | \r | |
136 | va_start(ap, root);\r | |
137 | \r | |
138 | if ( root != NULL )\r | |
139 | if ( root->_down != NULL ) { \r | |
140 | root->reportOverwriteOfDownPointer(); /* MR21 Report problem which almost always an error */\r | |
141 | return NULL;\r | |
142 | }\r | |
143 | child = va_arg(ap, ASTBase *);\r | |
144 | while ( child != NULL )\r | |
145 | {\r | |
146 | for (w=child; w->_right!=NULL; w=w->_right) {;} /* find end of child */\r | |
147 | if ( sibling == NULL ) {sibling = child; tail = w;}\r | |
148 | else {tail->_right = child; tail = w;}\r | |
149 | child = va_arg(ap, ASTBase *);\r | |
150 | }\r | |
151 | if ( root==NULL ) root = sibling;\r | |
152 | else root->_down = sibling;\r | |
153 | va_end(ap);\r | |
154 | return root;\r | |
155 | }\r | |
156 | \r | |
157 | #ifndef PCCTS_NOT_USING_SOR\r | |
158 | \r | |
159 | /* tree duplicate */\r | |
160 | // forgot to check for NULL this (TJP July 23,1995)\r | |
161 | ASTBase *\r | |
162 | ASTBase::dup()\r | |
163 | {\r | |
164 | ASTBase *u, *t=this;\r | |
165 | \r | |
166 | if ( t == NULL ) return NULL;\r | |
167 | /*\r | |
168 | u = new ASTBase;\r | |
169 | *u = *t;\r | |
170 | */\r | |
171 | u = (ASTBase *)this->shallowCopy();\r | |
172 | if ( t->_right!=NULL ) u->_right = t->_right->dup();\r | |
173 | else u->_right = NULL;\r | |
174 | if ( t->_down!=NULL ) u->_down = t->_down->dup();\r | |
175 | else u->_down = NULL;\r | |
176 | return u;\r | |
177 | }\r | |
178 | #endif\r | |
179 | \r | |
180 | //\r | |
181 | // 7-Apr-97 133MR1\r | |
182 | // Fix suggested by Asgeir Olafsson (olafsson@cstar.ac.com)\r | |
183 | //\r | |
184 | /* tree duplicate */\r | |
185 | \r | |
186 | #ifndef PCCTS_NOT_USING_SOR\r | |
187 | \r | |
188 | ASTBase *\r | |
189 | ASTDoublyLinkedBase::dup()\r | |
190 | {\r | |
191 | ASTDoublyLinkedBase *u, *t=this;\r | |
192 | \r | |
193 | if ( t == NULL ) return NULL;\r | |
194 | u = (ASTDoublyLinkedBase *)this->shallowCopy();\r | |
195 | u->_up = NULL; /* set by calling invocation */\r | |
196 | u->_left = NULL;\r | |
197 | if (t->_right!=NULL) { // MR1\r | |
198 | u->_right=t->_right->dup(); // MR1\r | |
199 | ((ASTDoublyLinkedBase *)u->_right)->_left = u; // MR1\r | |
200 | } else { // MR1\r | |
201 | u->_right = NULL; // MR1\r | |
202 | }; // MR1\r | |
203 | if (t->_down!=NULL) { // MR1\r | |
204 | u->_down = t->_down->dup(); // MR1\r | |
205 | ((ASTDoublyLinkedBase *)u->_down)->_up = u; // MR1\r | |
206 | } else { // MR1\r | |
207 | u->_down = NULL; // MR1\r | |
208 | }; // MR1\r | |
209 | return u;\r | |
210 | }\r | |
211 | \r | |
212 | #endif\r | |
213 | \r | |
214 | /*\r | |
215 | * Set the 'up', and 'left' pointers of all nodes in 't'.\r | |
216 | * Initial call is double_link(your_tree, NULL, NULL).\r | |
217 | */\r | |
218 | void\r | |
219 | ASTDoublyLinkedBase::double_link(ASTBase *left, ASTBase *up)\r | |
220 | {\r | |
221 | ASTDoublyLinkedBase *t = this;\r | |
222 | \r | |
223 | t->_left = (ASTDoublyLinkedBase *) left;\r | |
224 | t->_up = (ASTDoublyLinkedBase *) up;\r | |
225 | if (t->_down != NULL)\r | |
226 | ((ASTDoublyLinkedBase *)t->_down)->double_link(NULL, t);\r | |
227 | if (t->_right != NULL)\r | |
228 | ((ASTDoublyLinkedBase *)t->_right)->double_link(t, up);\r | |
229 | }\r | |
230 | \r | |
231 | // MR21 ASTBase::reportOverwriteOfDownPointer\r | |
232 | \r | |
233 | void ASTBase::reportOverwriteOfDownPointer()\r | |
234 | {\r | |
235 | panic("Attempt to overwrite down pointer in ASTBase::tmake");\r | |
236 | }\r | |
237 | \r | |
238 | // MR21 ASTBase::panic\r | |
239 | \r | |
240 | void ASTBase::panic(const char *msg)\r | |
241 | {\r | |
242 | /* MR23 */ printMessage(stderr,"ASTBase panic: %s\n", msg);\r | |
243 | exit(PCCTS_EXIT_FAILURE);\r | |
244 | }\r | |
245 | \r | |
246 | #ifdef PCCTS_NOT_USING_SOR\r | |
247 | //MR23\r | |
248 | int ASTBase::printMessage(FILE* pFile, const char* pFormat, ...)\r | |
249 | {\r | |
250 | va_list marker;\r | |
251 | va_start( marker, pFormat );\r | |
252 | int iRet = vfprintf(pFile, pFormat, marker);\r | |
253 | va_end( marker );\r | |
254 | return iRet;\r | |
255 | }\r | |
256 | #endif\r |