-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.html
145 lines (122 loc) · 2.79 KB
/
calculator.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
<style>
body {
font-family: Arial;
font-size: 120%;
}
td, th {
padding: 6px 16px;
}
table {
margin: 2em auto;
}
</style>
<body>
<script>
/*
AddSub ::= MulDiv ('+' MulDiv | '-' MulDiv)*
MulDiv ::= Exp ('*' Exp | '/' Exp)*
Exp ::= Factor ('**' Factor)*
Factor ::= ['-'] (Number | '(' AddSub ')')
Number ::= Digit+
*/
const {PI} = Math;
const blank = /\s/;
const num = /^NaN|^pi|^PI|^[0-9]+(\.[0-9]*)?(e[0-9]+)?|^[0-9]*\.[0-9]+(e[0-9]+)?/;
function parseAddSub(s, i=0){
let [j, v] = parseMulDiv(s, i);
while (j<s.length){
if (blank.test(s[j])) j++;
else if (s[j]=='+'){
const [k, w] = parseMulDiv(s, j+1);
[j, v] = [k, v+w];
} else if (s[j]=='-'){
const [k, w] = parseMulDiv(s, j+1);
[j, v] = [k, v-w];
} else break;
}
return [j,v];
}
function parseMulDiv(s, i){
let [j, v] = parseExponent(s, i);
while (j<s.length){
if (blank.test(s[j])) j++;
else if (s[j]=='*'){
const [k, w] = parseExponent(s, j+1);
[j, v] = [k, v*w];
} else if (s[j]=='/'){
const [k, w] = parseExponent(s, j+1);
[j, v] = [k, v/w];
} else break;
}
return [j,v];
}
function parseExponent(s, i){ // right associative
let [j, v] = parseFactor(s, i);
while (j<s.length){
if (blank.test(s[j])) j++;
else if (s[j]=='*'&&s[j+1]=='*') {
const [k, w] = parseExponent(s, j+2);
return [k, v**w];
} else break;
}
return [j, v];
}
function parseFactor(s, i){
let factor = 1;
while (i<s.length){
if (blank.test(s[i])) i++;
else if (s[i]=='+') i++;
else if (s[i]=='-') {factor *= -1; i++; }
else if (s[i]=='(') {
const [j, v] = parseAddSub(s, i+1);
if (s[j]!==')') throw new Error('parentheses not matched '+s.slice(i,j));
return [j+1, factor * v];
} else {
const [j, v] = parseNumber(s, i);
return [j, factor * v];
}
}
}
function parseNumber(s, i){
while (blank.test(s[i])) i++;
let m = s.slice(i).match(num);
if (!m) {
debugger;
throw new Error('expected number, received '+s.slice(i));
}
const v = m[0];
return [i+v.length, v=='pi'||v=='PI'?PI:parseFloat(v)];
}
// todo make it work with a binary tree, instead of directly evaluating
var testCases = [
"2 * (4+2)",
"2* 3 +4+3 *8*(7+1) + 6",
" -2 ** 3",
" (-.201e1) ** 3",
" 2 ** 2 ** 3",
" 2 ** (+2 - 3)",
" 2 ** -3 ",
" 2 ** -3.14 ",
" 2.001 ** -PI ",
"(1+2)*3+5*(4*6)",
"(1+2)*3+6*5*(2*3)",
"1+ (2*(4+3)+6)*8",
"(4+5) + ((2*(1+3)+4)*4+5)*4 +5*6",
"2*(4 - 2 -5) *6 +24 /5",
"2+((9-4/7)*2-1)-18*(14/4/(2*(3+5)))"
]
var lines = testCases.map(s=>[s, parseAddSub(s)[1], evil(s)]);
document.body.innerHTML += `<table>
<tr><th>input<th>calculated<th>eval
${lines.map(([s, ev, evi])=>`<tr><td>${s}<td>${ev}<td>${evi}`).join('\n')}
</table>`;
// for (const s of testCases) {
// console.log(parseAddSub(s), evil(s));
// }
function evil(s){
try {
return eval(s);
} catch(e) {return e}
}
</script>
</body>