-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
170 lines (140 loc) · 4.79 KB
/
index.html
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
<script src="bundle.js"></script>
<form>
<br />Expression: <input type="text" id="expr" />
<br />Input Value: <input type="text" id="value" />
<br />Input expected: <input type="text" id="expected" />
<br />Input Result: <input type="text" id="result" />
<br />Input Value: <input type="button"
class='input_submit'
name="submit "
style="margin-right: 15px;"
onClick="execute()" />
<div id="message"></div>
</form>
<div id="treeDisplay">
Tree:
</div>
<style>
/* Remove default bullets */
ul, #myUL {
list-style-type: none;
}
/* Remove margins and padding from the parent ul */
#myUL {
margin: 0;
padding: 0;
}
/* Style the caret/arrow */
.caret {
cursor: pointer;
user-select: none; /* Prevent text selection */
}
/* Create the caret/arrow with a unicode, and style it */
.caret::before {
content: "\25B6";
color: black;
display: inline-block;
margin-right: 6px;
}
/* Rotate the caret/arrow icon when clicked on (using JavaScript) */
.caret-down::before {
transform: rotate(90deg);
}
/* Hide the nested list */
.nested {
display: none;
}
/* Show the nested list when the user clicks on the caret/arrow (with JavaScript) */
.active {
display: block;
}
</style>
<script>
function toggleTree() {
var toggler = document.getElementsByClassName("caret");
var i;
for (i = 0; i < toggler.length; i++) {
toggler[i].addEventListener("click", function () {
this.parentElement.querySelector(".nested").classList.toggle("active");
this.classList.toggle("caret-down");
});
}
}
function execute() {
const expr = document.getElementById("expr");
const res = document.getElementById("result");
let ret = testExpr(expr.value, document.getElementById("value").value, document.getElementById("expected").value);
res.value = ret.result;
}
function testExpr(expr, value, res) {
let msg = '';
const engine = new ExprEngine.ExpressionEngine();
const ret = engine.evaluate(expr);
displayExpressionTree(ret.expression);
return;
displayExpression(ret.expression.rootNode);
tree.forEach(line => {
html+=line;
});
treeHtml.innerHTML='<b><table>'+ html+'</table></b>';
if (ret !== res) {
msg = "***Error -" + expr + " for " + value + "result: " + ret + "expecting:" + res;
console.log(msg);
}
document.getElementById("message").innerHTML = msg;
return ret;
}
function testCond(expr, value, res,) {
let msg = '';
const engine = new ExprEngine.ExpressionEngine();
const ret = engine.evaluateCondition(expr, value);
if (ret !== res) {
msg = "***Error -" + expr + " for " + value + "result: " + ret + "expecting:" + res;
console.log(msg);
}
document.getElementById("message").innerHTML = msg;
return ret;
}
var tree = [];
function displayExpression(expr, level = 0) {
expr.loop(function (expr, level) {
let msg = '';
let res = (expr.result ? `result=<${expr.result}> ` : '');
msg = level + '-'.repeat(level + 1) + ">" + expr.type;
msg += "</td><td> #" + expr.id;
msg += "</td><td> " + expr.value;
msg += "</td><td> " +res;
tree.push('<tr><td>'+msg+'</td></tr>');
console.log(msg);
}, level);
}
function displayTree(expr, level = 0) {
let msg = '';
let res = (expr.result ? `result=<${expr.result}> ` : '');
msg = level + '-'.repeat(level + 1) + ">" + expr.type;
msg += " #" + expr.id;
msg += " " + expr.value;
msg += " " + res;
if (expr.children.length > 0) {
tree.push('<li><span class="caret">' + msg + '</span>');
tree.push('<ul class="nested">');
expr.children.forEach(child => {
displayTree(child, level + 1);
});
tree.push('</ul></li>');
}
else
tree.push('<li>' + msg + '</li>');
}
function displayExpressionTree(expr) {
tree = [];
displayTree(expr.rootNode);
const treeHtml = document.getElementById('treeDisplay');
let html = '';
tree.forEach(line => {
html += line;
});
treeHtml.innerHTML = '<ul id="myUL">' + html + '</ul>';
toggleTree();
}
</script>