control the whole evaluation process with a worker
This commit is contained in:
@@ -14,15 +14,30 @@ 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/>.
|
||||
*/
|
||||
importScripts("./math.js")
|
||||
importScripts("./lexer.js", "parser.js", "./math.js")
|
||||
|
||||
addEventListener("message", (message) => {
|
||||
if (message.data.command === "compute") {
|
||||
const result = compute(message.data.queue, message.data.m);
|
||||
const expr = message.data.expr;
|
||||
const modulus = BigInt(message.data.modulus);
|
||||
const result = evaluate(expr, modulus);
|
||||
postMessage(result);
|
||||
}
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function binaryOpPop(stack) {
|
||||
const b = stack.pop();
|
||||
const a = stack.pop();
|
||||
|
||||
45
workers/lexer.js
Normal file
45
workers/lexer.js
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
function tokenize(expr) {
|
||||
// NOTE: not handling whitespace
|
||||
// NOTE: currently ends early if string doesn't match token
|
||||
|
||||
// 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;
|
||||
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(")");
|
||||
} else if (match[0] === "sqrt") {
|
||||
tokens.push("sqrt")
|
||||
} else if (match[0] === "ord") {
|
||||
tokens.push("ord")
|
||||
}
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
126
workers/parser.js
Normal file
126
workers/parser.js
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
function isLeftAssociative(op) {
|
||||
return op === "+" || op === "-" || op === "*" || op === "/";
|
||||
}
|
||||
|
||||
function popOps(opstack, queue, op, powInStack) {
|
||||
const prec = {"+": 1, "-": 1, "*": 2, "/": 2, "u": 3, "^": 3}
|
||||
while (opstack.length > 0) {
|
||||
const op2 = opstack.at(-1);
|
||||
if (op2 === "(") {
|
||||
break;
|
||||
}
|
||||
|
||||
if ((prec[op2] > prec[op]) || (prec[op2] === prec[op] && isLeftAssociative(op))) {
|
||||
opstack.pop();
|
||||
queue.push(op2);
|
||||
if (op2 === "^") {
|
||||
powInStack = false;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// practically, we want 2^(3+4) mod 7 to evaluate to 2^7 mod 7 = 2
|
||||
// however, since our operators are all modular, this would instead
|
||||
// evaluate as 2^0 mod 7 = 1
|
||||
// rather than complicate things by evaluating exponent expressions
|
||||
// normally instead of modularly (and consequently needing to deal with
|
||||
// fractions) we simply require exponents to be integers
|
||||
// NOTE: as written this allows for the exponent to be a function, not
|
||||
// sure if that should change or not
|
||||
if (powInStack && op !== "u") {
|
||||
throw new Error("exponent must be an integer, not an expression");
|
||||
}
|
||||
|
||||
return powInStack;
|
||||
}
|
||||
|
||||
function popBetweenParens(opstack, queue, powInStack) {
|
||||
while (opstack.at(-1) !== "(") {
|
||||
if (opstack.length === 0) {
|
||||
throw new Error("mismatched parentheses");
|
||||
}
|
||||
|
||||
const op = opstack.pop();
|
||||
if (op === "^") {
|
||||
powInStack = false;
|
||||
}
|
||||
queue.push(op);
|
||||
}
|
||||
|
||||
opstack.pop();
|
||||
const t = opstack.at(-1);
|
||||
if (t === "sqrt" || t === "ord") {
|
||||
const func = opstack.pop();
|
||||
queue.push(func);
|
||||
}
|
||||
return powInStack;
|
||||
}
|
||||
|
||||
function empty(opstack, queue) {
|
||||
while (opstack.length !== 0) {
|
||||
const op = opstack.pop();
|
||||
if (op === "(") {
|
||||
throw new Error("mismatched parentheses");
|
||||
}
|
||||
|
||||
queue.push(op);
|
||||
}
|
||||
}
|
||||
|
||||
function isUnaryMinus(token, lastToken) {
|
||||
return token === "-" && (lastToken === null || /[(-+*/^]/.test(lastToken));
|
||||
}
|
||||
|
||||
function shunt(tokens) {
|
||||
const queue = [];
|
||||
const opstack = [];
|
||||
let lastToken = null;
|
||||
let powInStack = false;
|
||||
for (let token of tokens) {
|
||||
if (typeof token === "bigint") {
|
||||
queue.push(token);
|
||||
} else if (/[-+*/^]/.test(token)) {
|
||||
if (isUnaryMinus(token, lastToken)) {
|
||||
token = "u";
|
||||
}
|
||||
|
||||
powInStack = popOps(opstack, queue, token, powInStack);
|
||||
|
||||
opstack.push(token);
|
||||
if (token === "^") {
|
||||
powInStack = true;
|
||||
}
|
||||
} else if (token === "(") {
|
||||
opstack.push(token);
|
||||
} else if (token === ")") {
|
||||
powInStack = popBetweenParens(opstack, queue, powInStack);
|
||||
} else if (token === "sqrt") {
|
||||
opstack.push(token);
|
||||
} else if (token === "ord") {
|
||||
opstack.push(token);
|
||||
}
|
||||
|
||||
lastToken = token;
|
||||
}
|
||||
|
||||
empty(opstack, queue);
|
||||
return queue;
|
||||
}
|
||||
Reference in New Issue
Block a user