the x was for indeX retard. wip code here

This commit is contained in:
Ean Milligan 2025-07-06 03:50:03 -04:00
parent abe49d49c2
commit 69e9d6ca1d
3 changed files with 29 additions and 12 deletions

View File

@ -11,9 +11,9 @@ import { loopCountCheck } from 'artigen/managers/loopManager.ts';
import { tokenizeMath } from 'artigen/math/mathTokenizer.ts';
import { reduceCountDetails } from 'artigen/utils/counter.ts';
import { closeInternal, internalWrapRegex, openInternal } from 'artigen/utils/escape.ts';
import { closeInternal, cmdSplitRegex, internalWrapRegex, openInternal } from 'artigen/utils/escape.ts';
import { loggingEnabled } from 'artigen/utils/logFlag.ts';
import { getMatchingInternalId, getMatchingPostfixId } from 'artigen/utils/parenBalance.ts';
import { assertGroupBalance, getMatchingGroupIdx, getMatchingInternalIdx, getMatchingPostfixIdx } from 'artigen/utils/parenBalance.ts';
import { basicReducer } from 'artigen/utils/reducers.ts';
// tokenizeCmd expects a string[] of items that are either config.prefix/config.postfix or some text that contains math and/or dice rolls
@ -34,7 +34,7 @@ export const tokenizeCmd = (
loopCountCheck();
const openIdx = cmd.indexOf(config.prefix);
const closeIdx = getMatchingPostfixId(cmd, openIdx);
const closeIdx = getMatchingPostfixIdx(cmd, openIdx);
const currentCmd = cmd.slice(openIdx + 1, closeIdx);
@ -131,10 +131,27 @@ export const tokenizeCmd = (
}
return [returnData, countDetails, rollDists];
} else {
loggingEnabled && log(LT.LOG, `Tokenizing math ${JSON.stringify(cmd)}`);
// Check for any groups and handle them?
const groupParts = cmd
.join('')
.split(/([{,}])/g)
.filter((x) => x);
if (groupParts.includes('{')) {
assertGroupBalance(groupParts);
}
while (groupParts.includes('{')) {
loggingEnabled && log(LT.LOG, `Handling Groups | Current cmd: ${JSON.stringify(groupParts)}`);
const openIdx = groupParts.indexOf('}');
const closeIdx = getMatchingGroupIdx;
const temp = cmd.join('').replaceAll('{', '').replaceAll('}', '').replaceAll(',', '');
cmd = temp.split(cmdSplitRegex);
}
const cmdForMath = groupParts.join('');
loggingEnabled && log(LT.LOG, `Tokenizing math ${cmdForMath}`);
// Solve the math and rolls for this cmd
const [tempData, tempCounts, tempDists] = tokenizeMath(cmd.join(''), modifiers, previousResults);
const [tempData, tempCounts, tempDists] = tokenizeMath(cmdForMath, modifiers, previousResults);
const data = tempData[0];
loggingEnabled &&
log(LT.LOG, `Solved math is back ${JSON.stringify(data)} | ${JSON.stringify(returnData)} ${JSON.stringify(tempCounts)} ${JSON.stringify(tempDists)}`);
@ -150,7 +167,7 @@ export const tokenizeCmd = (
loopCountCheck();
const openIdx = initConf.indexOf(openInternal);
const closeIdx = getMatchingInternalId(initConf, openIdx);
const closeIdx = getMatchingInternalIdx(initConf, openIdx);
// Take first returnData out of array
const dataToMerge = returnData.shift();

View File

@ -11,7 +11,7 @@ import { loopCountCheck } from 'artigen/managers/loopManager.ts';
import { legalMath, legalMathOperators } from 'artigen/utils/legalMath.ts';
import { loggingEnabled } from 'artigen/utils/logFlag.ts';
import { getMatchingParenId } from 'artigen/utils/parenBalance.ts';
import { getMatchingParenIdx } from 'artigen/utils/parenBalance.ts';
// mathSolver(conf, wrapDetails) returns one condensed SolvedStep
// mathSolver is a function that recursively solves the full roll and math
@ -39,7 +39,7 @@ export const mathSolver = (conf: MathConf[], wrapDetails = false): SolvedStep =>
loggingEnabled && log(LT.LOG, `Evaluating roll ${JSON.stringify(conf)} | Looking for (`);
// Get first open parenthesis
let openParenIdx = conf.indexOf('(');
const closeParenIdx = getMatchingParenId(conf, openParenIdx);
const closeParenIdx = getMatchingParenIdx(conf, openParenIdx);
// Call the solver on the items between openParenIdx and closeParenIdx (excluding the parens)
const parenSolve = mathSolver(conf.slice(openParenIdx + 1, closeParenIdx), true);

View File

@ -58,7 +58,7 @@ export const assertParenBalance = (conf: MathConf[]) => checkBalance(conf, '(',
export const assertPrePostBalance = (conf: MathConf[]) => checkBalance(conf, config.prefix, config.postfix, 'PrefixPostfix', false, 0);
// getMatchingXIdx gets the matching X, also partially verifies the conf has balanced X
export const getMatchingGroupId = (conf: MathConf[], openIdx: number): number => checkBalance(conf, '{', '}', 'Group', true, openIdx);
export const getMatchingInternalId = (conf: MathConf[], openIdx: number): number => checkBalance(conf, openInternal, closeInternal, 'Internal', true, openIdx);
export const getMatchingParenId = (conf: MathConf[], openIdx: number): number => checkBalance(conf, '(', ')', 'Paren', true, openIdx);
export const getMatchingPostfixId = (conf: MathConf[], openIdx: number): number => checkBalance(conf, config.prefix, config.postfix, 'PrefixPostfix', true, openIdx);
export const getMatchingGroupIdx = (conf: MathConf[], openIdx: number): number => checkBalance(conf, '{', '}', 'Group', true, openIdx);
export const getMatchingInternalIdx = (conf: MathConf[], openIdx: number): number => checkBalance(conf, openInternal, closeInternal, 'Internal', true, openIdx);
export const getMatchingParenIdx = (conf: MathConf[], openIdx: number): number => checkBalance(conf, '(', ')', 'Paren', true, openIdx);
export const getMatchingPostfixIdx = (conf: MathConf[], openIdx: number): number => checkBalance(conf, config.prefix, config.postfix, 'PrefixPostfix', true, openIdx);