From 504df7c80f4d179a4372fabeb59b10106d3ec112 Mon Sep 17 00:00:00 2001 From: Ean Milligan Date: Sat, 3 May 2025 21:41:23 -0400 Subject: [PATCH] fix (5)-5 so it does +-5 instead of *-5 --- src/artigen/math/mathSolver.ts | 1 + src/artigen/math/mathTokenizer.ts | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/artigen/math/mathSolver.ts b/src/artigen/math/mathSolver.ts index 17a090a..7b20e77 100644 --- a/src/artigen/math/mathSolver.ts +++ b/src/artigen/math/mathSolver.ts @@ -72,6 +72,7 @@ export const mathSolver = (conf: MathConf[], wrapDetails = false): SolvedStep => // Look for any implicit multiplication that may have been missed // Start at index 1 as there will never be implicit multiplication before the first element + loggingEnabled && log(LT.LOG, `Checking for missing implicit multiplication ${JSON.stringify(conf)}`); for (let i = 1; i < conf.length; i++) { loopCountCheck(); diff --git a/src/artigen/math/mathTokenizer.ts b/src/artigen/math/mathTokenizer.ts index ee189b2..e501b3e 100644 --- a/src/artigen/math/mathTokenizer.ts +++ b/src/artigen/math/mathTokenizer.ts @@ -16,7 +16,9 @@ import { legalMathOperators } from 'artigen/utils/legalMath.ts'; import { loggingEnabled } from 'artigen/utils/logFlag.ts'; import { assertParenBalance } from 'artigen/utils/parenBalance.ts'; -const operators = ['(', ')', '^', '*', '/', '%', '+', '-']; +// minusOps are operators that will cause a negative sign to collapse into a number (in cases like + - 1) +const minusOps = ['(', '^', '*', '/', '%', '+', '-']; +const allOps = [...minusOps, ')']; export const tokenizeMath = (cmd: string, modifiers: RollModifiers, previousResults: number[]): [ReturnData[], CountDetails[]] => { const countDetails: CountDetails[] = []; @@ -132,7 +134,7 @@ export const tokenizeMath = (cmd: string, modifiers: RollModifiers, previousResu } else { throw new Error(`IllegalVariable_${curMathConfStr}`); } - } else if (![...operators, ...legalMathOperators].includes(curMathConfStr)) { + } else if (![...allOps, ...legalMathOperators].includes(curMathConfStr)) { // If nothing else has handled it by now, try it as a roll const formattedRoll = generateFormattedRoll(curMathConfStr, modifiers); mathConf[i] = formattedRoll.solvedStep; @@ -140,7 +142,7 @@ export const tokenizeMath = (cmd: string, modifiers: RollModifiers, previousResu } // Identify if we are in a state where the current number is a negative number - if (mathConf[i - 1] === '-' && ((!mathConf[i - 2] && mathConf[i - 2] !== 0) || operators.includes( mathConf[i - 2]))) { + if (mathConf[i - 1] === '-' && ((!mathConf[i - 2] && mathConf[i - 2] !== 0) || minusOps.includes( mathConf[i - 2]))) { if (typeof mathConf[i] === 'string') { // Current item is a mathOp, need to insert a "-1 *" before it mathConf.splice(i - 1, 1, ...[parseFloat('-1'), '*']);