forked from MathieuNls/clever-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAstResult.java
More file actions
68 lines (55 loc) · 2.46 KB
/
Copy pathAstResult.java
File metadata and controls
68 lines (55 loc) · 2.46 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
import java.util.List;
import java.util.ArrayList;
import org.json.*;
public class AstResult {
List<VariableDescription> variablesDeclarations;
public AstResult() {
this.variablesDeclarations = new ArrayList<VariableDescription>();
}
public String toString() {
StringBuffer buffer = new StringBuffer();
for (VariableDescription vd : variablesDeclarations) {
buffer.append(vd);
}
return buffer.toString();
}
public void add(String typeName, String varName) {
this.variablesDeclarations.add(new VariableDescription(typeName, varName));
}
private class VariableDescription {
String typeName;
String varName;
public VariableDescription(String typeName, String varName) {
this.typeName = typeName;
this.varName = varName;
}
public String toString() {
return String.format("{%s}{%s}\n", this.typeName, this.varName);
}
}
public void propagate(JSONObject jo) {
try {
JSONArray children = jo.getJSONArray("Children"); // get the children of the current node in the JSONObject
if (jo.get("Type").equals("VariableDeclaration")) { // check if the current node is a "VariableDeclaration"
JSONObject first_child = children.getJSONObject(0).getJSONArray("Children").getJSONObject(0);
String typeName = first_child.getString("ValueText"); // first child of a variable declaration will give the type
JSONObject second_child = children.getJSONObject(1).getJSONArray("Children").getJSONObject(0);
String varName = second_child.getString("ValueText"); // second child of a variable declaration will give the variable name
this.add(typeName, varName); // add the variable to the AstResult
for (int i = 2; children != null && i < children.length(); i++) { // goes through the rest of the children of the current node
this.propagate(children.getJSONObject(i));
}
}
else {
for (int i = 0; children != null && i < children.length(); i++) { // goes through all the children of the current node
this.propagate(children.getJSONObject(i));
}
}
}
catch(JSONException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
return;
}
}