Skip to content

Fix operator precedence/associativity and support repeated percent postfixes#2

Open
ChasLui wants to merge 1 commit into
masterfrom
codex/find-and-fix-an-important-bug-ofoxju
Open

Fix operator precedence/associativity and support repeated percent postfixes#2
ChasLui wants to merge 1 commit into
masterfrom
codex/find-and-fix-an-important-bug-ofoxju

Conversation

@ChasLui

@ChasLui ChasLui commented Jul 7, 2026

Copy link
Copy Markdown
Owner

Motivation

  • Correct operator reduction so groups have explicit associativity (notably making ^ right-associative) and ensure postfix % can be applied repeatedly.
  • Simplify and centralize binary operator precedence logic to make maintenance and future changes easier.
  • Add missing dev typing dependency for Node to align dev environment types.

Description

  • Introduced OperatorAssociativity and BinaryOperatorGroup and replaced the previous precedence array with BINARY_OPERATOR_GROUPS that include associativity metadata.
  • Replaced in-place precedence loop in formulaWithBinaryOp with reduceBinaryOperators and applyInfixAt helpers to correctly handle left- and right-associative operator groups (right-associative ^ reduced from the end).
  • Changed percent handling from OPTION to MANY in formulaWithPercentOp so multiple % postfixes (e.g. 100%%) are parsed and applied sequentially.
  • Added @types/node to devDependencies and updated pnpm-lock.yaml accordingly.
  • Expanded operator precedence tests in test/operators/testcase.ts to cover chained exponentiation, mixed precedence/concatenation cases, various percent combinations, and chaining comparison behavior.

Testing

  • Ran the unit test suite with vitest, including the test/operators cases; all tests passed.
  • Confirmed the new operator precedence and percent chaining tests in test/operators/testcase.ts succeed under the updated parser implementation.

Codex Task

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread grammar/parsing.ts
private utils: Utils;
private binaryOperatorsPrecedence: string[][];
private static readonly BINARY_OPERATOR_GROUPS: ReadonlyArray<BinaryOperatorGroup> = [
{ opSet: new Set(['^']), associativity: 'right' },

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant