forked from leesue630/tree-to-sml-converter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
to-sml-script.js
443 lines (398 loc) · 11.7 KB
/
to-sml-script.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
var depth;
var treeArray;
var table;
var activeTab;
function setDepthErrorText(str) {
document.getElementById("depth_error_text").innerHTML = str;
}
function setNegativeWarningText(str) {
document.getElementById("negative_warning_text").innerHTML = str;
}
function setTableWarningText(str) {
document.getElementById("table_warning_text").innerHTML = str;
}
function resetWarningText() {
setTableWarningText("");
setNegativeWarningText("");
setDepthErrorText("");
}
function setSMLOutputText(str) {
document.getElementById("sml_output_text").innerHTML = str;
}
function resetDepth() {
document.getElementById("depth_form").value = "";
document.getElementById("generate_sml_text_btn").disabled = true;
}
/**
* Checks for non-negative integer depth < 6.
* Sets appropriate error text if necessary.
* @param depth int that specifies depth
*/
function validateDepth(d) {
if (d == "" || isNaN(d)) {
setDepthErrorText("Depth must be a non-negative integer.");
return false;
}
if (d < 0) {
setDepthErrorText("Depth must be a non-negative integer.");
return false;
}
if (d > 6) {
setDepthErrorText("Depth <=6 pls :(");
return false;
}
return true;
}
/**
* Called when "Enter depth" is clicked.
* Sets global depth var.
* Validates depth.
* If valid, clears table, error text, and generates input table
*/
function depthEntered() {
// sets global depth variable
depth = document.getElementById("depth_form").value;
if (validateDepth(depth)) {
// clear table
clearTable();
// clear error text
resetWarningText();
// generate input table
generateInputTable();
// enable generate sml text btn
document.getElementById("generate_sml_text_btn").disabled = false;
}
}
/**
* Generate the input table for a tree or shrub datatype given the depth.
*/
function generateInputTable() {
if (depth == 0) {
let row = table.insertRow();
let cell = row.insertCell();
cell.innerHTML = "Zero depth";
} else {
for (var i = 0; i < depth; i++) {
let row = table.insertRow();
for (var j = 0; j < Math.pow(2, i); j++) {
var cell = row.insertCell();
cell.colSpan = Math.pow(2, depth - 1 - i);
cell.align = "center";
switch (activeTab) {
case "lazytree":
case "tree":
cell.appendChild(newTreeCell(i, j));
break;
case "shrub":
cell.appendChild(newShrubCell(i, j));
if (i < depth - 1) {
document.getElementById(
"branch_btn" + i + j
).onchange = createUncolorInputBorderFunction(i, j);
document.getElementById(
"branch_btn" + i + j
).onclick = createLeafEnableFunction(i, j, false);
document.getElementById(
"leaf_btn" + i + j
).onchange = createUncolorInputBorderFunction(i, j);
document.getElementById(
"leaf_btn" + i + j
).onclick = createLeafEnableFunction(i, j, true);
}
break;
}
}
}
}
}
/**
* Clears the table of all its children.
*/
function clearTable() {
while (table.firstChild) {
table.removeChild(table.firstChild);
}
}
/**
* Set the global table var.
* Clears the table.
* Creates a dummy "Node table will appear here" cell.
*/
function resetTable() {
switch (activeTab) {
case "sml":
table = document.getElementById("output_table");
break;
default:
table = document.getElementById("input_table");
}
// clear table
clearTable();
// insert initial dummy cell
let row = table.insertRow();
let cell = row.insertCell();
var text = document.createTextNode("Node table will appear here");
cell.appendChild(text);
}
function isConvertingToSML() {
return activeTab.localeCompare("sml") != 0;
}
/**
* Reset the table.
* Reset the error text.
* Reset the SML output text.
*/
function resetPage() {
resetTable();
if (isConvertingToSML()) {
resetWarningText();
setSMLOutputText("~SML Text will appear here~");
}
}
function colorCell(cell, color) {
cell.classList.remove("base-cell");
cell.classList.remove("valid-cell");
switch (color) {
case "base":
cell.classList.add("base-cell");
break;
case "valid":
cell.classList.add("valid-cell");
break;
}
}
function getCell(i, j) {
return document.getElementById("cell" + i + j);
}
function createUncolorInputBorderFunction(i, j) {
return function () {
uncolorInputBorder(i, j);
};
}
function uncolorInputBorder(i, j) {
colorCell(getCell(i, j), "base");
}
function getLeaf(i, j) {
return document.getElementById("leaf_value" + i + j);
}
function createLeafEnableFunction(i, j, enable) {
return function () {
getLeaf(i, j).disabled = !enable;
};
}
/**
* Creates and returns an input element with id "cellij"
* @param i The 0-indexed row of the cell
* @param j The 0-indexed column of the cell
*/
function newTreeCell(i, j) {
var input = document.createElement("input");
input.type = "text";
input.size = "5";
input.id = "cell" + i + j;
input.placeholder = "value";
colorCell(input, "base");
input.onchange = createUncolorInputBorderFunction(i, j);
return input;
}
/**
* Creates and returns a div element with id "cellij".
* The div contains a Leaf input element.
* The div also contains radio button toggles and a Branch option if the cell
* is not on the last row.
* @param i The 0-indexed row of the cell
* @param j The 0-indexed column of the cell
*/
function newShrubCell(i, j) {
// create div container
var div = document.createElement("div");
div.id = "cell" + i + j;
div.classList.add("left-inner");
div.classList.add("shrub-div");
div.align = "left";
colorCell(div, "base");
// if not last level, cell has Branch or Leaf option
if (i < depth - 1) {
// create Branch radio button
var branchBtn = document.createElement("input");
branchBtn.type = "radio";
branchBtn.id = "branch_btn" + i + j;
branchBtn.name = "radio" + i + j;
div.appendChild(branchBtn);
div.innerHTML += "Branch<br/>";
// create Leaf radio button
var leafBtn = document.createElement("input");
leafBtn.type = "radio";
leafBtn.id = "leaf_btn" + i + j;
leafBtn.name = "radio" + i + j;
div.appendChild(leafBtn);
}
div.innerHTML += "Leaf: ";
var leafValue = document.createElement("input");
leafValue.type = "text";
leafValue.size = "5";
leafValue.id = "leaf_value" + i + j;
leafValue.placeholder = "value";
div.appendChild(leafValue);
return div;
}
/**
* Returns the SML version of the tree rooted at cell i, j
* @param i The 0-indexed row of the cell root
* @param j The 0-indexed column of the cell root
*/
function treeTextHelper(i, j) {
if (i == depth) {
// shouldn't be possible for shrubs
if(activeTab == "lazytree") return "LazyTree.delay (fn () => LazyTree.LEAF)";
return "Empty";
}
var cellij = getCell(i, j);
switch (activeTab) {
case "tree":
if (cellij.value == "") return "Empty";
colorCell(cellij, "valid");
if (cellij.value.includes("-")) {
setNegativeWarningText("Warning: Negative sign used instead of ~");
}
return (
"Node(" +
treeTextHelper(i + 1, j * 2) +
"," +
cellij.value +
"," +
treeTextHelper(i + 1, j * 2 + 1) +
")"
);
break;
case "lazytree":
if (cellij.value == "") return "LazyTree.delay (fn () => LazyTree.LEAF)";
colorCell(cellij, "valid");
if (cellij.value.includes("-")) {
setNegativeWarningText("Warning: Negative sign used instead of ~");
}
return (
"LazyTree.delay (fn () => LazyTree.NODE (" +
treeTextHelper(i + 1, j * 2) +
"," +
cellij.value +
"," +
treeTextHelper(i + 1, j * 2 + 1) +
"))"
);
break;
case "shrub":
// leaf cell
if (
i == depth - 1 ||
document.getElementById("leaf_btn" + i + j).checked
) {
var leafij = getLeaf(i, j);
colorCell(cellij, "valid");
if (leafij.value == "")
setTableWarningText("Warning: Empty leaf value");
if (leafij.value.includes("-")) {
setNegativeWarningText("Warning: Negative sign used instead of ~");
}
return "Leaf(" + leafij.value + ")";
}
// branch cell
if (document.getElementById("branch_btn" + i + j).checked) {
colorCell(cellij, "valid");
return (
"Branch(" +
treeTextHelper(i + 1, j * 2) +
"," +
treeTextHelper(i + 1, j * 2 + 1) +
")"
);
}
if (i == 0) {
return "Empty";
} else {
throw "Branch without valid children";
}
break;
}
}
/**
* Called when 'Generate SML Text' button clicked
*/
function generateText() {
if (activeTab === "rose") {
generateRoseText();
} else {
// reset error text
resetWarningText();
// uncolor all input borders
for (var i = 0; i < depth; i++) {
for (var j = 0; j < Math.pow(2, i); j++) {
uncolorInputBorder(i, j);
}
}
// compute SML text from tree/shrub
var text;
try {
text = treeTextHelper(0, 0);
} catch (e) {
console.log(e);
text = "Node must have 2 valid children.";
}
// set SML output text
setSMLOutputText(text);
}
}
/**
* Called when the user opens a certain tab.
*/
function openTab(evt, tabName) {
// Declare all variables
var tabContent, tabLinks, toSMLContent, fromSMLContent;
activeTab = tabName;
depth = null;
// Get all elements with class="tab-content" and hide them
tabContent = document.getElementsByClassName("tab-content");
for (let i = 0; i < tabContent.length; i++) {
tabContent[i].style.display = "none";
}
// Get all elements with class="tab-links" and remove the class "active"
tabLinks = document.getElementsByClassName("tab-links");
for (let i = 0; i < tabLinks.length; i++) {
tabLinks[i].className = tabLinks[i].className.replace(" active", "");
}
evt.currentTarget.className += " active";
// Show the current tab, and add an "active" class to the button that opened the tab
document.getElementById(tabName).style.display = "block";
toSMLContent = document.getElementById("to_sml_content");
fromSMLContent = document.getElementById("from_sml_content");
depthContent = document.getElementById("depth_content");
if (tabName === "sml") {
toSMLContent.style.display = "none";
fromSMLContent.style.display = "block";
} else {
toSMLContent.style.display = "block";
fromSMLContent.style.display = "none";
}
resetPage();
if (tabName === "rose") {
depthContent.style.display = "none";
document.getElementById("generate_sml_text_btn").disabled = false;
generateRoseTable();
} else {
depthContent.style.display = "block";
resetDepth();
}
}
const depthForm = document.getElementById("depth_form");
depthForm.addEventListener("keyup", function (event) {
if (event.key === "Enter") {
document.getElementById("enter_depth_btn").onclick();
}
});
const smlForm = document.getElementById("sml_form");
smlForm.addEventListener("keyup", function (event) {
if (event.key === "Enter") {
document.getElementById("generate_tree_btn").onclick();
}
});
document.getElementById("default_open").click();