-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasttost.cpp
More file actions
359 lines (311 loc) · 13.4 KB
/
Copy pathasttost.cpp
File metadata and controls
359 lines (311 loc) · 13.4 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
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
#include "psg.hpp"
#include "lexicon.hpp"
#include "asttost.hpp"
#include <cstring>
#include <fstream>
#include <iostream>
#include <stack>
#include <list>
void convertFunctionForm(Node *functionFormNode)
{
// cout<<"\nInside function form conversion!\nFunction form is:\n";
// if(functionFormNode->nextSibling != NULL) {
// cout<<"functionFormNode's sibling is: "<<functionFormNode->nextSibling->label<<"\n";
// }
// recursivelyPrintTreeNode(functionFormNode, "");
Node *fcnLambdaRightChildNodeHeader = new Node; // the "lambda" right child node header of the final standardized sub-tree
fcnLambdaRightChildNodeHeader->label = LAMBDA_STD_LABEL;
fcnLambdaRightChildNodeHeader->nextSibling = NULL;
list<Node *> fcnVariableList;
functionFormNode->label = "="; // the "=" header node of the final standardized sub-tree
Node *temp = functionFormNode->firstKid; // the fcn label left child node of the final standardized sub-tree
while (temp->nextSibling->nextSibling !=
NULL)
{ // temp->nextSibling->nextSibling == NULL implies temp->nextSibling is the "Expression" part of the fcnForm
temp = temp->nextSibling;
fcnVariableList.push_back(temp);
}
// temp = temp->nextSibling; //this would be the expression part of the fcn form //the final expression node which is the rightMost child of the right sub-tree
functionFormNode->firstKid->nextSibling = fcnLambdaRightChildNodeHeader;
fcnLambdaRightChildNodeHeader->nextSibling = NULL;
fcnLambdaRightChildNodeHeader->firstKid = fcnVariableList.front();
Node *lambdaTemp = fcnLambdaRightChildNodeHeader;
fcnVariableList.pop_front();
while (fcnVariableList.size() > 0)
{
Node *newLambdaRightNode = new Node;
lambdaTemp->firstKid->nextSibling = newLambdaRightNode;
newLambdaRightNode->label = LAMBDA_STD_LABEL;
newLambdaRightNode->nextSibling = NULL;
lambdaTemp = newLambdaRightNode;
lambdaTemp->firstKid = fcnVariableList.front();
fcnVariableList.pop_front();
}
// lambdaTemp->firstKid->nextSibling = temp;
// cout<<"\nInside function form conversion!\nThe standardized Function form is:\n";
// recursivelyPrintTreeNode(functionFormNode, "");
//
// if(functionFormNode->nextSibling != NULL) {
// cout<<"functionFormNode's sibling is: "<<functionFormNode->nextSibling->label<<"\n";
// }
}
void convertInfixOperator(Node *infixOperatorNode)
{
// cout<<"\nInside Infix Operator conversion!\nInfix ast form before standardizing is:\n";
// recursivelyPrintTree(opNode, "");
Node *leftGammaChild = new Node;
Node *leftLeftOperatorChild = new Node;
leftLeftOperatorChild->label = infixOperatorNode->firstKid->nextSibling->label;
leftLeftOperatorChild->nextSibling = infixOperatorNode->firstKid; // E1
leftLeftOperatorChild->firstKid = NULL;
leftGammaChild->label = GAMMA_STD_LABEL;
leftGammaChild->firstKid = leftLeftOperatorChild;
leftGammaChild->nextSibling = infixOperatorNode->firstKid->nextSibling->nextSibling; // E2
infixOperatorNode->firstKid->nextSibling = NULL;
infixOperatorNode->firstKid = leftGammaChild;
infixOperatorNode->label = GAMMA_STD_LABEL;
// cout<<"\nThe standardized Infix operator is:\n";
// recursivelyPrintTree(opNode, "");
}
void convertLambdaExpression(Node *lambdaNode)
{
// cout << "\nInside lambda expression conversion!\n lambda expression ast form before standardizing is:\n";
// recursivelyPrintTreeNode(lambdaNode, "");
lambdaNode->label = LAMBDA_STD_LABEL;
list<Node *> fcnVariableList;
Node *temp = lambdaNode->firstKid; // the top left child node of the final standardized lambda sub-tree
while (temp->nextSibling->nextSibling !=
NULL)
{ // temp->nextSibling->nextSibling == NULL implies temp->nextSibling is the "Expression" part of the fcnForm
temp = temp->nextSibling;
fcnVariableList.push_back(temp);
}
temp = temp->nextSibling; // this would be the expression part of the fcn form //the final expression node which is the rightMost child of the right sub-tree
Node *lambdaTemp = lambdaNode;
while (fcnVariableList.size() > 0)
{
Node *newLambdaRightNode = new Node;
lambdaTemp->firstKid->nextSibling = newLambdaRightNode;
newLambdaRightNode->nextSibling = NULL;
newLambdaRightNode->label = LAMBDA_STD_LABEL;
lambdaTemp = newLambdaRightNode;
lambdaTemp->firstKid = fcnVariableList.front();
fcnVariableList.pop_front();
}
lambdaTemp->firstKid->nextSibling = temp; // E
// cout << "\nThe standardized lambda expression is:\n";
// recursivelyPrintTreeNode(lambdaNode, "");
}
void convertRecExpression(Node *recNode)
{
// cout<< "\nThe recNode label is: "<<recNode->label<<"\n";
// cout << "\nInside convertRecExpression conversion!\nrecNode ast form before standardizing is:\n";
// recursivelyPrintTreeNode(recNode, "");
// cout<< "\nThe recNode label is: "<<recNode->label<<"\n";
//
// if(recNode->nextSibling != NULL) {
// cout<<"recNode's sibling is: "<<recNode->nextSibling->label<<"\n";
// }
//
// if(recNode->firstKid->nextSibling != NULL) {
// cout<<"recNode's firstKid's sibling is: "<<recNode->firstKid->nextSibling->label<<"\n";
// }
Node *recNodeOriginalEqualsChild = recNode->firstKid;
recNode->label = recNodeOriginalEqualsChild->label;
recNode->firstKid = recNodeOriginalEqualsChild->firstKid;
Node *rightGammaChild = new Node;
rightGammaChild->label = GAMMA_STD_LABEL;
rightGammaChild->nextSibling = NULL;
Node *rightRightLambdaChild = new Node;
rightRightLambdaChild->label = LAMBDA_STD_LABEL;
rightRightLambdaChild->nextSibling = NULL;
Node *leftChildYNode = new Node;
leftChildYNode->label = "Y";
leftChildYNode->firstKid = NULL;
rightGammaChild->firstKid = leftChildYNode;
leftChildYNode->nextSibling = rightRightLambdaChild;
Node *functionNameNode = new Node;
functionNameNode->label = recNode->firstKid->label;
functionNameNode->firstKid = NULL;
rightRightLambdaChild->firstKid = functionNameNode;
functionNameNode->nextSibling = recNode->firstKid->nextSibling; // E
recNode->firstKid->nextSibling = rightGammaChild;
// cout<< "\nThe recNode label is: "<<recNode->label<<"\n";
// cout << "\nInside convertRecExpression conversion!\nrecNode ast form after standardizing is:\n";
// recursivelyPrintTreeNode(recNode, "");
// cout<< "\nThe recNode label is: "<<recNode->label<<"\n";
}
void convertWhereExpression(Node *whereNode)
{
whereNode->label = GAMMA_STD_LABEL;
Node *pNode = whereNode->firstKid;
Node *leftChildLambdaNode = pNode->nextSibling;
leftChildLambdaNode->label = LAMBDA_STD_LABEL;
Node *eNode = leftChildLambdaNode->firstKid->nextSibling;
whereNode->firstKid = leftChildLambdaNode;
// switch the p and e nodes
leftChildLambdaNode->nextSibling = eNode;
leftChildLambdaNode->firstKid->nextSibling = pNode;
pNode->nextSibling = NULL;
}
void convertWithinExpression(Node *withinNode)
{
withinNode->label = "=";
Node *withinOne = withinNode->firstKid;
Node *withinTwo = withinOne->nextSibling;
Node *rightGammaChild = new Node;
Node *rightLeftLambdaChild = new Node;
rightGammaChild->label = GAMMA_STD_LABEL;
rightGammaChild->nextSibling = NULL;
rightLeftLambdaChild->label = LAMBDA_STD_LABEL;
rightGammaChild->firstKid = rightLeftLambdaChild;
rightLeftLambdaChild->nextSibling = withinOne->firstKid->nextSibling; // E1
rightLeftLambdaChild->firstKid = withinOne->firstKid; // X1
rightLeftLambdaChild->firstKid->nextSibling = withinTwo->firstKid->nextSibling; // E2
withinNode->firstKid = withinTwo->firstKid; // X2
withinNode->firstKid->nextSibling = rightGammaChild;
}
void convertAndExpression(Node *andHeaderNode)
{
andHeaderNode->label = "=";
Node *tempEqualsChildHeaderNode = andHeaderNode->firstKid;
list<Node *> variableNodesList;
list<Node *> expressionNodesList;
while (tempEqualsChildHeaderNode != NULL)
{
variableNodesList.push_back(tempEqualsChildHeaderNode->firstKid);
expressionNodesList.push_back(tempEqualsChildHeaderNode->firstKid->nextSibling);
tempEqualsChildHeaderNode = tempEqualsChildHeaderNode->nextSibling;
}
Node *commaHeaderNode = new Node;
Node *tauHeaderNode = new Node;
commaHeaderNode->label = ",";
tauHeaderNode->label = "tau";
tauHeaderNode->nextSibling = NULL;
commaHeaderNode->nextSibling = tauHeaderNode;
andHeaderNode->firstKid = commaHeaderNode;
Node *commaVariableTempNode, *tauExpressionTempNode;
commaVariableTempNode = variableNodesList.front();
variableNodesList.pop_front();
tauExpressionTempNode = expressionNodesList.front();
expressionNodesList.pop_front();
commaHeaderNode->firstKid = commaVariableTempNode;
tauHeaderNode->firstKid = tauExpressionTempNode;
while (!variableNodesList.empty())
{
commaVariableTempNode->nextSibling = variableNodesList.front();
variableNodesList.pop_front();
tauExpressionTempNode->nextSibling = expressionNodesList.front();
expressionNodesList.pop_front();
commaVariableTempNode = commaVariableTempNode->nextSibling;
tauExpressionTempNode = tauExpressionTempNode->nextSibling;
}
commaVariableTempNode->nextSibling = NULL;
tauExpressionTempNode->nextSibling = NULL;
}
void convertLetExpression(Node *letNode)
{
// cout << "\nInside convertLetExpression conversion!\nletNode ast form before standardizing is:\n";
// recursivelyPrintTreeNode(letNode, "");
letNode->label = GAMMA_STD_LABEL;
letNode->firstKid->label = LAMBDA_STD_LABEL;
Node *pNode = letNode->firstKid->nextSibling;
Node *eNode = letNode->firstKid->firstKid->nextSibling;
// switch the p and e nodes
letNode->firstKid->nextSibling = eNode;
letNode->firstKid->firstKid->nextSibling = pNode;
// cout << "\nInside convertLetExpression conversion!\nletNode ast form after standardizing is:\n";
// recursivelyPrintTreeNode(letNode, "");
}
void recursivelyStandardizeTree(Node *node)
{
if (node->firstKid != NULL)
{
recursivelyStandardizeTree(node->firstKid);
}
if (node->nextSibling != NULL)
{
recursivelyStandardizeTree(node->nextSibling);
}
if (node->label == "->")
{
// Do not standardize conditionals (optimizations for the CISE machine)
}
else if (node->label == "not" || node->label == "neg")
{ // convert unary operators to standardized form
// Do not standardize unary operators (optimizations for the CISE machine) //convertUop(node);
}
else if (node->label == "aug" || node->label == "or" || node->label == "&" || node->label == "gr" ||
node->label == "ge" || node->label == "ls" || node->label == "le" || node->label == "eq" ||
node->label == "ne" || node->label == "+" || node->label == "-" || node->label == "*" ||
node->label == "/" || node->label == "**")
{
// Do not standardize binary operators (optimizations for the CISE machine) //convertOperator(node);
}
else if (node->label == "tau")
{
// Do not standardize tau (optimizations for the CISE machine)
}
else if (node->label == "lambda")
{ // convert lambda expression to standardized form
if (node->firstKid->label == ",")
{ // lambda expression with a tuple of variables
// Do not standardize lambda with a tuple of variables (optimizations for the CISE machine)
}
else
{ // lambda expression with a list(?) of variable(s)
// cout << "\nGoing to convertLambdaExpression\n";
convertLambdaExpression(node);
}
}
else if (node->label == FCN_FORM_LABEL)
{ // convert function_form to standardized form
// cout << "\nGoing to convertFunctionForm\n";
convertFunctionForm(node);
}
else if (node->label == "@")
{ // convert infix operator to standardized form
// cout << "\nGoing to convertInfixOperator\n";
convertInfixOperator(node);
}
else if (node->label == "and")
{
// cout << "\nGoing to convertAndExpression\n";
convertAndExpression(node);
}
else if (node->label == "within")
{
// cout << "\nGoing to convertWithinExpression\n";
convertWithinExpression(node);
}
else if (node->label == "rec")
{
// cout << "\nGoing to convertRecExpression for nodeLabel= " << node->label << "\n";
convertRecExpression(node);
// cout << "\nAfter convertRecExpression for nodeLabel= " << node->label << "\n";
}
else if (node->label == "let")
{
// cout << "\nGoing to convertLetExpression\n";
convertLetExpression(node);
}
else if (node->label == "where")
{
// cout << "\nGoing to convertWhereExpression\n";
convertWhereExpression(node);
}
}
void convertASTToStandardizedTree()
{
// cout << "\n\nGoing to standardize the tree now!\n\n";
if (trees.empty())
{
return;
}
else
{
// cout << "\n\nThis is supposed to be the only tree below!\n";
Node *treeRootOfAST = trees.top();
recursivelyStandardizeTree(treeRootOfAST);
}
}