forked from codebymitch/TitanBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafeMathParser.js
More file actions
295 lines (237 loc) · 7.08 KB
/
Copy pathsafeMathParser.js
File metadata and controls
295 lines (237 loc) · 7.08 KB
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
289
290
291
292
293
294
295
// safeMathParser.js
const SUPPORTED_FUNCTIONS = Object.freeze({
sin: Math.sin,
cos: Math.cos,
tan: Math.tan,
sqrt: Math.sqrt,
abs: Math.abs,
log: Math.log,
log10: Math.log10,
exp: Math.exp
});
const SUPPORTED_CONSTANTS = Object.freeze({
pi: Math.PI,
e: Math.E
});
const OPERATOR_PRECEDENCE = Object.freeze({
'u-': 5,
'^': 4,
'*': 3,
'/': 3,
'%': 3,
'+': 2,
'-': 2
});
const RIGHT_ASSOCIATIVE_OPERATORS = new Set(['^', 'u-']);
function normalizeExpression(input) {
if (typeof input !== 'string') {
throw new Error('Expression must be a string');
}
return input
.trim()
.toLowerCase()
.replace(/×/g, '*')
.replace(/÷/g, '/')
.replace(/π/g, 'pi')
.replace(/√/g, 'sqrt');
}
function preprocessDegrees(expression) {
return expression.replace(/(\d+(?:\.\d+)?)\s*deg\b/g, '($1*pi/180)');
}
function isDigit(character) {
return character >= '0' && character <= '9';
}
function isAlpha(character) {
return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || character === '_';
}
function tokenize(expression) {
const tokens = [];
let index = 0;
while (index < expression.length) {
const character = expression[index];
if (character === ' ' || character === '\t' || character === '\n' || character === '\r') {
index += 1;
continue;
}
if (isDigit(character) || character === '.') {
let numberText = '';
let dotCount = 0;
while (index < expression.length) {
const current = expression[index];
if (!isDigit(current) && current !== '.') {
break;
}
if (current === '.') {
dotCount += 1;
if (dotCount > 1) {
throw new Error('Invalid number format');
}
}
numberText += current;
index += 1;
}
if (numberText === '.' || numberText.length === 0) {
throw new Error('Invalid number format');
}
tokens.push({ type: 'number', value: Number(numberText) });
continue;
}
if (isAlpha(character)) {
let identifier = '';
while (index < expression.length && isAlpha(expression[index])) {
identifier += expression[index];
index += 1;
}
if (Object.prototype.hasOwnProperty.call(SUPPORTED_CONSTANTS, identifier)) {
tokens.push({ type: 'number', value: SUPPORTED_CONSTANTS[identifier] });
continue;
}
if (Object.prototype.hasOwnProperty.call(SUPPORTED_FUNCTIONS, identifier)) {
tokens.push({ type: 'function', value: identifier });
continue;
}
throw new Error(`Unsupported token: ${identifier}`);
}
if ('+-*/%^()'.includes(character)) {
if (character === '(') {
tokens.push({ type: 'leftParen', value: character });
} else if (character === ')') {
tokens.push({ type: 'rightParen', value: character });
} else {
tokens.push({ type: 'operator', value: character });
}
index += 1;
continue;
}
throw new Error(`Unsupported character: ${character}`);
}
return tokens;
}
function toRpn(tokens) {
const output = [];
const stack = [];
let previousTokenType = null;
for (const token of tokens) {
if (token.type === 'number') {
output.push(token);
previousTokenType = 'number';
continue;
}
if (token.type === 'function') {
stack.push(token);
previousTokenType = 'function';
continue;
}
if (token.type === 'operator') {
let operatorValue = token.value;
if (operatorValue === '-' && (previousTokenType === null || previousTokenType === 'operator' || previousTokenType === 'leftParen' || previousTokenType === 'function')) {
operatorValue = 'u-';
}
while (stack.length > 0) {
const top = stack[stack.length - 1];
if (top.type !== 'operator') {
break;
}
const currentPrecedence = OPERATOR_PRECEDENCE[operatorValue];
const topPrecedence = OPERATOR_PRECEDENCE[top.value];
const isRightAssociative = RIGHT_ASSOCIATIVE_OPERATORS.has(operatorValue);
if ((isRightAssociative && currentPrecedence < topPrecedence) || (!isRightAssociative && currentPrecedence <= topPrecedence)) {
output.push(stack.pop());
} else {
break;
}
}
stack.push({ type: 'operator', value: operatorValue });
previousTokenType = 'operator';
continue;
}
if (token.type === 'leftParen') {
stack.push(token);
previousTokenType = 'leftParen';
continue;
}
if (token.type === 'rightParen') {
let hasOpeningParen = false;
while (stack.length > 0) {
const top = stack.pop();
if (top.type === 'leftParen') {
hasOpeningParen = true;
break;
}
output.push(top);
}
if (!hasOpeningParen) {
throw new Error('Mismatched parentheses');
}
if (stack.length > 0 && stack[stack.length - 1].type === 'function') {
output.push(stack.pop());
}
previousTokenType = 'rightParen';
}
}
while (stack.length > 0) {
const top = stack.pop();
if (top.type === 'leftParen' || top.type === 'rightParen') {
throw new Error('Mismatched parentheses');
}
output.push(top);
}
return output;
}
function evaluateRpn(rpnTokens) {
const stack = [];
for (const token of rpnTokens) {
if (token.type === 'number') {
stack.push(token.value);
continue;
}
if (token.type === 'operator') {
if (token.value === 'u-') {
if (stack.length < 1) {
throw new Error('Invalid expression');
}
stack.push(-stack.pop());
continue;
}
if (stack.length < 2) {
throw new Error('Invalid expression');
}
const right = stack.pop();
const left = stack.pop();
if (token.value === '+') stack.push(left + right);
else if (token.value === '-') stack.push(left - right);
else if (token.value === '*') stack.push(left * right);
else if (token.value === '/') stack.push(left / right);
else if (token.value === '%') stack.push(left % right);
else if (token.value === '^') stack.push(Math.pow(left, right));
else throw new Error(`Unsupported operator: ${token.value}`);
continue;
}
if (token.type === 'function') {
if (stack.length < 1) {
throw new Error('Invalid function usage');
}
const value = stack.pop();
const handler = SUPPORTED_FUNCTIONS[token.value];
if (!handler) {
throw new Error(`Unsupported function: ${token.value}`);
}
stack.push(handler(value));
continue;
}
}
if (stack.length !== 1) {
throw new Error('Invalid expression');
}
return stack[0];
}
export function evaluateMathExpression(expression) {
const normalized = preprocessDegrees(normalizeExpression(expression));
const tokens = tokenize(normalized);
const rpn = toRpn(tokens);
const value = evaluateRpn(rpn);
if (!Number.isFinite(value)) {
throw new Error('Expression result is not finite');
}
return value;
}