2025-12-12 04:49:34 +00:00
|
|
|
function tokenize(expr) {
|
|
|
|
|
// NOTE: not handling whitespace
|
|
|
|
|
// NOTE: currently ends early if string doesn't match token
|
2025-12-12 04:49:34 +00:00
|
|
|
const regexp = /[0-9]+|[-+*/^]|\(|\)|sqrt/gy;
|
2025-12-12 04:49:34 +00:00
|
|
|
const matches = expr.matchAll(regexp);
|
|
|
|
|
|
|
|
|
|
const tokens = [];
|
|
|
|
|
for (const match of matches) {
|
|
|
|
|
if (/[0-9]+/.test(match[0])) {
|
|
|
|
|
tokens.push(BigInt(match[0]));
|
|
|
|
|
} else if (/[-+*^/]/.test(match[0])) {
|
|
|
|
|
tokens.push(match[0])
|
|
|
|
|
} else if (match[0] == "(") {
|
|
|
|
|
tokens.push("(");
|
|
|
|
|
} else if (match[0] == ")") {
|
|
|
|
|
tokens.push(")");
|
2025-12-12 04:49:34 +00:00
|
|
|
} else if (match[0] == "sqrt") {
|
|
|
|
|
tokens.push("sqrt")
|
2025-12-12 04:49:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tokens;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { tokenize };
|