-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAstNode.java
More file actions
133 lines (117 loc) · 4.38 KB
/
AstNode.java
File metadata and controls
133 lines (117 loc) · 4.38 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
/**
* Represents a head node for some Abstract Syntax Tree (or it's subtree).
* Content of a node - {@code AstExpression}.
*/
public class AstNode {
private AstExpression expression;
private AstNode parent, lChild, rChild;
public AstNode(AstExpression exp) {
expression = exp;
parent = null;
lChild = null;
rChild = null;
}
public AstExpression getExpression() {
return expression;
}
public AstNode getParent() {
return parent;
}
public AstNode getLeftChild() {
return lChild;
}
public void setLeftChild(AstNode lChild) {
this.lChild = lChild;
if (lChild != null) {
lChild.parent = this;
}
}
public AstNode getRightChild() {
return rChild;
}
public void setRightChild(AstNode rChild) {
this.rChild = rChild;
if (rChild != null) {
rChild.parent = this;
}
}
@Override
public String toString() {
return "{ expression : " + expression +
", leftChild : " + lChild.hashCode() +
", rightChild : " + rChild.hashCode() + "}";
}
/**
* Print tree/subtree, which is starting from this node.
* Format of output:
* ├── el1<p>
* │ ├── el3<p>
* │ └── el4<p>
* └── el2
*/
public void printSubtree() {
printSubtree("", true);
}
private void printSubtree(String prefix, boolean isTail) {
System.out.println(prefix + (isTail ? "└── " : "├── ") + expression.getContent());
if (lChild != null) {
lChild.printSubtree(prefix + (isTail ? " " : "│ "), rChild == null);
if (rChild != null) {
rChild.printSubtree(prefix + (isTail ? " " : "│ "), true);
}
} else if (rChild != null) {
rChild.printSubtree(prefix + (isTail ? " " : "│ "), true);
}
}
public String actOnSubtree() throws ExpressionFormatException {
String[] args = {"", ""};
if (lChild != null) {
args[0] = lChild.actOnSubtree();
}
if (rChild != null) {
args[1] = rChild.actOnSubtree();
}
return expression.performAction(args);
}
/**
* Creates a JSON representation of this subtree. May be used instead of {@code toString} for subtree.
*
* @param tabulation String for pre-line tabulation
* @param endLine String at hte end of each line (for example, {@code "\n"})
* @return String in JSON format with this subtree
*/
public String subtreeToJson(String tabulation, String endLine) {
return subtreeToJson("", tabulation, endLine);
}
private String subtreeToJson(String prefix, String tab, String nl) {
String res = "{" + nl;
res += prefix + tab + "\"expression\" : \"" + expression.getContent() + "\"," + nl;
res += prefix + tab + "\"leftChild\" : " +
(lChild == null ? "null" : lChild.subtreeToJson(prefix + tab, tab, nl)) + "," + nl;
res += prefix + tab + "\"rightChild\" : " +
(rChild == null ? "null" : rChild.subtreeToJson(prefix + tab, tab, nl)) + "," + nl;
return res + prefix + "}";
}
/**
* Creates an XML representation of this subtree. May be used instead of {@code toString} for subtree.
*
* @param tabulation String for pre-line tabulation
* @param endLine String at hte end of each line (for example, {@code "\n"})
* @return String in XML format with this subtree
*/
public String subtreeToXml(String tabulation, String endLine) {
return subtreeToXml("", tabulation, endLine);
}
private String subtreeToXml(String prefix, String tab, String nl) {
String res = "<AstNode expression=\"" + expression.getContent() + "\">" + nl;
res += prefix + tab + "<leftChild>" +
(lChild == null ? "" : nl + prefix + tab + tab +
lChild.subtreeToXml(prefix + tab + tab, tab, nl) + nl + prefix + tab) +
"</LeftChild>" + nl;
res += prefix + tab + "<RightChild>" +
(rChild == null ? "" : nl + prefix + tab + tab +
rChild.subtreeToXml(prefix + tab + tab, tab, nl) + nl + prefix + tab) +
"</RightChild>" + nl;
return res + prefix + "</AstNode>";
}
}