Skip to content

fix: add unary minus and correct subtraction parsing (GH #315)#433

Open
toddr-bot wants to merge 1 commit into
mainfrom
koan.toddr.bot/fix-unary-minus-parsing
Open

fix: add unary minus and correct subtraction parsing (GH #315)#433
toddr-bot wants to merge 1 commit into
mainfrom
koan.toddr.bot/fix-unary-minus-parsing

Conversation

@toddr-bot
Copy link
Copy Markdown
Contributor

What

Subtraction expressions now work correctly in MACRO arguments and all other contexts.

Why

The tokenizer's number regex (-?\d+) greedily consumed - as part of a negative number literal even after identifiers. This caused show(x-2) to tokenize as IDENT:x NUMBER:-2 (two separate args) instead of IDENT:x MINUS NUMBER:2 (a subtraction expression). The macro silently received only x, producing wrong results with no error.

Fixes #315.

How

Three coordinated changes:

  • Tokenizer (Parser.pm): Removed -? from the number regex so - is always emitted as a separate operator token
  • Grammar (Parser.yp + Grammar.pm.skel): Promoted - from BINOP to its own token (like + and /), added expr '-' expr for binary subtraction and '-' expr %prec UMINUS for unary negation. Widened range rule from sterm TO sterm to expr TO expr so [-2..2] continues to work
  • Recompiled Grammar.pm from the updated grammar

Adds 4 S/R conflicts (total 5 S/R + 1 R/R), all correctly resolved by default shift preference — binary minus binds in expression context.

Testing

  • All 3192 existing tests pass
  • Added t/zz-unary-minus-gh-315.t with 25 tests covering: MACRO subtraction args, unary minus, assignment with negatives, precedence, double negation, negative ranges, negative floats, mixed unary/binary minus

@toddr-bot
Copy link
Copy Markdown
Contributor Author

Recreated from #387 (auto-closed when the toddr-bot fork was removed). Original branch is now hosted on this repo.

@toddr
Copy link
Copy Markdown
Member

toddr commented May 24, 2026

@toddr-bot rebase

The tokenizer's number regex (-?\d+) greedily consumed '-' as part of a
negative number literal even when it followed an identifier, causing
show(x-2) to silently pass only x to the macro.

Three coordinated changes:
- Tokenizer: remove -? from number regex so '-' is always a separate token
- Grammar: promote '-' from BINOP to its own token, add unary minus rule
  with UMINUS precedence, and widen range rule to accept expressions
- Recompile Grammar.pm from updated Parser.yp

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@toddr-bot
Copy link
Copy Markdown
Contributor Author

Rebase with requested adjustments

Branch koan.toddr.bot/fix-unary-minus-parsing was rebased onto main and review feedback was applied.

Stats

5 files changed, 4441 insertions(+), 4135 deletions(-)
Actions performed
  • Already-solved check: negative (confidence=high, reasoning=No commit in recent main history addresses unary minus parsing, subtraction tokenization, or GH Silent parsing failure when passing expressions to a MACRO #315)
  • Resolved merge conflicts (1 round(s))
  • Rebased koan.toddr.bot/fix-unary-minus-parsing onto origin/main
  • Pre-push CI check: previous run passed
  • Force-pushed koan.toddr.bot/fix-unary-minus-parsing to origin
  • CI check enqueued in ## CI (async)

CI status

CI will be checked asynchronously.


Automated by Kōan

@toddr-bot toddr-bot force-pushed the koan.toddr.bot/fix-unary-minus-parsing branch from 258b275 to e8934b7 Compare May 24, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Silent parsing failure when passing expressions to a MACRO

2 participants