-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
251 lines (210 loc) · 6.95 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
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
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link href="https://unpkg.com/treeflex/dist/css/treeflex.css" rel="stylesheet">
<title> Expression Evaluator Tree Creator </title>
</head>
<script>
//parsing logic for each criteria
var parse = function(num,content){
if(num != 0){
//it's the next expression
console.log('Result till '+num);
add('Result till '+num);
}
content = content.trim();
//a critera can have either && or ||
//lets for && first
if(content.includes('&&') == false && content.includes('||') == false) {
var arrayWithSpace = content.split(' ');
if(arrayWithSpace[0] == undefined || arrayWithSpace[1] == undefined || arrayWithSpace[2] == undefined ) return;
console.log('Operand : '+ arrayWithSpace[0] +' | Operator : '+arrayWithSpace[1] + ' | Value : '+arrayWithSpace[2]);
add(content);
}
else if(content.includes('&&')){
var arrayWithAND = content.split('&&');
for(var index = 0; index < arrayWithAND.length ; index++){
if(arrayWithAND[index].trim() == ''){
console.log('&&');
add('&&');
continue;
}
//check for || now
var arrayWithOR = arrayWithAND[index].split('||');
// console.log('OR : '+arrayWithOR);
for(var index2 = 0; index2 < arrayWithOR.length ; index2++){
var string = arrayWithOR[index2].trim();
if(string == '') {
console.log('||');
add('||')
continue;
}
var arrayWithSpace = string.split(' ');
console.log('Operand : '+ arrayWithSpace[0] +' | Operator : '+arrayWithSpace[1] + ' | Value : '+arrayWithSpace[2]);
add(string);
if(index2 + 1 != arrayWithOR.length){
console.log('||');
add('||');
}
}
if(index + 1 != arrayWithAND.length){
console.log('&&');
add('&&');
}
}
}
else{
var arrayWithOR = content.split('||');
for(var index = 0; index < arrayWithOR.length ; index++){
if(arrayWithOR[index].trim() == ''){
console.log('||');
add('||');
continue;
}
//check for || now
var arrayWithAND = arrayWithOR[index].split('&&');
// console.log('OR : '+arrayWithAND);
for(var index2 = 0; index2 < arrayWithAND.length ; index2++){
var string = arrayWithAND[index2].trim();
if(string == '') {
console.log('&&');
add('&&')
continue;
}
var arrayWithSpace = string.split(' ');
console.log('Operand : '+ arrayWithSpace[0] +' | Operator : '+arrayWithSpace[1] + ' | Value : '+arrayWithSpace[2]);
add(string);
if(index2 + 1 != arrayWithAND.length){
console.log('&&');
add('&&');
}
}
if(index + 1 != arrayWithOR.length){
console.log('||');
add('||');
}
}
}
};
var isValid = function(s) {
var stack = [];
var index = 0;
var num = 0;
var content = '';
//base condition, to check the start
if(s[index] != '(' && s[index] != '[' && s[index] != '{') {
//of no bracket found just return
console.log('Not a valid String '+s[index] + ' at index '+index);
add('Not a valid String '+s[index] + ' at index '+index);
return;
}
else{
//else push it in stack
stack.push(s[index]);
index++;
}
while (s[index] != null) {
//check if it is an opening
if(s[index] == '(' || s[index] == '[' || s[index] == '{'){
//push it
stack.push(s[index]);
}
//if it is a closing
else if(s[index] == ')' || s[index] == ']' || s[index] == '}'){
//take the top
var topElement = stack[stack.length - 1];
var currentElement = s[index];
//and compare the closing with top. if it is valid
if((topElement == '(' && currentElement == ')') || ( topElement == '[' && currentElement == ']' ) || (topElement == '{' && currentElement == '}')){
//valid, remove the top element
stack.pop();
console.log(topElement);
add(topElement);
parse(num,content);
console.log(currentElement);
add(currentElement);
num++;
content = '';
}//else string is defective, not needt to process
else{
console.log('Not a valid String '+s[index] + ' at index '+index);
add('Not a valid String '+s[index] + ' at index '+index);
return;
}
}
else{
//save the content, betwwen the openeing and closing
content += s[index];
}
index++;
}
};
function createTree(){
var expression = document.getElementById('expression').value;
clear();
if(expression == undefined || expression == null || expression == "" || expression.trim() ==""){
$('#result').append('<span>Please provide some input!</span></br>');
return;
}
isValid(expression);
createView();
}
function createView(){
var node = '<ul><li><span class="tf-nc">Final Result</span><ul>';
var tempNodes = [];
var foundClosing = false;
var i;
for(i = contentArray.length - 1 ; i >= 0 ; i--){
if(contentArray[i].includes(')') || contentArray[i].includes(']') || contentArray[i].includes('}')){
foundClosing = true;
continue;
}
if(contentArray[i].includes('(') || contentArray[i].includes('[') || contentArray[i].includes('{')){
foundClosing = false;
continue;
}
if(foundClosing){
tempNodes.push('<span class="tf-nc">'+contentArray[i]+'</span>');
}
}
var count = 0;
for(item in tempNodes){
if(tempNodes[item].includes("Result")){
node += '<li>'+tempNodes[item]+'<ul>';
count++;
}else{
var str = tempNodes[item].substring(20);
str = str.replaceAll("</span>","");
var opArray = str.trim().split(" ");
if(opArray.length == 3){
node += '<li><span class="tf-nc">EXP</span><ul><li><span class="tf-nc">'+opArray[0]+'</span></li>';
node += '<li><span class="tf-nc">'+opArray[1]+'</span></li>';
node += '<li><span class="tf-nc">'+opArray[2]+'</span></li></ul></li>';
}
else{
node += '<li>'+tempNodes[item]+'</li>';
}
}
}
while(count > 0){
node += '</ul></li>';
count--;
}
node +='</ul></li></ul>';
$('#result').append(node);
console.log(node);
}
function add(item){
//$('#result').append('<span>'+item+'</span></br>');
contentArray.push(item);
}
function clear(){
$('#result').empty();
contentArray = [];
}
</script>
<h1>Expression Evaluator Tree Creator</h1>
<textarea id="expression" style="width: 100%;" placeholder="( ( [a > 1] && c == 3 )|| d == 4)"></textarea></br></br>
<button onclick="createTree()"> Create Tree </button></br>
</br>
<div id="result" class="tf-tree">
</div>