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:35 +00:00
|
|
|
importScripts("./lexer.js", "./parser.js", "./math.js")
|
2025-12-12 04:49:34 +00:00
|
|
|
|
|
|
|
|
addEventListener("message", (message) => {
|
2025-12-12 04:49:35 +00:00
|
|
|
if (message.data.command === "evaluate") {
|
2025-12-12 04:49:35 +00:00
|
|
|
const expr = message.data.expr;
|
|
|
|
|
const modulus = BigInt(message.data.modulus);
|
|
|
|
|
const result = evaluate(expr, modulus);
|
2025-12-12 04:49:34 +00:00
|
|
|
postMessage(result);
|
2025-12-12 04:49:34 +00:00
|
|
|
}
|
|
|
|
|
});
|
2025-12-12 04:49:34 +00:00
|
|
|
|
2025-12-12 04:49:35 +00:00
|
|
|
function evaluate(expr, m) {
|
|
|
|
|
if (m <= 0n) {
|
|
|
|
|
throw new Error("invalid modulus");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tokens = tokenize(expr);
|
|
|
|
|
console.log(tokens);
|
|
|
|
|
const queue = shunt(tokens);
|
|
|
|
|
console.log(queue);
|
|
|
|
|
const result = compute(queue, m);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 04:49:34 +00:00
|
|
|
function binaryOpPop(stack) {
|
|
|
|
|
const b = stack.pop();
|
|
|
|
|
const a = stack.pop();
|
|
|
|
|
if (a === undefined || b === undefined) {
|
|
|
|
|
throw new Error("invalid expression");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [a, b];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function compute(queue, modulus) {
|
|
|
|
|
const stack = [];
|
|
|
|
|
for (const token of queue) {
|
|
|
|
|
if (typeof token === "bigint") {
|
|
|
|
|
stack.push(token);
|
|
|
|
|
} else if (token === "+") {
|
|
|
|
|
let [a, b] = binaryOpPop(stack);
|
|
|
|
|
a %= modulus;
|
|
|
|
|
b %= modulus;
|
|
|
|
|
const c = (a + b) % modulus;
|
|
|
|
|
stack.push(c);
|
|
|
|
|
} else if (token === "-") {
|
|
|
|
|
let [a, b] = binaryOpPop(stack);
|
|
|
|
|
a %= modulus;
|
|
|
|
|
b %= modulus;
|
|
|
|
|
const c = (a - b) % modulus;
|
|
|
|
|
stack.push(c);
|
|
|
|
|
} else if (token === "*") {
|
|
|
|
|
let [a, b] = binaryOpPop(stack);
|
|
|
|
|
a %= modulus;
|
|
|
|
|
b %= modulus;
|
|
|
|
|
const c = (a * b) % modulus;
|
|
|
|
|
stack.push(c);
|
|
|
|
|
} else if (token === "/") {
|
|
|
|
|
let [a, b] = binaryOpPop(stack);
|
|
|
|
|
a %= modulus;
|
|
|
|
|
b %= modulus;
|
|
|
|
|
const binv = modinv(b, modulus);
|
|
|
|
|
const c = (a * binv) % modulus;
|
|
|
|
|
stack.push(c);
|
2025-12-12 04:49:34 +00:00
|
|
|
} else if (token === "u") {
|
|
|
|
|
let a = stack.pop();
|
|
|
|
|
if (a === undefined) {
|
|
|
|
|
throw new Error("invalid expression");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a *= -1n;
|
|
|
|
|
stack.push(a);
|
2025-12-12 04:49:34 +00:00
|
|
|
} else if (token === "^") {
|
|
|
|
|
const [a, b] = binaryOpPop(stack);
|
|
|
|
|
const c = modpow(a, b, modulus);
|
|
|
|
|
stack.push(c);
|
2025-12-12 04:49:34 +00:00
|
|
|
} else if (token === "sqrt") {
|
|
|
|
|
const a = stack.pop();
|
2025-12-12 04:49:34 +00:00
|
|
|
const s = modsqrt(a, modulus);
|
2025-12-12 04:49:34 +00:00
|
|
|
stack.push(s);
|
2025-12-12 04:49:34 +00:00
|
|
|
} else if (token === "ord") {
|
|
|
|
|
const a = stack.pop();
|
|
|
|
|
const r = ord(a, modulus);
|
|
|
|
|
stack.push(r);
|
2025-12-12 04:49:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stack.length !== 1) {
|
|
|
|
|
throw new Error("error evaluating expression");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let result = stack[0] % modulus;
|
|
|
|
|
if (result < 0n) {
|
|
|
|
|
result += modulus;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 04:49:34 +00:00
|
|
|
return result;
|
2025-12-12 04:49:34 +00:00
|
|
|
}
|