126 lines
4.0 KiB
JavaScript
126 lines
4.0 KiB
JavaScript
import { tokenize } from "./modules/lexer.js";
|
|
import { shunt } from "./modules/parser.js";
|
|
import { compute } from "./modules/compute.js";
|
|
|
|
function evaluate() {
|
|
const expr = document.querySelector("#expr");
|
|
const modulus = document.querySelector("#modulus");
|
|
const m = BigInt(modulus.value);
|
|
|
|
const tokens = tokenize(expr.value);
|
|
console.log(tokens);
|
|
|
|
const result = document.querySelector("#result");
|
|
let queue = [];
|
|
try {
|
|
queue = shunt(tokens);
|
|
} catch(e) {
|
|
result.value = e;
|
|
console.log(e);
|
|
return;
|
|
}
|
|
|
|
console.log(queue);
|
|
|
|
try {
|
|
result.value = compute(queue, m);
|
|
} catch(e) {
|
|
result.value = e;
|
|
console.log(e);
|
|
return;
|
|
}
|
|
}
|
|
|
|
let currentInput = document.querySelector("#expr");
|
|
let selectionStart = 0;
|
|
let selectionEnd = 0;
|
|
|
|
function setCurrentInput() {
|
|
currentInput = this;
|
|
selectionStart = this.selectionStart;
|
|
selectionEnd = this.selectionEnd;
|
|
}
|
|
|
|
function clear() {
|
|
currentInput.value = "";
|
|
currentInput.focus();
|
|
}
|
|
|
|
function keyPress(c) {
|
|
// FIXME not working with modulus
|
|
// errors in logs on each key press
|
|
// wonder if it's an issue with it being a number type
|
|
currentInput.focus();
|
|
const begin = currentInput.value.slice(0, selectionStart);
|
|
const end = currentInput.value.slice(selectionEnd);
|
|
currentInput.value = begin + c + end;
|
|
currentInput.setSelectionRange(selectionStart+c.length, selectionStart+c.length);
|
|
}
|
|
|
|
function backspace() {
|
|
currentInput.focus();
|
|
if (selectionEnd === 0) {
|
|
return;
|
|
}
|
|
|
|
let begin = null;
|
|
if (selectionStart === selectionEnd) {
|
|
begin = currentInput.value.slice(0, selectionStart-1);
|
|
} else {
|
|
begin = currentInput.value.slice(0, selectionStart);
|
|
}
|
|
|
|
const end = currentInput.value.slice(selectionEnd);
|
|
|
|
currentInput.value = begin + end;
|
|
|
|
if (selectionStart === 0) {
|
|
currentInput.setSelectionRange(0, 0);
|
|
} else if (selectionStart === selectionEnd) {
|
|
currentInput.setSelectionRange(selectionStart-1, selectionStart-1);
|
|
} else {
|
|
currentInput.setSelectionRange(selectionStart, selectionStart);
|
|
}
|
|
}
|
|
|
|
function evalOnEnter(e) {
|
|
if (e.key === "Enter") {
|
|
evaluate();
|
|
}
|
|
}
|
|
|
|
document.querySelector("#expr").addEventListener("focus", setCurrentInput);
|
|
document.querySelector("#expr").addEventListener("keyup", (e) => evalOnEnter(e));
|
|
|
|
document.querySelector("#modulus").addEventListener("focus", setCurrentInput);
|
|
document.querySelector("#modulus").addEventListener("keyup", (e) => evalOnEnter(e));
|
|
|
|
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("^"));
|
|
document.querySelector("#inv").addEventListener("click", () => keyPress("^-1"));
|
|
|
|
document.querySelector("#clear").addEventListener("click", clear);
|
|
document.querySelector("#enter").addEventListener("click", evaluate);
|
|
|
|
document.querySelector("#backspace").addEventListener("click", backspace);
|
|
|
|
// TODO implement square root key
|