2025-12-12 04:49:34 +00:00
|
|
|
/*
|
|
|
|
|
Copyright (C) 2025 filifa
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
// TODO: it would probably be beneficial to create some sort of
|
|
|
|
|
// function token instead of updating strings in several different
|
|
|
|
|
// places
|
|
|
|
|
const regexp = /[0-9]+|[-+*/^]|\(|\)|sqrt|ord/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])
|
2025-12-12 04:49:34 +00:00
|
|
|
} else if (match[0] === "(") {
|
2025-12-12 04:49:34 +00:00
|
|
|
tokens.push("(");
|
2025-12-12 04:49:34 +00:00
|
|
|
} else if (match[0] === ")") {
|
2025-12-12 04:49:34 +00:00
|
|
|
tokens.push(")");
|
2025-12-12 04:49:34 +00:00
|
|
|
} else if (match[0] === "sqrt") {
|
2025-12-12 04:49:34 +00:00
|
|
|
tokens.push("sqrt")
|
2025-12-12 04:49:34 +00:00
|
|
|
} else if (match[0] === "ord") {
|
|
|
|
|
tokens.push("ord")
|
2025-12-12 04:49:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tokens;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { tokenize };
|