handle unary minus

This commit is contained in:
filifa 2025-12-11 23:49:34 -05:00
parent 42d2e94fe6
commit 25189661c7
3 changed files with 26 additions and 7 deletions

View File

@ -93,6 +93,14 @@ function compute(queue, modulus) {
const binv = modinv(b, modulus);
const c = (a * binv) % modulus;
stack.push(c);
} else if (token === "u") {
let a = stack.pop();
if (a === undefined) {
throw new Error("invalid expression");
}
a *= -1n;
stack.push(a);
} else if (token === "^") {
const [a, b] = binaryOpPop(stack);
const c = modpow(a, b, modulus);

View File

@ -1,8 +1,7 @@
function tokenize(expr) {
// NOTE: not handling whitespace
// NOTE: currently ends early if string doesn't match token
// FIXME: need to handle unary minus (e.g. on parentheses)
const regexp = /-?[0-9]+|[-+*/^]|\(|\)/gy;
const regexp = /[0-9]+|[-+*/^]|\(|\)/gy;
const matches = expr.matchAll(regexp);
const tokens = [];

View File

@ -3,7 +3,7 @@ function isLeftAssociative(op) {
}
function popOps(opstack, queue, op, powInStack) {
const prec = {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3}
const prec = {"+": 1, "-": 1, "*": 2, "/": 2, "u": 3, "^": 3}
while (opstack.length > 0) {
const op2 = opstack.at(-1);
if (op2 === "(") {
@ -27,9 +27,11 @@ function popOps(opstack, queue, op, powInStack) {
// 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
if (powInStack) {
if (powInStack && op !== "u") {
throw new Error("exponent must be an integer, not an expression");
}
return powInStack;
}
function popBetweenParens(opstack, queue, powInStack) {
@ -60,16 +62,24 @@ function empty(opstack, queue) {
}
}
function isUnaryMinus(token, lastToken) {
return token === "-" && (lastToken === null || /[(-+*/^]/.test(lastToken));
}
function shunt(tokens) {
const queue = [];
const opstack = [];
let lastToken = null;
let powInStack = false;
for (const token of tokens) {
for (let token of tokens) {
if (typeof token === "bigint") {
queue.push(token);
} else if (/[-+*/^]/.test(token)) {
popOps(opstack, queue, token, powInStack);
powInStack = false;
if (isUnaryMinus(token, lastToken)) {
token = "u";
}
powInStack = popOps(opstack, queue, token, powInStack);
opstack.push(token);
if (token === "^") {
@ -80,6 +90,8 @@ function shunt(tokens) {
} else if (token === ")") {
powInStack = popBetweenParens(opstack, queue, powInStack);
}
lastToken = token;
}
empty(opstack, queue);