mcalc/main.js

154 lines
4.9 KiB
JavaScript
Raw Normal View History

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
import { tokenize } from "./modules/lexer.js";
import { shunt } from "./modules/parser.js";
2025-12-12 04:49:34 +00:00
2025-12-12 04:49:34 +00:00
function evaluate() {
2025-12-12 04:49:34 +00:00
const expr = document.querySelector("#expr");
const modulus = document.querySelector("#modulus");
2025-12-12 04:49:34 +00:00
const result = document.querySelector("#result");
2025-12-12 04:49:34 +00:00
const m = BigInt(modulus.value);
2025-12-12 04:49:34 +00:00
if (m <= 0n) {
2025-12-12 04:49:34 +00:00
const e = Error("invalid modulus")
2025-12-12 04:49:34 +00:00
result.value = e;
console.log(e);
return;
}
2025-12-12 04:49:34 +00:00
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);
2025-12-12 04:49:34 +00:00
worker.postMessage({command: "compute", queue, m});
result.value = "calculating...";
2025-12-12 04:49:34 +00:00
}
const expr = document.querySelector("#expr");
2025-12-12 04:49:34 +00:00
let selectionStart = 0;
let selectionEnd = 0;
2025-12-12 04:49:34 +00:00
function setupExprInput() {
selectionStart = expr.selectionStart;
selectionEnd = expr.selectionEnd;
switchButtons(true);
2025-12-12 04:49:34 +00:00
}
function clear() {
expr.value = "";
expr.focus();
2025-12-12 04:49:34 +00:00
}
2025-12-12 04:49:34 +00:00
2025-12-12 04:49:34 +00:00
function keyPress(c) {
expr.focus();
const begin = expr.value.slice(0, selectionStart);
const end = expr.value.slice(selectionEnd);
expr.value = begin + c + end;
expr.setSelectionRange(selectionStart+c.length, selectionStart+c.length);
2025-12-12 04:49:34 +00:00
}
2025-12-12 04:49:34 +00:00
function backspace() {
expr.focus();
2025-12-12 04:49:34 +00:00
if (selectionEnd === 0) {
return;
}
let begin = null;
if (selectionStart === selectionEnd) {
begin = expr.value.slice(0, selectionStart-1);
2025-12-12 04:49:34 +00:00
} else {
begin = expr.value.slice(0, selectionStart);
2025-12-12 04:49:34 +00:00
}
const end = expr.value.slice(selectionEnd);
2025-12-12 04:49:34 +00:00
expr.value = begin + end;
2025-12-12 04:49:34 +00:00
if (selectionStart === 0) {
expr.setSelectionRange(0, 0);
2025-12-12 04:49:34 +00:00
} else if (selectionStart === selectionEnd) {
expr.setSelectionRange(selectionStart-1, selectionStart-1);
2025-12-12 04:49:34 +00:00
} else {
expr.setSelectionRange(selectionStart, selectionStart);
2025-12-12 04:49:34 +00:00
}
}
2025-12-12 04:49:34 +00:00
function evalOnEnter(e) {
if (e.key === "Enter") {
evaluate();
}
}
function switchButtons(enabled) {
for (const btn of document.querySelectorAll("#main-buttons button")) {
btn.disabled = !enabled;
}
for (const btn of document.querySelectorAll("#extra-buttons button")) {
btn.disabled = !enabled;
}
}
2025-12-12 04:49:34 +00:00
const worker = new Worker("./workers/compute.js");
2025-12-12 04:49:34 +00:00
worker.addEventListener("message", (message) => {
document.querySelector("#result").value = message.data;
});
document.querySelector("#expr").addEventListener("focus", setupExprInput);
2025-12-12 04:49:34 +00:00
document.querySelector("#expr").addEventListener("keyup", (e) => evalOnEnter(e));
document.querySelector("#modulus").addEventListener("focus", () => switchButtons(false));
2025-12-12 04:49:34 +00:00
document.querySelector("#modulus").addEventListener("keyup", (e) => evalOnEnter(e));
2025-12-12 04:49:34 +00:00
document.querySelector("#zero").addEventListener("click", () => keyPress("0"));
document.querySelector("#one").addEventListener("click", () => keyPress("1"));
document.querySelector("#two").addEventListener("click", () => keyPress("2"));
document.querySelector("#three").addEventListener("click", () => keyPress("3"));
document.querySelector("#four").addEventListener("click", () => keyPress("4"));
document.querySelector("#five").addEventListener("click", () => keyPress("5"));
document.querySelector("#six").addEventListener("click", () => keyPress("6"));
document.querySelector("#seven").addEventListener("click", () => keyPress("7"));
document.querySelector("#eight").addEventListener("click", () => keyPress("8"));
document.querySelector("#nine").addEventListener("click", () => keyPress("9"));
document.querySelector("#plus").addEventListener("click", () => keyPress("+"));
document.querySelector("#minus").addEventListener("click", () => keyPress("-"));
document.querySelector("#times").addEventListener("click", () => keyPress("*"));
document.querySelector("#divide").addEventListener("click", () => keyPress("/"));
document.querySelector("#lparen").addEventListener("click", () => keyPress("("));
document.querySelector("#rparen").addEventListener("click", () => keyPress(")"));
document.querySelector("#pow").addEventListener("click", () => keyPress("^"));
2025-12-12 04:49:34 +00:00
document.querySelector("#inv").addEventListener("click", () => keyPress("^-1"));
2025-12-12 04:49:34 +00:00
document.querySelector("#sqrt").addEventListener("click", () => keyPress("sqrt("));
2025-12-12 04:49:34 +00:00
document.querySelector("#ord").addEventListener("click", () => keyPress("ord("));
2025-12-12 04:49:34 +00:00
document.querySelector("#clear").addEventListener("click", clear);
2025-12-12 04:49:34 +00:00
document.querySelector("#enter").addEventListener("click", evaluate);
2025-12-12 04:49:34 +00:00
2025-12-12 04:49:34 +00:00
document.querySelector("#backspace").addEventListener("click", backspace);