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
|
|
|
import { compute } from "./modules/compute.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) {
|
|
|
|
|
const e = Error("modulus cannot be 0")
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
try {
|
2025-12-12 04:49:34 +00:00
|
|
|
result.value = compute(queue, m);
|
2025-12-12 04:49:34 +00:00
|
|
|
} catch(e) {
|
|
|
|
|
result.value = e;
|
|
|
|
|
console.log(e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-12-12 04:49:34 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
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() {
|
2025-12-12 04:49:34 +00:00
|
|
|
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) {
|
2025-12-12 04:49:34 +00:00
|
|
|
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() {
|
2025-12-12 04:49:34 +00:00
|
|
|
expr.focus();
|
2025-12-12 04:49:34 +00:00
|
|
|
if (selectionEnd === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let begin = null;
|
|
|
|
|
if (selectionStart === selectionEnd) {
|
2025-12-12 04:49:34 +00:00
|
|
|
begin = expr.value.slice(0, selectionStart-1);
|
2025-12-12 04:49:34 +00:00
|
|
|
} else {
|
2025-12-12 04:49:34 +00:00
|
|
|
begin = expr.value.slice(0, selectionStart);
|
2025-12-12 04:49:34 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-12 04:49:34 +00:00
|
|
|
const end = expr.value.slice(selectionEnd);
|
2025-12-12 04:49:34 +00:00
|
|
|
|
2025-12-12 04:49:34 +00:00
|
|
|
expr.value = begin + end;
|
2025-12-12 04:49:34 +00:00
|
|
|
|
|
|
|
|
if (selectionStart === 0) {
|
2025-12-12 04:49:34 +00:00
|
|
|
expr.setSelectionRange(0, 0);
|
2025-12-12 04:49:34 +00:00
|
|
|
} else if (selectionStart === selectionEnd) {
|
2025-12-12 04:49:34 +00:00
|
|
|
expr.setSelectionRange(selectionStart-1, selectionStart-1);
|
2025-12-12 04:49:34 +00:00
|
|
|
} else {
|
2025-12-12 04:49:34 +00:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 04:49:34 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.querySelector("#expr").addEventListener("focus", setupExprInput);
|
2025-12-12 04:49:34 +00:00
|
|
|
document.querySelector("#expr").addEventListener("keyup", (e) => evalOnEnter(e));
|
|
|
|
|
|
2025-12-12 04:49:34 +00:00
|
|
|
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("#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);
|