Fix operator precedence/associativity and support repeated percent postfixes#2
Fix operator precedence/associativity and support repeated percent postfixes#2ChasLui wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: dc7077722b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| private utils: Utils; | ||
| private binaryOperatorsPrecedence: string[][]; | ||
| private static readonly BINARY_OPERATOR_GROUPS: ReadonlyArray<BinaryOperatorGroup> = [ | ||
| { opSet: new Set(['^']), associativity: 'right' }, |
There was a problem hiding this comment.
Keep exponent chains left-associative
For formulas with two or more exponent operators, such as 2^3^2 or A1^B1^C1, marking ^ as right-associative makes the parser reduce the expression as 2^(3^2) and return 512. Excel evaluates operators of the same precedence from left to right, so caret chains should be reduced as (2^3)^2 and return 64; this breaks Excel-compatible results and the new tests lock in the wrong behavior.
Useful? React with 👍 / 👎.
Motivation
^right-associative) and ensure postfix%can be applied repeatedly.Description
OperatorAssociativityandBinaryOperatorGroupand replaced the previous precedence array withBINARY_OPERATOR_GROUPSthat include associativity metadata.formulaWithBinaryOpwithreduceBinaryOperatorsandapplyInfixAthelpers to correctly handle left- and right-associative operator groups (right-associative^reduced from the end).OPTIONtoMANYinformulaWithPercentOpso multiple%postfixes (e.g.100%%) are parsed and applied sequentially.@types/nodetodevDependenciesand updatedpnpm-lock.yamlaccordingly.test/operators/testcase.tsto cover chained exponentiation, mixed precedence/concatenation cases, various percent combinations, and chaining comparison behavior.Testing
vitest, including thetest/operatorscases; all tests passed.test/operators/testcase.tssucceed under the updated parser implementation.Codex Task