-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogic.js
340 lines (305 loc) · 10.2 KB
/
logic.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
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
338
339
340
var ProgressStage = {
OPENBRACKET: 1, //(
CONSTRUCTOR: 2, //MAKE
IDENTIFIER: 3, //NODE
ARGUMENTS: 4, //VAL KEY LEFT RIGHT
CLOSEBRACKET: 5 //)
}
function process(block, identity){
var thisProcessStage = ProgressStage.OPENBRACKET;
var constructor = "";
var identifier = "";
var arguments = [];
var currentArgument = 0;
var numOpenBrackets = 0;
var hadSpace = false;
var path = []; path.push(-1);
var layer = 0;
var alreadyClosed = true; //brackets for (list and (make-node form an even and odd parity; thus we need a boolean to toggle parity values
var addedLayer = false;
for (var i = 0, s = block.length; i < s; i++){
var c = block[i];
switch(thisProcessStage){
case ProgressStage.OPENBRACKET:
if (c == " "){
continue;
} else if (c != "("){
throw "Error: Bad Syntax"; //if it's not an open bracket
} else {
numOpenBrackets++;
thisProcessStage = ProgressStage.CONSTRUCTOR;
}
break;
case ProgressStage.CONSTRUCTOR:
if (c == " " || c == "\n"){
continue;
} else if (constructor == "list" || constructor == "'("){
alreadyClosed = false;
constructor = "";
numOpenBrackets++;
path.push(0);
addedLayer = true;
layer++;
} else if (constructor == "make"){
alreadyClosed = true;
if (addedLayer == false){
path[layer]=path[layer]+1;
}
addedLayer = false;
thisProcessStage = ProgressStage.IDENTIFIER;
} else if (constructor.length > 8){ //both "make" and "list" have a length less than 8 - breaks the loop once syntax error is found
throw "Error: not a constructor";
} else {
constructor += c;
}
break;
case ProgressStage.IDENTIFIER:
if (c == " "){
if (identifier.indexOf(identity) != -1){
thisProcessStage = ProgressStage.ARGUMENTS;
} else {
throw "Error: Identifier undefined";
}
} else if (c == ")"){
throw "Error: Syntax / No Arguments Found";
} else {
identifier += c;
}
break;
case ProgressStage.ARGUMENTS:
var countList = (block.match(/list/g) || []).length;
//check if list is replaced by " ' " symbol => if countList = 0, then it has been replaced by " ' ".
if (arguments.length == currentArgument){
arguments[currentArgument]="";
}
if (c == " " || c == "\n"){
hadSpace = true;
} else if (c == ")"){
numOpenBrackets--;
if (numOpenBrackets == 0){
arguments[currentArgument] +="|"+path;
break;
} else{
alreadyClosed = false;
arguments[currentArgument] += "|" + path;
thisProcessStage = ProgressStage.CLOSEBRACKET;
}
} else if (c == "("){
numOpenBrackets++;
identifier = "";
constructor = "";
arguments[currentArgument] +="|"+path;
currentArgument++;
thisProcessStage = ProgressStage.CONSTRUCTOR;
} else if (c == "'" && countList == 0){
numOpenBrackets++;
identifier = "";
constructor = "'";
arguments[currentArgument] +="|"+path;
currentArgument++;
thisProcessStage = ProgressStage.CONSTRUCTOR;
} else{
if (hadSpace == true && arguments[currentArgument].length!=0){
arguments[currentArgument] += ","+c;
hadSpace = false;
} else {
arguments[currentArgument] += c;
}
}
break;
case ProgressStage.CLOSEBRACKET:
if (c == ")" && alreadyClosed ==false){
arguments[currentArgument] += "|" + path;
layer--;
path.pop();
alreadyClosed = true;
} else if (c == " "){
continue;
} else if (c == "("){
constructor = "";
identifier = "";
currentArgument++;
thisProcessStage = ProgressStage.CONSTRUCTOR;
} else if (c==")") {
alreadyClosed = false;
}
break;
}
}
console.log(arguments);
//removes the first "comma" left over from previous parsing
for (var i = 0; i < arguments.length; i++){
var arg = arguments[i];
var new_argument = "";
var shouldDelete = true;
for (var j = 0; j < arg.length; j++){
if (arg.charAt(j) == ',' && shouldDelete == true){
continue;
}else{
new_argument += arg.charAt(j);
if (isAlphaNumeric(arg.charAt(j))){
shouldDelete = false;
}
}
}
arguments[i] = new_argument;
}
console.log(arguments);
processArguments(arguments);
}
function processArguments(arguments){
var info = [];
for (var i = 0; i < arguments.length; i++){
var arg = arguments[i];
var fieldIndex = arg.indexOf("|");
var fields = arg.substr(0,fieldIndex);
var path = arg.substring(fieldIndex+1);
if (path.indexOf("|") == -1){
path = arg.substring(fieldIndex+1);
}else{
path = path.substring(0, path.indexOf("|"));
}
info[i] = new Array (fields, path);
}
console.log(info);
getSibling (info);
}
function getSibling(info){
for (var j = 0; j < info.length; j++){
var count = 0;
var route = info[j][1];
for (var k = 0; k < info.length; k++){
if (info[k][1].length == route.length &&
info[k][1].charAt(info[k][1].length-3) ==
route.charAt(route.length-3)) {
count++;
}
}
info[j] = new Array (info[j][0], info[j][1], count, window.innerWidth, window.innerWidth, window.innerHeight);
}
info[0][4] /= 2;
info[0][5] = 30;
getXYCoordinates(info);
}
// Right now, info = Array(data, path, num-of-siblings, width, x-coordinate, y-coordinate); Note that it is also stored in order of descend.
function getXYCoordinates(info){
var height = 0;
for (var n = 1; n < info.length; n++){
height = Math.max(height, info[n][1].length);
}
height = (height+1)/2; //returns height of tree
var layerHeight = window.innerHeight/height;
var defaultHeight = 30;
for (var i = 1; i < info.length; i++){
if (info[i][1].length == info[i-1][1].length+2){
//meaning this node is the previous nodes' child
var siblings = info[i][2];
var index = parseInt(info[i][1].charAt(info[i][1].length-1));
var layer = (info[i][1].length + 1)/2 - 1;
var width = info[i-1][3];
var x_coordinate = info[i-1][4];
var occupy_width = width/siblings;
var new_x = x_coordinate + (index - siblings/2 + 0.5)*occupy_width;
var y_coordinate = layer * layerHeight + defaultHeight;
info[i][3] = occupy_width;
info[i][4] = new_x;
info[i][5] = y_coordinate;
}else if (info[i][1].length == info[i-1][1].length){ //meaning this node is previous nodes' sibling node
var index = parseInt(info[i][1].charAt(info[i][1].length-1));
var layer = (info[i][1].length + 1)/2 - 1;
var width = info[i-1][3];
var x_coordinate = info[i-1][4];
var new_x = x_coordinate+width;
var y_coordinate = layer * layerHeight + defaultHeight;
info[i][3] = width;
info[i][4] = new_x;
info[i][5] = y_coordinate;
}else{
//meaning this layer is done; need to find the nodes' sibling
for (var j = 1; j < info.length; j++){
if (info[j][1].length == info[i][1].length &&
info[j][1].charAt(info[j][1].length-3) ==
info[i][1].charAt(info[i][1].length-3)){
var index = parseInt(info[i][1].charAt(info[i][1].length-1));
var layer = (info[i][1].length + 1)/2 - 1;
var width = info[j][3];
var x_coordinate = info[j][4];
console.log(info[j]);
var new_x = x_coordinate+width*index;
var y_coordinate = layer * layerHeight + defaultHeight;
info[i][3] = width;
info[i][4] = new_x;
info[i][5] = y_coordinate;
break;
}
}
}
}
console.log(info);
drawTree(info);
}
//lstnodes passed in is an ordered pair of destination x and y values
function drawTree(info){
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
for (var i = 1; i < info.length; i++){
if (info[i][1].length == info[i-1][1].length+2){
//the child of previous node
ctx.beginPath();
ctx.moveTo(info[i-1][4], info[i-1][5]);
ctx.lineTo(info[i][4], info[i][5]);
ctx.stroke();
}else {
//finds the parent of this node (shud be else where)
for (var j = 0; j < info.length; j++){
if (info[i][1].length == info[j][1].length+2 && info[i][1].substring(0,info[j][1].length) === info[j][1]){
//parent is found
ctx.beginPath();
ctx.moveTo(info[j][4], info[j][5]);
ctx.lineTo(info[i][4], info[i][5]);
ctx.stroke();
break;
}
}
}
}
for (var i = 1; i < info.length; i++){
ctx.beginPath();
ctx.arc(info[i][4], info[i][5], 20, 0, 2*Math.PI);
ctx.fillStyle = "white";
ctx.fill();
ctx.stroke();
}
for (var i = 0; i < info.length; i++){
var index = info[i][0].indexOf("empty");
if (index == -1){
continue;
}else {
info[i][0] = info[i][0].substring(0, index-1);
}
}
for (var i = 1; i < info.length; i++){
ctx.font = "18px Times New Roman";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx. fillText(info[i][0], info[i][4], info[i][5]);
}
ctx.beginPath();
ctx.arc(info[0][4], info[0][5], 20, 0, 2*Math.PI);
ctx.fillStyle = "white";
ctx.fill();
ctx.stroke();
ctx.font = "18px Times New Roman";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx. fillText(info[0][0], info[0][4], info[0][5]);
}
//helper function
function isAlphaNumeric (letter){
var a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
if (a.indexOf(letter) == -1){
return false;
}else{
return true;
}
}