-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
288 lines (272 loc) · 8.38 KB
/
main.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
/**
* @author Ethan Braunstein
*
* @typedef {string[]} stringArray
* @typedef {"&" | "|" | "-" | "*"} operator
*/
// Init global constants
const A = new Set();
A.add("a");
A.add("ab");
A.add("ac");
A.add("abc");
const B = new Set();
B.add("b");
B.add("ab");
B.add("bc");
B.add("abc");
const C = new Set();
C.add("c");
C.add("bc");
C.add("ac");
C.add("abc");
const OPERATORS = ["&", "|", "-", "*"];
/**
* Checks if parentheses in a string are balanced.
* @param {string} string
*/
function hasBalancedParentheses(string) {
let stack = []; //Init stack
for (const char of string) {
// Loop through chars in string
if (char === "(") {
stack.push(char); // If "(", push to stack
} else if (char === ")") {
if (stack.length === 0) {
// Edge case where there is a ")" before a "(" is found
return false;
}
stack.pop(); // If ")", pop from stack
}
}
return stack.length === 0;
}
/**
* Checks if string contains parentheses.
* @param {string} string
*/
function hasParentheses(string) {
return string.includes("(") || string.includes(")");
}
/**
* Counts number of parentheses pairs in a string.
* @param {string} string
*/
function countParenthesesPairs(string) {
let pairsCount = 0;
let openCount = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] === "(") {
openCount++;
} else if (string[i] === ")") {
if (openCount > 0) {
pairsCount++;
openCount--;
} else {
console.log("Error: Unmatched closing parenthesis at index " + i);
}
}
}
return pairsCount;
}
/**
* Separates highest depth operation into an array.
* @param {string} expr
* @return {stringArray} operation
*/
function findOperation(expr) {
let stack = []; // Init stack
for (let i = 0; i < expr.length; i++) {
// Loop through index of expr
if (expr[i] === "(") {
stack.push("("); // If "(", push to stack
} else if (expr[i] === ")") {
stack.pop(); // If ")", push to stack
} else {
if (stack.length === 0 && OPERATORS.includes(expr[i])) {
// Not in paretheses scope and operator is found
console.log("Operation found:", [expr.slice(0, i), expr[i], expr.slice(i + 1)]);
return [expr.slice(0, i), expr[i], expr.slice(i + 1)]; // operand1, operator, operand2
}
}
}
return null; // No outside operation is found
}
/**
* Translates operation into corresponding Set() method
* @param {string} operand1
* @param {operator} operator Either an "&", "|", "-", or "*".
* @param {string} operand2
*/
function translateOperation(operand1, operator, operand2) {
console.log("Translating:", [operand1, operator, operand2]);
// Concatenate with correct Set() method
if (operator === "&") {
return `${operand1}.union[${operand2}]`;
} else if (operator === "|") {
return `${operand1}.intersection[${operand2}]`;
} else if (operator === "-") {
return `${operand1}.difference[${operand2}]`;
} else if (operator === "*") {
return `${operand1}.symmetricDifference[${operand2}]`;
} else {
return ""; // Edge case
}
}
/**
* Processes text expression into valid Javascript code.
* @param {string} expr
*/
function processExpr(expr) {
expr = expr.replace(/\s+/g, ""); // Remove whitespace
console.log("Processing:", expr);
if (hasParentheses(expr)) {
if (hasBalancedParentheses(expr)) {
console.log("If Case");
let [operand1, operator, operand2] = findOperation(expr); // Separate operation // this 'let' cost me 6 hours of debugging
console.log("Operation:", operand1, operator, operand2);
let translatedOperand1;
let translatedOperand2;
if (countParenthesesPairs(operand1) > 1) {
translatedOperand1 = processExpr(operand1);
} else if (hasParentheses(operand1)) {
console.log("Operand1 has parentheses:", operand1.slice(1, operand1.length - 1));
translatedOperand1 = processExpr(operand1.slice(1, operand1.length - 1));
} else {
translatedOperand1 = operand1;
}
if (countParenthesesPairs(operand1) > 1) {
translatedOperand2 = processExpr(operand2);
} else if (hasParentheses(operand2)) {
console.log("Operand2 has parentheses:", operand2.slice(1, operand2.length - 1));
translatedOperand2 = processExpr(operand2.slice(1, operand2.length - 1));
} else {
translatedOperand2 = operand2;
}
console.log("New operation:", translatedOperand1, operator, translatedOperand2);
return translateOperation(translatedOperand1, operator, translatedOperand2);
//
// return translateOperation(
// hasParentheses(operand1)
// ? (console.log(
// "Operand1 has parentheses:",
// operand1.slice(1, operand1.length - 1)
// ),
// processExpr(operand1.slice(1, operand1.length - 1)))
// : operand1,
// operator,
// hasParentheses(operand2)
// ? (console.log(
// "Operand2 has parentheses:",
// operand2.slice(1, operand2.length - 1)
// ),
// processExpr(operand2.slice(1, operand2.length - 1)))
// : operand2
// );
} else {
console.log("Close all parentheses!");
return;
}
} else {
console.log("Else Case");
[operand1, operator, operand2] = findOperation(expr); // Separate operation
if (operand2.length > 1) {
console.log("Remaining:", operand2.slice(1));
return processExpr(
translateOperation(operand1, operator, operand2[0]) + operand2.slice(1)
);
} else {
return translateOperation(operand1, operator, operand2);
}
}
}
/**
* Evaluates translated operation.
* @param {string} expr
* @returns {stringArray} values
*/
function evaluateOperation(operation_string) {
operation_string = operation_string.replaceAll("[", "(");
operation_string = operation_string.replaceAll("]", ")");
return eval(operation_string + ".values()"); // Get values of JS expression
}
/**
* Handles the oninput event for the textbox.
*/
function inputHandler() {
resetSVG(); // Start modification with blank slate SVG
const expr = document.getElementById("expression_input").value; // Get text expression from textbox
console.log(expr);
updateSVG(evaluateOperation(processExpr(expr))); // Translate text expression, get values, and fill region based on values
}
/**
* Sets the correct regions of the SVG to fill="red".
* @param {stringArray} regions
*/
function updateSVG(regions) {
for (const id of regions) {
// For each region
document
.getElementById("venn")
.getSVGDocument()
.getElementById(id.toUpperCase())
.setAttribute("fill", "red"); // Set fill to "red"
}
}
/**
* Resets all regions of the SVG back to fill="none".
*/
function resetSVG() {
const elements = document
.getElementById("venn")
.getSVGDocument()
.getElementsByClassName("region"); // Get all possible regions
for (const element of elements) {
// For each region
element.setAttribute("fill", "none"); // Set fill to "none"
}
}
// #--------------TESTING--------------#
function runTests() {
console.log("#------------TESTS------------#");
console.log("----------------1", "(A & B) | C");
test("(A & B) | C", "A.union(B).intersection(C)");
console.log("----------------2", "A - (B | C)");
test("A - (B | C)", "A.difference(B.intersection(C))");
console.log("----------------3", "(A & (B | C)) * (C - A)");
test(
"A & (B | C) * (C - A)",
"A.union(B.intersection(C)).symmetricDifference(C.difference(A))"
);
console.log("----------------4", "A & B - C");
test("A & B - C", "A.union(B).difference(C)");
console.log("----------------5", "(A & B) * (C - A) | (A | B)");
test(
"(A & B) * (C - A) | (A | B)",
"(A.union(B).symmetricDifference(C.difference(A))).intersection(A.intersection(B)"
);
console.log("----------------6", "(A * B) | (C | A) & (B - C)");
test(
"(A * B) | (C | A) & (B - C)",
"(A.symmetricDifference(B).intersection(C.intersection(A)).union(B.difference(C))"
);
console.log("----------------7", "(A * B) | (C & A) | ((A | B) * (C * A) | (B - C))");
test(
"(A * B) | (C & A) | ((A | B) * (C * A) | (B - C))",
"A.symmetricDifference(B).intersection(C.union(A)).intersection(A.union(B).symmetricDifference(C.symmetricDifference(A)).intersection(B.difference(C))"
);
console.log("#----------END TESTS----------#");
}
function test(expr, expected_result) {
let result = processExpr(expr);
result = result.replaceAll("[", "(");
result = result.replaceAll("]", ")");
if (result === expected_result) {
console.log("----------------Passed!");
} else {
console.log(`Expected: ${expected_result}`);
console.log(`Got: ${result}`);
console.log("----------------Failed!");
}
}
runTests();