-
Notifications
You must be signed in to change notification settings - Fork 5
/
json_to_D3_tree.js
156 lines (133 loc) · 3.99 KB
/
json_to_D3_tree.js
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
// takes in a JSON object and reformats object to comply with D3 tree format
var json =
{
"define": [
"define",
"area",
[
"lambda",
[
"r"
],
[
"*",
3.141592653,
[
"*",
"r",
"r"
]
]
]
]
};
var createSchemeD3TreeFormat = function(json){
var tree = [];
// to start off, need to access the only key in the dictionary json
for (var key in json){
json_list = json[key];
tree.push(createNode(json_list));
}
// return final tree
return tree;
};
var createNode = function(args){
if (typeof(args) != typeof([])){
var terminal_node = {
"name": args
};
return terminal_node;
}
else {
var parent = args[0];
var new_node = {
"name": parent,
"children": []
};
var children = args.slice(1);
var i = 0; // iterator
if (parent=="lambda"){
var lambda_args = args[1]; // arguments list
lambda_args.unshift("(args)"); // insert "(args)" node for tree rendering
} // I'm revisiting this 2 weeks later...what is happening. Where does lambda_args get passed...
for (i in children){
child_node = createNode(children[i]);
new_node["children"].push(child_node);
}
// uncomment this to see all the individual objects in tree
// console.log(new_node);
return new_node;
}
};
var createJSD3TreeFormat = function(json){
var tree = [];
var i = 0; // iterator
// initial elements will always be either statements or functions
s_json = JSON.stringify(json, null, '\t');
// console.log('json passed for parsing is ' + s_json);
for (i=0; i<json.length; i++){
var block = json[i]; // block to parse
// console.log('block is now ' + JSON.stringify(block, null, '\t'));
if (json[i][0] == "stmt"){
// console.log('editing block start point');
block = json[i][1];
if (block[0] == "exp"){
// console.log('editing block start point b/c it is exp');
block = block[1];
}
}
console.log('passing block into tree node ' + JSON.stringify(block, null, '\t'));
tree.push(createJSNode(block));
}
// return final tree
return tree;
};
var createJSNode = function(args){
console.log('\n\n\n');
console.log("ARGS IS " + JSON.stringify(args, null, '\t'));
console.log(args.length);
var parent = args[1];
var childArray = args.slice(2);
if (args[0] == "function" || args[0] == "compound statement" || args[0] == "exp" || args[0] == "if-then-else" || args[0] == "if-then" || args[0] == "assign" || args[0] == "return"){
parent = args[0];
if (parent == "compound statement"){
// compound statement is followed by a list of lists
childArray = args[1];
}
if (parent == "exp"){
childArray = [args[1]];
}
if (parent == "if-then" || parent == "if-then-else"){
childArray = args.slice(1);
}
if (parent == "assign" || parent == "return"){
childArray = args.slice(1);
}
if (parent == "function"){
childArray = args.slice(1);
}
}
s_parent = JSON.stringify(parent, null, '\t');
console.log('parent is ', s_parent);
if (parent === undefined){
parent = args;
}
if (args[0] == "call"){
childArray = childArray[0];
}
console.log("MAKING NODE FOR PARENT " + parent);
var new_node = {
"name": parent,
"children": []
};
console.log('child array is', JSON.stringify(childArray, null, '\t'));
var i = 0; // iterator
for (i=0; i<childArray.length; i++){
console.log("creating child node for " + childArray[i]);
child_node = createJSNode(childArray[i]);
new_node["children"].push(child_node);
}
// uncomment this to see all the individual objects in tree
// console.log(new_node);
return new_node;
};