-
Notifications
You must be signed in to change notification settings - Fork 0
/
booleval.js
106 lines (104 loc) · 4.08 KB
/
booleval.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
/*
evaluates a boolean expression
*/
function boolEval(expression, inversion = false) {
if (expression.includes("||") || expression.includes("&&")) {
for (let i = 0; i < expression.length; i++) {
while (expression[i] == " ") {
i++;
}
if (expression[i] == "(") {
let end_parenthesis = lastParentesis(expression, i);
if (end_parenthesis == expression.length - 1) {
return inversion ? !boolEval(expression.slice(i + 1, end_parenthesis)) : !boolEval(expression.slice(i + 1, end_parenthesis));
} else {
let left = expression.slice(i + 1, end_parenthesis);
i = end_parenthesis + 1;
while (expression[i] == " ") {
i++;
}
let next_space = expression.indexOf(" ", i);
next_space == -1 ? expression.length : next_space;
switch (expression.substring(i, next_space)) {
case "||":
return (inversion ? !boolEval(left) : boolEval(left)) || boolEval(expression.slice(next_space + 1));
case "&&":
return (inversion ? !boolEval(left) : boolEval(left)) && boolEval(expression.slice(next_space + 1));
default:
console.log("Error.")
break;
}
}
} else if (expression[i] == "!") {
return boolEval(expression.slice(i + 1, expression.length), true);
} else {
let next_space = expression.indexOf(" ", i);
next_space == -1 ? expression.length : next_space;
switch (expression.substring(i, next_space)) {
case "||":
return boolEval(expression.substring(0, i - 1)) || boolEval(expression.substring(next_space + 1, expression.length));
case "&&":
return boolEval(expression.substring(0, i - 1)) && boolEval(expression.substring(next_space + 1, expression.length));
default:
i = next_space;
break;
}
}
}
} else {
if (expression[0] == "(") {
let end_parenthesis = lastParentesis(expression, 0);
return inversion ? !boolEval(expression.slice(1, end_parenthesis)) : boolEval(expression.slice(1, end_parenthesis));
} else if (expression[0] == "!") {
return inversion ? !boolEval(expression.slice(1, expression.length), true) : boolEval(expression.slice(1, expression.length), true);
} else {
let branch = expression.split(" ");
if (!isNaN(branch[0]) && !isNaN(branch[2])) {
return inversion ? !nodeEval(parseFloat(branch[0]), parseFloat(branch[2]), branch[1]) : nodeEval(parseFloat(branch[0]), parseFloat(branch[2]), branch[1]);
}
}
}
}
/*
node eval
interprets the relation between two nodes
*/
function nodeEval(node1, node2, operator) {
switch (operator) {
case "==":
return node1 == node2;
case "!=":
return node1 != node2;
case ">":
return node1 > node2;
case "<":
return node1 < node2;
case ">=":
return node1 >= node2;
case "<=":
return node1 <= node2;
case "and":
case "&&":
return node1 && node2;
case "or":
case "||":
return node1 || node2;
}
}
/*
finds the last parenthesis as we give the start one
*/
function lastParentesis(expression, start) {
let parenthesis_count = 0;
for (let i = start; i < expression.length; i++) {
if (expression[i] == "(") {
parenthesis_count++;
} else if (expression[i] == ")") {
parenthesis_count--;
}
if (parenthesis_count == 0) {
return i;
}
}
return -1;
}