-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.java
289 lines (255 loc) · 10 KB
/
Node.java
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
import java.util.LinkedList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
public class Node {
public String data;
public type nodeType;
public LinkedList<Node> childNodes;
public Node(String newData, type newNodeType) {
this.data = newData;
this.nodeType = newNodeType;
childNodes = new LinkedList<Node>();
}
// i know, i know, this is not a visitor pattern
// but it's close enough and it'll have to do.
public Object accept(TreeVisitor visitor, LinkedList<Variable> vars, PrintWriter out, String[] lines) {
// MAIN NODE
if (this.nodeType.equals(type.MainNode)) {
return this.childNodes.getFirst().accept(visitor, vars, out, lines);
} else
//////////////////////////////////////////////
// SEQUENCE NODE
if (this.nodeType.equals(type.SequenceNode)) {
this.childNodes.getFirst().accept(visitor, vars, out, lines);
this.childNodes.getLast().accept(visitor, vars, out, lines);
} else
/////////////////////////////////////////////
// BRACKET NODE
if (this.nodeType.equals(type.BracketNode)) {
return this.childNodes.getFirst().accept(visitor, vars, out, lines);
} else
/////////////////////////////////////////////
// INT NODE
if (this.nodeType.equals(type.IntNode)) {
return Integer.valueOf(data);
} else
/////////////////////////////////////////////
// BOOL NODE
if (this.nodeType.equals(type.BoolNode)) {
return Boolean.valueOf(data);
} else
/////////////////////////////////////////////
// VAR NODE
if (this.nodeType.equals(type.VarNode)) {
for (Variable var : vars) {
if (var.name.equals(this.data)) {
return var;
}
}
out.println("UnassignedVar " + findVarLine(lines, this.data));
out.close();
System.exit(0);
} else
/////////////////////////////////////////////
// ASSIGNMENT NODE
if (this.nodeType.equals(type.AssignmentNode)) {
Variable assignedToVar =
(Variable) this.childNodes.getFirst().accept(visitor, vars, out, lines);
if (this.childNodes.getLast().accept(visitor, vars, out, lines) instanceof Variable) {
assignedToVar.value =
((Variable) this.childNodes.getLast().accept(visitor, vars, out, lines)).value;
} else {
assignedToVar.value =
this.childNodes.getLast().accept(visitor, vars, out, lines);
}
} else
/////////////////////////////////////////////
// PLUS NODE
if (this.nodeType.equals(type.PlusNode)) {
Integer firstValue;
Integer secondValue;
if (this.childNodes.getFirst().nodeType.equals(type.VarNode)) {
firstValue = (Integer)
((Variable) this.childNodes.getFirst().accept(visitor, vars, out, lines)).value;
} else {
firstValue = (Integer) this.childNodes.getFirst().accept(visitor, vars, out, lines);
}
if (this.childNodes.getLast().nodeType.equals(type.VarNode)) {
secondValue = (Integer)
((Variable) this.childNodes.getLast().accept(visitor, vars, out, lines)).value;
} else {
secondValue = (Integer) this.childNodes.getLast().accept(visitor, vars, out, lines);
}
int line = -1;
if (firstValue == null) {
line = findNullLine(vars, lines);
}
if (secondValue == null) {
line = findNullLine(vars, lines);
}
if (line != -1) {
out.println("UnassignedVar " + line);
out.close();
System.exit(0);
}
return firstValue + secondValue;
} else
/////////////////////////////////////////////
// DIV NODE
if (this.nodeType.equals(type.DivNode)) {
Integer firstValue;
Integer secondValue;
if (this.childNodes.getFirst().nodeType.equals(type.VarNode)) {
firstValue = (Integer)
((Variable) this.childNodes.getFirst().accept(visitor, vars, out, lines)).value;
} else {
firstValue = (Integer) this.childNodes.getFirst().accept(visitor, vars, out, lines);
}
if (this.childNodes.getLast().nodeType.equals(type.VarNode)) {
secondValue = (Integer)
((Variable) this.childNodes.getLast().accept(visitor, vars, out, lines)).value;
} else {
secondValue = (Integer) this.childNodes.getLast().accept(visitor, vars, out, lines);
}
int line = -1;
if (firstValue == null) {
line = findNullLine(vars, lines);
}
if (secondValue == null) {
line = findNullLine(vars, lines);
}
if (line != -1) {
out.println("UnassignedVar " + line);
out.close();
System.exit(0);
}
if (secondValue != 0) {
return firstValue / secondValue;
} else {
out.println("DivideByZero " + getLineOfDiv(vars, lines));
out.close();
System.exit(0);
}
} else
/////////////////////////////////////////////
// BLOCK NODE
if (this.nodeType.equals(type.BlockNode)) {
for (Node child : this.childNodes) {
child.accept(visitor, vars, out, lines);
}
} else
/////////////////////////////////////////////
// NOT NODE
if (this.nodeType.equals(type.NotNode)) {
return ! ((Boolean) this.childNodes.getFirst().accept(visitor, vars, out, lines));
} else
/////////////////////////////////////////////
// AND NODE
if (this.nodeType.equals(type.AndNode)) {
return ((Boolean) this.childNodes.getFirst().accept(visitor, vars, out, lines)) &&
((Boolean) this.childNodes.getLast().accept(visitor, vars, out, lines));
}
/////////////////////////////////////////////
// GREATER NODE
if (this.nodeType.equals(type.GreaterNode)) {
Integer firstValue;
Integer secondValue;
if (this.childNodes.getFirst().nodeType.equals(type.VarNode)) {
firstValue = (Integer)
((Variable) this.childNodes.getFirst().accept(visitor, vars, out, lines)).value;
} else {
firstValue = (Integer) this.childNodes.getFirst().accept(visitor, vars, out, lines);
}
if (this.childNodes.getLast().nodeType.equals(type.VarNode)) {
secondValue = (Integer)
((Variable) this.childNodes.getLast().accept(visitor, vars, out, lines)).value;
} else {
secondValue = (Integer) this.childNodes.getLast().accept(visitor, vars, out, lines);
}
return firstValue > secondValue;
} else
/////////////////////////////////////////////
// IF NODE
if (this.nodeType.equals(type.IfNode)) {
if ((Boolean) this.childNodes.get(0).accept(visitor, vars, out, lines)) {
this.childNodes.get(1).accept(visitor, vars, out, lines);
} else {
if (this.childNodes.size() > 2) {
this.childNodes.get(2).accept(visitor, vars, out, lines);
}
}
} else
/////////////////////////////////////////////
// WHILE NODE
if (this.nodeType.equals(type.WhileNode)) {
while ((Boolean) this.childNodes.get(0).accept(visitor, vars, out, lines)) {
this.childNodes.get(1).accept(visitor, vars, out, lines);
}
}
/////////////////////////////////////////////
// generic case
return new Object();
}
private int findNullLine(LinkedList<Variable> vars, String[] lines) {
Variable nullVar = new Variable(Variable.varType.boolvar, "dummy");
for (Variable var : vars) {
if (var.value == null) {
nullVar = var;
break;
}
}
for (int i = 1; i <= lines.length; i++) {
if (lines[i].contains(nullVar.name)) {
return i + 1;
}
}
return -1;
}
private int findVarLine(String[] lines, String data) {
int cnt = 1;
for (String line : lines) {
if (line.contains(data)) {
return cnt;
}
cnt++;
}
return -1;
}
private int getLineOfDiv(LinkedList<Variable> vars, String[] lines) {
int cnt = 1;
String var = "0";
for (Variable variable : vars) {
if (variable.value instanceof Integer && ((Integer) variable.value).equals(0)) {
var = variable.name;
}
}
for (String line : lines) {
if (line.contains("/") && line.contains(var)
&& line.lastIndexOf(var) > line.lastIndexOf("/")) {
return cnt;
}
cnt++;
}
return -1;
}
public enum type {
MainNode, // done
IntNode, // done
BoolNode, // done
VarNode, // done
PlusNode, // done
DivNode, // done
BracketNode, // done
AndNode, // done
GreaterNode, // done
NotNode, // done
AssignmentNode, // done
BlockNode, // done
IfNode, // done
WhileNode, // done
SequenceNode // done, ignore these comments, used them to keep track of my progress
}
}