do not allow expressions in exponent
This commit is contained in:
parent
13bec9f40b
commit
20c793b31b
|
|
@ -94,11 +94,6 @@ function compute(queue, modulus) {
|
||||||
const c = (a * binv) % modulus;
|
const c = (a * binv) % modulus;
|
||||||
stack.push(c);
|
stack.push(c);
|
||||||
} else if (token === "^") {
|
} else if (token === "^") {
|
||||||
// FIXME: not always valid - consider 2^3^4 mod 7
|
|
||||||
// also 2^(3+4)
|
|
||||||
// solution might be to change parser
|
|
||||||
// might also be better to just mod once at the end
|
|
||||||
// instead of with every op
|
|
||||||
const [a, b] = binaryOpPop(stack);
|
const [a, b] = binaryOpPop(stack);
|
||||||
const c = modpow(a, b, modulus);
|
const c = modpow(a, b, modulus);
|
||||||
stack.push(c);
|
stack.push(c);
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ function isLeftAssociative(op) {
|
||||||
return op === "+" || op === "-" || op === "*" || op === "/";
|
return op === "+" || op === "-" || op === "*" || op === "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
function popOps(opstack, queue, op) {
|
function popOps(opstack, queue, op, powInStack) {
|
||||||
const prec = {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3}
|
const prec = {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3}
|
||||||
while (opstack.length > 0) {
|
while (opstack.length > 0) {
|
||||||
const op2 = opstack.at(-1);
|
const op2 = opstack.at(-1);
|
||||||
|
|
@ -13,25 +13,40 @@ function popOps(opstack, queue, op) {
|
||||||
if ((prec[op2] > prec[op]) || (prec[op2] === prec[op] && isLeftAssociative(op))) {
|
if ((prec[op2] > prec[op]) || (prec[op2] === prec[op] && isLeftAssociative(op))) {
|
||||||
opstack.pop();
|
opstack.pop();
|
||||||
queue.push(op2);
|
queue.push(op2);
|
||||||
|
if (op2 === "^") {
|
||||||
|
powInStack = false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
opstack.push(op);
|
// practically, we want 2^(3+4) mod 7 to evaluate to 2^7 mod 7 = 2
|
||||||
|
// however, since our operators are all modular, this would instead
|
||||||
|
// evaluate as 2^0 mod 7 = 1
|
||||||
|
// 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) {
|
||||||
|
throw new Error("exponent must be an integer, not an expression");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function popBetweenParens(opstack, queue) {
|
function popBetweenParens(opstack, queue, powInStack) {
|
||||||
while (opstack.at(-1) !== "(") {
|
while (opstack.at(-1) !== "(") {
|
||||||
if (opstack.length === 0) {
|
if (opstack.length === 0) {
|
||||||
throw new Error("mismatched parentheses");
|
throw new Error("mismatched parentheses");
|
||||||
}
|
}
|
||||||
|
|
||||||
const op = opstack.pop();
|
const op = opstack.pop();
|
||||||
|
if (op === "^") {
|
||||||
|
powInStack = false;
|
||||||
|
}
|
||||||
queue.push(op);
|
queue.push(op);
|
||||||
}
|
}
|
||||||
|
|
||||||
opstack.pop();
|
opstack.pop();
|
||||||
|
return powInStack;
|
||||||
}
|
}
|
||||||
|
|
||||||
function empty(opstack, queue) {
|
function empty(opstack, queue) {
|
||||||
|
|
@ -48,15 +63,22 @@ function empty(opstack, queue) {
|
||||||
function shunt(tokens) {
|
function shunt(tokens) {
|
||||||
const queue = [];
|
const queue = [];
|
||||||
const opstack = [];
|
const opstack = [];
|
||||||
|
let powInStack = false;
|
||||||
for (const token of tokens) {
|
for (const token of tokens) {
|
||||||
if (typeof token === "bigint") {
|
if (typeof token === "bigint") {
|
||||||
queue.push(token);
|
queue.push(token);
|
||||||
} else if (/[-+*/^]/.test(token)) {
|
} else if (/[-+*/^]/.test(token)) {
|
||||||
popOps(opstack, queue, token);
|
popOps(opstack, queue, token, powInStack);
|
||||||
|
powInStack = false;
|
||||||
|
|
||||||
|
opstack.push(token);
|
||||||
|
if (token === "^") {
|
||||||
|
powInStack = true;
|
||||||
|
}
|
||||||
} else if (token === "(") {
|
} else if (token === "(") {
|
||||||
opstack.push(token);
|
opstack.push(token);
|
||||||
} else if (token === ")") {
|
} else if (token === ")") {
|
||||||
popBetweenParens(opstack, queue);
|
powInStack = popBetweenParens(opstack, queue, powInStack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue