-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl2m_LRLTree.java
More file actions
330 lines (303 loc) · 8.95 KB
/
l2m_LRLTree.java
File metadata and controls
330 lines (303 loc) · 8.95 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
/*
* Christopher Helmer
* Loren Milliman
* TCSS 342 Sp14
* HW3
* LRLTree.java
* Due: 5/6/14
*/
import java.io.*;
import java.util.*;
/**
* @author Christopher Helmer
* @author Loren Milliman
* @version TCSS342sp14
*
*/
public class LRLTree {
/** The String representation of the indent for the toJavaString method. */
final private static String INDENT = " ";
/** Node for the LRL Tree. */
class TreeNode {
private String data;
private TreeNode left;
private TreeNode right;
/** Construct an empty TreeNode. */
private TreeNode() {
data = "";
left = right = null;
}
/** Constructs a TreeNode with the given data. */
private TreeNode(final String theData) {
data = theData;
left = right = null;
}
} // end of TreeNode
/** A reference to the root of the LRL Tree. */
final private TreeNode root;
/** Maintains a list of all variables and their current values. */
final private HashMap<String, Integer> env;
/**
* Constructs an LRL Tree with the given LRL code.
* @param anInputFile the raw LRL code
* @throws FileNotFoundException If the referenced file cannot be found.
*/
public LRLTree(final String anInputFile) throws FileNotFoundException {
final Scanner input = new Scanner(new File(anInputFile));
env = new HashMap<String, Integer>();
root = buildTree(input);
populateEnv(root);
}
/**
* Recursively build LRL Tree from LRL input file.
* @param s A Scanner to an LRL File.
* @return A TreeNode
*/
private TreeNode buildTree(Scanner s) {
TreeNode tempNode = new TreeNode();
String str = s.next();
while ((str.equals(")") && s.hasNext()) || str.equals("(")) {
str = s.next();
}
tempNode.data = str; // base case
if(isOperator(str)) { // recursive case
tempNode.left = buildTree(s);
if (!str.equals("print")) {
tempNode.right = buildTree(s);
}
}
return tempNode;
}
/**
* Determines if the given string is an LRL operator.
* @param theString the provided token.
* @return TRUE if the given string is an operator.
*/
private boolean isOperator(String str) {
return str.equals("+") || str.equals("-") || str.equals("*")
|| str.equals("/") || str.equals("==") || str.equals("=")
|| str.equals("<") || str.equals(">") || str.equals("if")
|| str.equals("while") || str.equals("block") || str.equals("print");
}
/**
* Recursively trace the LRL tree and assign variables to keys in the env hashMap.
* @param aNode the current Node called upon.
*/
private void populateEnv(TreeNode aNode) {
if(aNode.data.equals("=") && isInteger(aNode.right.data)) { //base case
env.put(aNode.left.data, Integer.parseInt(aNode.right.data));
}
if(aNode.right != null) { //if you can go right, then you must be able to go left
populateEnv(aNode.left); //go left first
populateEnv(aNode.right);
}
}
/**
* Determines if the given String is an integer.
* @param str A given String.
* @return True if the String is an integer.
*/
private boolean isInteger(String str) {
if(str == null) {
return false;
} else {
try {
Integer.parseInt(str);
} catch(NumberFormatException e) {
return false;
}
}
return true;
}
/**
* Produce console output for the LRL Tree.
*/
public void print() {
print(root);
}
/**
* Recursively trace the LRL Tree and output to console.
* @param aNode is the input node to print.
*/
private void print(TreeNode aNode) {
if(aNode == null) {
System.out.print(")");
} else if(!isOperator(aNode.data)) {
System.out.print(aNode.data + " ");
} else if(aNode.data.equals("print")) {
System.out.print("(" + aNode.data + " ");
print(aNode.left);
System.out.print(")");
} else {
System.out.print("(" + aNode.data + " ");
print(aNode.left);
print(aNode.right);
System.out.print(")");
}
}
/**
* Evaluate (execute) the LRL code.
* @return 0
*/
public int eval() {
return eval(root);
}
/**
* Recursively evaluates the LRL code in the LRL tree.
* @param aNode is the node from which to evaluate the LRL code.
* @return an integer representation of the leaves of the LRL tree.
*/
private int eval(TreeNode aNode) {
if(aNode.data.equals("print")) {
System.out.println(eval(aNode.left));
}
if(aNode.data.equals("block")) {
eval(aNode.left);
eval(aNode.right);
}
if(aNode.data.equals("while")) {
handleWhile(aNode.left, aNode.right);
}
if(aNode.data.equals("=")) {
env.put(aNode.left.data, eval(aNode.right));
}
if(isInteger(aNode.data)) {
return Integer.parseInt(aNode.data);
}
if(!isOperator(aNode.data) && !isInteger(aNode.data)) {
return env.get(aNode.data);
}
if(aNode.data.equals("if")) {
handleIf(aNode.left, aNode.right);
}
if(aNode.data.equals("+")) {
return eval(aNode.left) + eval(aNode.right);
}
if(aNode.data.equals("/")) {
return eval(aNode.left) / eval(aNode.right);
}
if(aNode.data.equals("-")) {
return eval(aNode.left) - eval(aNode.right);
}
if(aNode.data.equals("*")) {
return eval(aNode.left) * eval(aNode.right);
}
return 0;
}
/**
* Handles the "while" case when evaluating the LRL code.
* @param left is the node to the left of "while" containing the conditional.
* @param right is the node to the right of "while" containing the code to
* execute while the condition is yet to terminate.
*/
private void handleWhile(TreeNode left, TreeNode right) {
if(left.data.equals("<")) {
while(eval(left.left) < eval(left.right)) {
eval(right);
}
}
if(left.data.equals(">")) {
while(eval(left.left) > eval(left.right)) {
eval(right);
}
}
if(left.data.equals("==")) {
while(eval(left.left) == eval(left.right)) {
eval(right);
}
}
}
/**
* Handles the "if" case when evaluating the LRL code.
* @param left is the node to the left of the "if" statement containing
* the conditions.
* @param right is the node to the right of the "if" statement containing
* the code to evaluate if the conditions are met.
*/
private void handleIf(TreeNode left, TreeNode right) {
if(left.data.equals("<")) {
if(eval(left.left) < eval(left.right)) {
eval(right);
} else {
return;
}
}
if(left.data.equals(">")) {
if(eval(left.left) > eval(left.right)) {
eval(right);
} else {
return;
}
}
if(left.data.equals("==")) {
if(eval(left.left) == eval(left.right)) {
eval(right);
} else {
return;
}
}
}
/**
* Format the LRL tree into Java formatted code.
* @return A representation of the LRL Tree in Java formatted code.
*/
public String toJavaString() {
int currIndent = 0;
// Setup preliminary output
StringBuilder javaString = new StringBuilder(500);
javaString.append("public class JavaCode {\n");
javaString.append(indentString(++currIndent)).
append("public static void main(String[] args) {\n");
currIndent++;
for (String key : env.keySet()) {
javaString.append(indentString(currIndent)).append("int ").append(key).append(";\n");
}
// Recurse Tree
javaString.append(toJavaString(currIndent, root));
// close out blocks
javaString.append((javaString.charAt(javaString.length() - 1) == '}') ? "\n" : ";\n");
while (currIndent > 0) {
javaString.append(indentString(--currIndent)).append("}\n");
}
return javaString.toString();
}
/** Given indent multiplier return a String with the correct number of space chars. */
private String indentString(final int currIndent) {
String retVal = "";
for (int i = 0; i < currIndent; i++) {
retVal += INDENT;
}
return retVal;
}
/** The recursive portion of formatting the LRL tree into Java formatted code. */
private String toJavaString(int currIndent, TreeNode aNode) {
// String retVal = "";
StringBuilder retVal = new StringBuilder(400);
if(aNode == null) {
return null;
} else if (aNode.data.equals("block")) {
retVal.append(toJavaString(currIndent, aNode.left)).
append((retVal.charAt(retVal.length() - 1) == '}') ? "\n" : ";\n").
append(toJavaString(currIndent, aNode.right));
} else if(aNode.data.equals("print")) {
retVal.append(indentString(currIndent)).append("System.out.println(").
append(toJavaString(0, aNode.left)).append(")");
} else if (aNode.data.equals("while") || aNode.data.equals("if")) {
retVal.append(indentString(currIndent)).append(aNode.data).
append("(").append(toJavaString(0, aNode.left)).append(") {\n");
currIndent++;
retVal.append(toJavaString(currIndent, aNode.right)).
append((retVal.charAt(retVal.length() - 1) == '}') ? "\n" : ";\n").
append(indentString(--currIndent) + "}");
// operator (excluding Block While or If which have already been checked for above)
} else if(isOperator(aNode.data)) {
retVal.append(indentString(currIndent)).append((currIndent == 0) ? "(" : "").
append(toJavaString(0, aNode.left) + " ").append(aNode.data).append(" ").
append(toJavaString(0, aNode.right)).append((currIndent == 0) ? ")" : "");
// var or int: return the data
} else {
retVal.append(aNode.data);
}
return retVal.toString();
}
}