control the whole evaluation process with a worker
This commit is contained in:
parent
7e7b805e3e
commit
409225e81b
27
main.js
27
main.js
|
|
@ -14,38 +14,15 @@ GNU General Public License for more details.
|
||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
import { tokenize } from "./modules/lexer.js";
|
|
||||||
import { shunt } from "./modules/parser.js";
|
|
||||||
|
|
||||||
function evaluate() {
|
function evaluate() {
|
||||||
const expr = document.querySelector("#expr");
|
const expr = document.querySelector("#expr");
|
||||||
const modulus = document.querySelector("#modulus");
|
const modulus = document.querySelector("#modulus");
|
||||||
const result = document.querySelector("#result");
|
const result = document.querySelector("#result");
|
||||||
|
|
||||||
const m = BigInt(modulus.value);
|
worker.postMessage({command: "compute", expr: expr.value, modulus: modulus.value});
|
||||||
if (m <= 0n) {
|
|
||||||
const e = Error("invalid modulus")
|
|
||||||
result.value = e;
|
|
||||||
console.log(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const tokens = tokenize(expr.value);
|
|
||||||
console.log(tokens);
|
|
||||||
let queue = [];
|
|
||||||
try {
|
|
||||||
queue = shunt(tokens);
|
|
||||||
} catch(e) {
|
|
||||||
result.value = e;
|
|
||||||
console.log(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(queue);
|
|
||||||
|
|
||||||
worker.postMessage({command: "compute", queue, m});
|
|
||||||
timeoutId = setTimeout(() => {
|
timeoutId = setTimeout(() => {
|
||||||
document.querySelector("#result").value = "calculating...";
|
result.value = "calculating...";
|
||||||
switchInputs(false);
|
switchInputs(false);
|
||||||
switchButtons(false);
|
switchButtons(false);
|
||||||
}, 500);
|
}, 500);
|
||||||
|
|
|
||||||
|
|
@ -14,15 +14,30 @@ GNU General Public License for more details.
|
||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
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) => {
|
addEventListener("message", (message) => {
|
||||||
if (message.data.command === "compute") {
|
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);
|
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) {
|
function binaryOpPop(stack) {
|
||||||
const b = stack.pop();
|
const b = stack.pop();
|
||||||
const a = stack.pop();
|
const a = stack.pop();
|
||||||
|
|
|
||||||
|
|
@ -43,5 +43,3 @@ function tokenize(expr) {
|
||||||
|
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { tokenize };
|
|
||||||
|
|
@ -124,5 +124,3 @@ function shunt(tokens) {
|
||||||
empty(opstack, queue);
|
empty(opstack, queue);
|
||||||
return queue;
|
return queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { shunt };
|
|
||||||
Loading…
Reference in New Issue