-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleNode.java
More file actions
432 lines (386 loc) · 15.8 KB
/
Copy pathSimpleNode.java
File metadata and controls
432 lines (386 loc) · 15.8 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
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
/* Generated By:JJTree: Do not edit this line. SimpleNode.java Version 7.0 */
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=false,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
import java.util.HashSet;
import java.util.Set;
import java.util.Random;
public
class SimpleNode implements Node {
protected Node parent;
protected Node[] children;
protected int id;
protected Object value;
protected Prog2 parser;
public SimpleNode(int i) {
id = i;
}
public SimpleNode(Prog2 p, int i) {
this(i);
parser = p;
}
public void jjtOpen() {
}
public void jjtClose() {
}
public void jjtSetParent(Node n) {
parent = n;
}
public Node jjtGetParent() {
return parent;
}
public void jjtAddChild(Node n, int i) {
if (children == null) {
children = new Node[i + 1];
} else if (i >= children.length) {
Node[] c = new Node[i + 1];
System.arraycopy(children, 0, c, 0, children.length);
children = c;
}
children[i] = n;
}
public Node jjtGetChild(int i) {
return children[i];
}
public int jjtGetNumChildren() {
return (children == null) ? 0 : children.length;
}
public void jjtSetValue(Object value) {
this.value = value;
}
/* You can override these two methods in subclasses of SimpleNode to
customize the way the node appears when the tree is dumped. If
your output uses more than one line you should override
toString(String), otherwise overriding toString() is probably all
you need to do. */
public Object jjtGetValue() {
return value;
}
public String toString() {
return Constants.jjtNodeName[id];
}
public String toString(String prefix) {
return prefix + this;
}
public int getId() {
return id;
}
public SimpleNode deepCopy() {
SimpleNode copy;
if (this instanceof ASTInt originalIntNode) {
copy = new ASTInt(Constants.JJTINT);
((ASTInt) copy).setName(originalIntNode.getName());
copy.jjtSetValue(originalIntNode.getName());
} else if (this instanceof ASTID originalIdNode) {
copy = new ASTID(Constants.JJTID);
((ASTID) copy).setName(originalIdNode.getName());
copy.jjtSetValue(originalIdNode.getName());
} else {
copy = new SimpleNode(id);
copy.jjtSetValue(toString());
}
for (int i = 0; i < jjtGetNumChildren(); i++) {
SimpleNode child = (SimpleNode) jjtGetChild(i);
copy.jjtAddChild(child.deepCopy(), i);
}
return copy;
}
private String generateUnusedRandomLetter(String var, Set<String> lettersHashSet) {
Random random = new Random();
String randomLetter;
do {
randomLetter = Character.toString((char) (random.nextInt(26) + 'A'));
} while (lettersHashSet.contains(randomLetter.toLowerCase()) && randomLetter.toLowerCase().equals(var));
return randomLetter.toLowerCase();
}
public void astToString() {
switch (Constants.jjtNodeName[id]) {
case "Start":
jjtGetChild(0).astToString();
break;
case "appl":
System.out.print("(");
jjtGetChild(0).astToString();
System.out.print(" ");
jjtGetChild(1).astToString();
System.out.print(")");
break;
case "lamb":
System.out.print("(L ");
jjtGetChild(0).astToString();
System.out.print(" . ");
jjtGetChild(1).astToString();
System.out.print(")");
break;
case "ID":
case "Int":
System.out.print(this);
break;
case "add":
System.out.print("+ ");
break;
case "sub":
System.out.print("- ");
break;
case "mul":
System.out.print("* ");
break;
case "div":
System.out.print("/ ");
break;
case "mod":
System.out.print("% ");
break;
}
}
public void dumpAST(String prefix) {
if (!toString().equals("Start")) {
System.out.println(toString(prefix));
}
if (children != null) {
for (Node child : children) {
if (child != null) {
if (!toString().equals("Start")) {
((SimpleNode) child).dumpAST(prefix + " ");
} else {
((SimpleNode) child).dumpAST(prefix);
}
}
}
}
}
public Set<String> FV() {
Set<String> fv = new HashSet<>();
addFV(fv);
return fv;
}
public void addFV(Set<String> fv) {
switch (Constants.jjtNodeName[id]) {
case "Start":
jjtGetChild(0).addFV(fv);
break;
case "lamb":
fv.addAll(jjtGetChild(1).FV());
fv.remove(jjtGetChild(0).toString());
break;
case "ID":
fv.add(toString());
break;
case "appl":
fv.addAll(jjtGetChild(0).FV());
fv.addAll(jjtGetChild(1).FV());
break;
default:
break;
}
}
public void dumpFV(String prefix, Set<String> fv) {
if (!toString().equals("Start")) {
System.out.println(toString(prefix) + " " + FV());
}
if (children != null) {
for (Node child : children) {
if (child != null) {
if (!toString().equals("Start")) {
child.dumpFV((prefix + " "), FV());
} else {
child.dumpFV((prefix + " "), FV());
}
}
}
}
}
public SimpleNode performSubstitution(String var, SimpleNode expr) {
if (id == Constants.JJTID) {
ASTID originalIdNode = (ASTID) this;
if (originalIdNode.toString().equals(var)) {
return expr;
}
return this;
} else if (id == Constants.JJTLAMB) {
//System.out.println("in subst 1");
if (jjtGetChild(0).toString().equals(var)) {
//System.out.println("Do nothing and return same lamb expression");
} else {
if (!(expr.FV().contains(jjtGetChild(0).toString()))) {
//System.out.println("parameter of lambda not in free v of expr");
SimpleNode newRight = jjtGetChild(1).performSubstitution(var, expr);
this.jjtAddChild(newRight, 1);
return this;
} else {
//System.out.println("parameter of lambda is in free v of expr");
SimpleNode old = (SimpleNode) jjtGetChild(0);
String renamingParam = generateUnusedRandomLetter(var, expr.FV());
ASTID newParam = new ASTID(Constants.JJTID);
newParam.setName(renamingParam);
this.jjtAddChild(newParam, 0);
SimpleNode newNode = jjtGetChild(1).performSubstitution(old.toString(), newParam);
this.jjtAddChild(newNode, 1);
this.performSubstitution(var, expr);
}
}
return this;
} else if (id == Constants.JJTAPPL) {
//System.out.println("In application substitution");
SimpleNode newRight = jjtGetChild(1).performSubstitution(var, expr);
this.jjtAddChild(newRight.deepCopy(), 1);
SimpleNode newLeft = jjtGetChild(0).performSubstitution(var, expr);
this.jjtAddChild(newLeft.deepCopy(), 0);
return this;
} else {
return this;
}
}
public SimpleNode substitute(String var, SimpleNode expr) {
if (children != null) {
return children[0].performSubstitution(var, expr);
}
return null;
}
private boolean isBetaReduxTree() {
//System.out.println("inside isBetaReduxTree().." + toString());
//System.out.println("isBetaReduxTree().. dump here"); this.dumpAST("");
if (jjtGetNumChildren() > 0) {
//System.out.println("isBetaReduxTree()-> jjtGetNumChildren() > 0..");
if (isBetaRedux()) {
//System.out.println("isBetaReduxTree()->it is beta redux node..");
return true;
} else {
//System.out.println("isBetaReduxTree()-> checking its left child and right child for reduxes..");
if (jjtGetNumChildren() != 0) {
//System.out.println("inside isBetaReduxTree() else part..");
return ((SimpleNode) this.jjtGetChild(0)).isBetaReduxTree() || (this.jjtGetNumChildren() > 1 && ((SimpleNode) this.jjtGetChild(1)).isBetaReduxTree());
}
}
}
return false;
}
public SimpleNode normalOrderEvaluate() {
SimpleNode currentNode = this;
while (currentNode.isBetaReduxTree()) {
//System.out.println("normalOrderEvaluate --> isBetaReduxTree() returned ture ... it is beta redux tree inside while..");
//System.out.println("normalOrderEvaluate --> dump before.."); currentNode.dumpAST("");
//System.out.println("dump ends ------------------- \n");
currentNode = currentNode.normalOrderEvaluateNode();
//System.out.println("normalOrderEvaluate --> dump after.."); currentNode.dumpAST("");
//System.out.println("dump ends ------------------- \n");
}
//System.out.println("No more beta redux");
while (currentNode.isDeltaReduxTree()) {
//System.out.println("inside while currentNode.isDeltaReduxTree() returns true");
currentNode = currentNode.deltaReduceTree();
}
//System.out.println("delta finish");
return currentNode;
}
private SimpleNode normalOrderEvaluateNode() {
//System.out.println("inside normalOrderEvaluateNode..");
if (isBetaRedux()) {
//System.out.println("normalOrderEvaluateNode --> isBetaRedux() true...");
return betaReduce();
} else if (((SimpleNode) jjtGetChild(0)).isBetaReduxTree()) {
//System.out.println("normalOrderEvaluateNode --> else if jjtGetChild(0)).isBetaReduxTree()...");
SimpleNode leftChild = ((SimpleNode) jjtGetChild(0)).normalOrderEvaluateNode();
this.jjtAddChild(leftChild, 0);
} else {
//System.out.println("normalOrderEvaluateNode --> else....");
SimpleNode rightChild = ((SimpleNode) jjtGetChild(1)).normalOrderEvaluateNode();
this.jjtAddChild(rightChild, 1);
}
return this;
}
private boolean isBetaRedux() {
//System.out.println("inside isBetaRedux()..");
return jjtGetNumChildren() > 1 && this.getId() == Constants.JJTAPPL && jjtGetChild(0).getId() == Constants.JJTLAMB;
}
private SimpleNode betaReduce() {
//System.out.println("inside betaReduce()");
SimpleNode lambParameter = (SimpleNode) this.jjtGetChild(0).jjtGetChild(0);
SimpleNode lambBody = (SimpleNode) this.jjtGetChild(0).jjtGetChild(1);
SimpleNode rightChildApp = (SimpleNode) this.jjtGetChild(1);
return lambBody.performSubstitution(lambParameter.toString(), rightChildApp);
}
private boolean isDeltaReduxTree() {
//System.out.println("Inside isDeltaReduxTree().. id is" + this.id + "toString is :: " + this.toString());
if (jjtGetNumChildren() == 0) {
return false;
} else if (id == Constants.JJTAPPL) {
//System.out.println("its appl inside else-if");
if (isDeltaReduxNode()) {
//System.out.println("isDeltaReduxNode() returns true");
return true;
} else {
//System.out.println("isDeltaReduxNode else");
return ((SimpleNode) this.jjtGetChild(0)).isDeltaReduxTree() ||
(this.jjtGetNumChildren() > 1 &&
((SimpleNode) this.jjtGetChild(1)).isDeltaReduxTree());
}
} else {
return (this.jjtGetNumChildren() > 1 && ((SimpleNode) this.jjtGetChild(1)).isDeltaReduxTree());
}
}
private boolean isDeltaReduxNode() {
//System.out.println("inside isDeltaReduxNode().... id here is :: " + this.id); this.toString();
if (jjtGetNumChildren() == 0) {
return false;
} else if (id == Constants.JJTAPPL) {
if (jjtGetChild(0).jjtGetNumChildren() == 0) {
return false;
} else {
boolean isOperatorNode = ((SimpleNode) (this.jjtGetChild(0).jjtGetChild(0))).id == Constants.JJTADD ||
((SimpleNode) (this.jjtGetChild(0).jjtGetChild(0))).id == Constants.JJTSUB ||
((SimpleNode) (this.jjtGetChild(0).jjtGetChild(0))).id == Constants.JJTMUL ||
((SimpleNode) (this.jjtGetChild(0).jjtGetChild(0))).id == Constants.JJTDIV||
((SimpleNode) (this.jjtGetChild(0).jjtGetChild(0))).id == Constants.JJTMOD;
//System.out.println("isOperatorNode :: " + isOperatorNode);
return (
isOperatorNode &&
(((SimpleNode) jjtGetChild(1)).id == Constants.JJTINT || ((SimpleNode) jjtGetChild(1)).id == Constants.JJTID) &&
(((SimpleNode) jjtGetChild(0).jjtGetChild(1)).id == Constants.JJTINT || ((SimpleNode) jjtGetChild(0).jjtGetChild(1)).id == Constants.JJTID)
);
}
} else {
return false;
}
}
public SimpleNode deltaReduceTree() {
//System.out.println("insise deltaReduceTree... dump here is :: "); this.dumpAST("");
SimpleNode clonedNode = this;
if (clonedNode.isDeltaReduxNode()) {
clonedNode = clonedNode.deltaReduceNode();
return clonedNode;
} else if (((SimpleNode) this.jjtGetChild(0)).isDeltaReduxTree()) {
SimpleNode leftChild = ((SimpleNode) clonedNode.jjtGetChild(0)).deltaReduceTree();
clonedNode.jjtAddChild(leftChild, 0);
return clonedNode;
} else {
SimpleNode rightChild = ((SimpleNode) clonedNode.jjtGetChild(1)).deltaReduceTree();
clonedNode.jjtAddChild(rightChild, 1);
return clonedNode;
}
}
private SimpleNode deltaReduceNode() {
SimpleNode leftRightGrandchild = (SimpleNode) this.jjtGetChild(0).jjtGetChild(1);
int leftC = Integer.parseInt(leftRightGrandchild.toString());
int rightC = Integer.parseInt(this.jjtGetChild(1).toString());
int result = 0;
switch (((SimpleNode) (this.jjtGetChild(0).jjtGetChild(0))).id) {
case Constants.JJTADD:
result = leftC + rightC;
break;
case Constants.JJTSUB:
result = leftC - rightC;
break;
case Constants.JJTMUL:
result = leftC * rightC;
break;
case Constants.JJTDIV:
result = leftC / rightC;
break;
case Constants.JJTMOD:
result = leftC % rightC;
break;
}
ASTInt tempNode = new ASTInt(((SimpleNode) this.jjtGetChild(1)).id);
tempNode.setName(String.valueOf(result));
return tempNode;
}
}