-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLToSyntaxTree.java
337 lines (332 loc) · 23.4 KB
/
XMLToSyntaxTree.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
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
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
public class XMLToSyntaxTree {
private Document loadXMLFromString(String xml) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(xml));
return builder.parse(inputSource);
}
public ProgramBase xmlToProgram(String xml) throws Exception {
Node node = loadXMLFromString(xml).getFirstChild();
if (!node.getNodeName().equals("program") && !node.getNodeName().equals("pr")) return new SyntaxTree.Programs();
node = node.getFirstChild();
return xmlToProgram(node);
}
public ProgramBase xmlToProgram(Node node) {
ArrayList<ProgramBase> programs = new ArrayList<>();
while (node != null) {
if (node.getNodeName().equals("print") || node.getNodeName().equals("p")) {
ValueBase separator = null;
ArrayList<ValueBase> values = new ArrayList<>();
Node node1;
if (node.getNodeName().equals("p")) node1 = node.getChildNodes().item(0);
else node1 = node.getChildNodes().item(1);
while (node1 != null) {
if (node1.getNodeName().equals("separator"))
separator = getValueFromNode(node1.getChildNodes().item(1));
if (node1.getNodeName().equals("data"))
values.add(getValueFromNode(node1.getChildNodes().item(1)));
if (node1.getNodeName().equals("s"))
separator = getValueFromNode(node1.getChildNodes().item(0));
if (node1.getNodeName().equals("d"))
values.add(getValueFromNode(node1.getChildNodes().item(0)));
node1 = node1.getNextSibling();
}
ValueBase[] valuesArray = new ValueBase[values.size()];
for (int i = 0; i < values.size(); i++) {
valuesArray[i] = values.get(i);
}
programs.add(new SyntaxTree.Print(valuesArray).setSeparator(separator));
} else if (node.getNodeName().equals("exit") || node.getNodeName().equals("e")) {
Node node1;
if (node.getNodeName().equals("p")) node1 = node.getChildNodes().item(0);
else node1 = node.getChildNodes().item(1);
programs.add(new SyntaxTree.Exit(getValueFromNode(node1)));
} else if (node.getNodeName().equals("if") || node.getNodeName().equals("i")) {
ValueBase condition;
ProgramBase program;
ProgramBase elseProgram = null;
if (node.getNodeName().equals("if")) {
condition = getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1));
program = xmlToProgram(node.getChildNodes().item(3).getChildNodes().item(1));
try {
elseProgram = xmlToProgram(node.getChildNodes().item(5).getChildNodes().item(1));
} catch (Exception ignore) {}
} else {
condition = getValueFromNode(node.getFirstChild().getFirstChild());
program = xmlToProgram(node.getChildNodes().item(1).getFirstChild());
try {
elseProgram = xmlToProgram(node.getChildNodes().item(2).getFirstChild());
} catch (Exception ignore) {}
}
SyntaxTree.If _if = new SyntaxTree.If(condition, program);
if (elseProgram != null) _if.addElse(elseProgram);
programs.add(_if);
} else if (node.getNodeName().equals("while") || node.getNodeName().equals("w")) {
ValueBase condition;
ProgramBase program;
if (node.getNodeName().equals("while")) {
condition = getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1));
program = xmlToProgram(node.getChildNodes().item(3).getChildNodes().item(1));
} else {
condition = getValueFromNode(node.getFirstChild().getFirstChild());
program = xmlToProgram(node.getChildNodes().item(1).getFirstChild());
}
programs.add(new SyntaxTree.While(condition, program));
} else if (node.getNodeName().equals("break") || node.getNodeName().equals("br")) {
programs.add(new SyntaxTree.Break());
} else if (node.getNodeName().equals("function") || node.getNodeName().equals("fun")) {
programs.add(new SyntaxTree.Function(node.getAttributes().getNamedItem(node.getNodeName().equals("fun")? "n":"name").getNodeValue().replace("#", "<"), xmlToProgram(node.getFirstChild()), node.getAttributes().getNamedItem(node.getNodeName().equals("fun")? "a":"args").getNodeValue().split(",")));
} else if (node.getNodeName().equals("set-variable") || node.getNodeName().equals("set")) {
ValueBase instance = null;
try {
if (node.getLastChild().getPreviousSibling().getNodeName().equals("instance") || node.getLastChild().getNodeName().equals("instance")) {
if (node.getNodeName().equals("set-variable"))
instance = getValueFromNode(node.getLastChild().getPreviousSibling().getChildNodes().item(1));
else
instance = getValueFromNode(node.getLastChild().getFirstChild());
}
} catch (Exception ignored) {}
programs.add(new SyntaxTree.SetVariable(node.getAttributes().getNamedItem(node.getNodeName().equals("set")? "n":"name")
.getNodeValue(), getValueFromNode(node.getNodeName().equals("set")? node.getFirstChild().getFirstChild():node.getChildNodes().item(1).getChildNodes().item(1))).fromInstance(instance).setAddInstanceName(node.getAttributes().getNamedItem(node.getNodeName().equals("set")? "ain":"addInstanceName").getNodeValue().equals("true"))
.setIsDeclaration(node.getAttributes().getNamedItem(node.getNodeName().equals("set")? "d":"declaration").getNodeValue().equals("true")));
} else if (node.getNodeName().equals("continue") || node.getNodeName().equals("con")) {
programs.add(new SyntaxTree.Continue());
} else if (node.getNodeName().equals("repeat")) {
programs.add(new SyntaxTree.Repeat(getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)), xmlToProgram(node.getChildNodes().item(1).getChildNodes().item(1))));
} else if (node.getNodeName().equals("r")) {
programs.add(new SyntaxTree.Repeat(getValueFromNode(node.getChildNodes().item(1).getFirstChild()), xmlToProgram(node.getFirstChild().getFirstChild())));
} else if (node.getNodeName().equals("class")) {
programs.add(new SyntaxTree.CreateClass(node.getAttributes().getNamedItem("name").getFirstChild().getNodeValue(), xmlToProgram(node.getChildNodes().item(1))));
} else if (node.getNodeName().equals("cl")) {
programs.add(new SyntaxTree.CreateClass(node.getAttributes().getNamedItem("n").getFirstChild().getNodeValue(), xmlToProgram(node.getFirstChild())));
} else if (node.getNodeName().equals("for") || node.getNodeName().equals("f")) {
ValueBase condition;
ProgramBase step, init, program;
if (node.getNodeName().equals("for")) {
init = xmlToProgram(node.getChildNodes().item(1).getChildNodes().item(1));
step = xmlToProgram(node.getChildNodes().item(3).getChildNodes().item(1));
program = xmlToProgram(node.getChildNodes().item(5).getChildNodes().item(1));
condition = getValueFromNode(node.getChildNodes().item(7).getChildNodes().item(1));
} else {
init = xmlToProgram(node.getFirstChild().getFirstChild());
step = xmlToProgram(node.getChildNodes().item(1).getFirstChild());
program = xmlToProgram(node.getChildNodes().item(2).getFirstChild());
condition = getValueFromNode(node.getChildNodes().item(3).getFirstChild());
}
programs.add(new SyntaxTree.For(condition, step, init, program));
} else if (node.getNodeName().equals("executeValue") || node.getNodeName().equals("ev")) {
ValueBase value = null;
Node node1 = node.getNodeName().equals("ev")? node.getChildNodes().item(0):node.getChildNodes().item(1);
if (node1.getNodeName().equals("value") || node1.getNodeName().equals("v")) {
value = node1.getNodeName().equals("v")? getValueFromNode(node1.getChildNodes().item(0)):getValueFromNode(node1.getChildNodes().item(1));
}
programs.add(new SyntaxTree.ExecuteValue(value));
}
node = node.getNextSibling();
}
ProgramBase[] programsArray = new ProgramBase[programs.size()];
for (int i = 0; i < programs.size(); i++) {
programsArray[i] = programs.get(i);
}
return new SyntaxTree.Programs(programsArray);
}
private ValueBase getValueFromNode(Node node) {
switch (node.getNodeName()) {
case "number":
case "n":
return new SyntaxTree.Number(new BigDecimal(node.getTextContent()));
case "bool":
case "b":
return new SyntaxTree.Boolean(node.getTextContent().toLowerCase().equals("true"));
case "null":
case "nl":
return new SyntaxTree.Null();
case "list":
case "l": {
ArrayList<ValueBase> res = new ArrayList<>();
Node node1 = node.getFirstChild();
while (node1 != null) {
if (node1.getNodeName().equals("data"))
res.add(getValueFromNode(node1.getChildNodes().item(1)));
if (node1.getNodeName().equals("d"))
res.add(getValueFromNode(node1.getChildNodes().item(0)));
node1 = node1.getNextSibling();
}
ValueBase[] valuesArray = new ValueBase[res.size()];
for (int i = 0; i < res.size(); i++) {
valuesArray[i] = res.get(i);
}
return new SyntaxTree.List(valuesArray);
}
case "text":
case "t":
return new SyntaxTree.Text(node.getAttributes().getNamedItem("data").getNodeValue());
case "printFunction":
case "pf":
return new SyntaxTree.PrintFunction((SyntaxTree.Print) ((SyntaxTree.Programs) xmlToProgram(node.getFirstChild())).getPrograms()[0]);
case "add":
return new SyntaxTree.Add(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "a":
return new SyntaxTree.Add(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "sub":
return new SyntaxTree.Sub(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "s":
return new SyntaxTree.Sub(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "mul":
return new SyntaxTree.Mul(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "m":
return new SyntaxTree.Mul(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "div":
return new SyntaxTree.Div(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "d":
return new SyntaxTree.Div(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "mod":
return new SyntaxTree.Mod(getValueFromNode(node.getFirstChild().getLastChild()), getValueFromNode(node.getLastChild().getChildNodes().item(1)));
case "m1":
return new SyntaxTree.Mod(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "pow":
return new SyntaxTree.Pow(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "p1":
return new SyntaxTree.Pow(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "equals":
return new SyntaxTree.Equals(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "eq":
return new SyntaxTree.Equals(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "strict-equals":
return new SyntaxTree.StrictEquals(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "seq":
return new SyntaxTree.StrictEquals(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "greater-than":
return new SyntaxTree.GreaterThan(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "gt":
return new SyntaxTree.GreaterThan(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "lesser-than":
return new SyntaxTree.LesserThan(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "lt":
return new SyntaxTree.LesserThan(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "lesser-than-or-equal":
return new SyntaxTree.LesserThanOrEqual(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "le":
return new SyntaxTree.LesserThanOrEqual(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "greater-than-or-equal":
return new SyntaxTree.GreaterThanOrEqual(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "ge":
return new SyntaxTree.GreaterThanOrEqual(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "and":
return new SyntaxTree.And(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "a1":
return new SyntaxTree.And(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "or":
return new SyntaxTree.Or(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "o":
return new SyntaxTree.Or(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "bitwise-and":
return new SyntaxTree.BitwiseAnd(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "ba":
return new SyntaxTree.BitwiseAnd(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "bitwise-or":
return new SyntaxTree.BitwiseOr(getValueFromNode(node.getChildNodes().item(1).getChildNodes().item(1)), getValueFromNode(node.getChildNodes().item(3).getChildNodes().item(1)));
case "bo":
return new SyntaxTree.BitwiseOr(getValueFromNode(node.getFirstChild().getFirstChild()), getValueFromNode(node.getLastChild().getFirstChild()));
case "bitwise-not":
return new SyntaxTree.BitwiseNot(getValueFromNode(node.getChildNodes().item(1)));
case "bn":
return new SyntaxTree.BitwiseNot(getValueFromNode(node.getFirstChild()));
case "not":
return new SyntaxTree.Not(getValueFromNode(node.getChildNodes().item(1)));
case "n1":
return new SyntaxTree.Not(getValueFromNode(node.getFirstChild()));
case "exitFunction":
case "ef":
return new SyntaxTree.ExitFunction((SyntaxTree.Exit) ((SyntaxTree.Programs) xmlToProgram(node.getFirstChild())).getPrograms()[0]);
case "variable": {
ValueBase instance = null;
try {
if (node.getLastChild().getPreviousSibling().getNodeName().equals("instance")) {
instance = getValueFromNode(node.getLastChild().getPreviousSibling().getChildNodes().item(1));
}
} catch (Exception ignored) {
}
return new SyntaxTree.Variable(node.getAttributes().getNamedItem("name").getNodeValue()).fromInstance(instance).setAddInstanceName(node.getAttributes().getNamedItem("addInstanceName").getNodeValue().equals("true"));
}
case "v": {
ValueBase instance = null;
try {
if (node.getLastChild().getNodeName().equals("instance")) {
instance = getValueFromNode(node.getLastChild().getFirstChild());
}
} catch (Exception ignored) {
}
return new SyntaxTree.Variable(node.getAttributes().getNamedItem("n").getNodeValue()).fromInstance(instance).setAddInstanceName(node.getAttributes().getNamedItem("ain").getNodeValue().equals("true"));
}
case "increase":
return new SyntaxTree.Increase((SyntaxTree.Variable) getValueFromNode(node.getChildNodes().item(1)), node.getAttributes().getNamedItem("is-postfix").getNodeValue().equals("true"));
case "in":
return new SyntaxTree.Increase((SyntaxTree.Variable) getValueFromNode(node.getFirstChild()), node.getAttributes().getNamedItem("p").getNodeValue().equals("true"));
case "decrease":
return new SyntaxTree.Decrease((SyntaxTree.Variable) getValueFromNode(node.getChildNodes().item(1)), node.getAttributes().getNamedItem("is-postfix").getNodeValue().equals("true"));
case "de":
return new SyntaxTree.Decrease((SyntaxTree.Variable) getValueFromNode(node.getFirstChild()), node.getAttributes().getNamedItem("p").getNodeValue().equals("true"));
case "ci": {
ArrayList<ValueBase> values = new ArrayList<>();
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
values.add(getValueFromNode(node.getChildNodes().item(i).getFirstChild()));
}
ValueBase[] valuesArray = new ValueBase[values.size()];
valuesArray = values.toArray(valuesArray);
return new SyntaxTree.CreateInstance(node.getAttributes().getNamedItem("n").getNodeValue(), valuesArray);
}
case "createInstance": {
ArrayList<ValueBase> values = new ArrayList<>();
for (int i = 1; i < node.getChildNodes().getLength(); i += 2) {
values.add(getValueFromNode(node.getChildNodes().item(i).getChildNodes().item(1)));
}
ValueBase[] valuesArray = new ValueBase[values.size()];
valuesArray = values.toArray(valuesArray);
return new SyntaxTree.CreateInstance(node.getAttributes().getNamedItem("name").getNodeValue(), valuesArray);
}
case "call-function": {
ArrayList<ValueBase> values = new ArrayList<>();
for (int i = 1; i < node.getChildNodes().getLength(); i += 2) {
if (node.getChildNodes().item(i).getNodeName().startsWith("arg"))
values.add(getValueFromNode(node.getChildNodes().item(i).getChildNodes().item(1)));
}
ValueBase[] valuesArray = new ValueBase[values.size()];
valuesArray = values.toArray(valuesArray);
ValueBase instance = null;
try {
if (node.getLastChild().getPreviousSibling().getNodeName().equals("instance")) {
instance = getValueFromNode(node.getLastChild().getPreviousSibling().getChildNodes().item(1));
}
} catch (Exception ignored) {}
return new SyntaxTree.CallFunction(node.getAttributes().getNamedItem("name").getNodeValue(), valuesArray).setAddInstanceName(node.getAttributes().getNamedItem("addInstanceName").getNodeValue().equals("true")).fromInstance(instance);
}
case "c": {
ArrayList<ValueBase> values = new ArrayList<>();
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
if (node.getChildNodes().item(i).getNodeName().startsWith("arg"))
values.add(getValueFromNode(node.getChildNodes().item(i).getFirstChild()));
}
ValueBase instance = null;
try {
if (node.getLastChild().getNodeName().equals("instance")) {
instance = getValueFromNode(node.getLastChild().getFirstChild());
}
} catch (Exception ignored) {}
ValueBase[] valuesArray = new ValueBase[values.size()];
valuesArray = values.toArray(valuesArray);
return new SyntaxTree.CallFunction(node.getAttributes().getNamedItem("n").getNodeValue(), valuesArray).setAddInstanceName(node.getAttributes().getNamedItem("ain").getNodeValue().equals("true")).fromInstance(instance);
}
}
return new SyntaxTree.Null();
}
}