Skip to content

Fix ++ and -- on fields, texts and array elements, and the line of runtime errors - #673

Merged
bertysentry merged 4 commits into
mainfrom
feature/issues-667-670-inc-dec-line-numbers
Nov 27, 2026
Merged

bertysentry merged 4 commits into
mainfrom
feature/issues-667-670-inc-dec-line-numbers

Conversation

@bertysentry

Copy link
Copy Markdown
Contributor

Closes #667, closes #668, closes #669, closes #670.

What changes

++ and -- (#667, #668, #669)

PreIncAst, PreDecAst, PostIncAst and PostDecAst are replaced by one IncDecAst (in AwkParser), which evaluates the lvalue once in every form: a prefix form evaluates to the new value, a postfix form to the numeric value before the change.

  • ++$n and --$n evaluate to another field instead of the updated value #667: ++$n and --$n emit index, DUP, INC_DOLLAR_REF, POP, GET_INPUT_FIELD. INC_DOLLAR_REF pushes the old value, so the field read was $(old value) and the duplicated index leaked on the operand stack.
  • x++ and x-- on a variable that holds a text evaluate to the text, not to its number #668: AVM.inc() and dec() return numericValueOf() of the old value. The postfix forms on a special variable (NR, FS, OFS, CONVFMT...) convert it with UNARY_PLUS before the DUP, which had the same bug (FS = "3x"; y = FS++ gave 3x).
  • ++ and -- on an array element evaluate the subscript twice #669: INC_MAP_REF and DEC_MAP_REF push the new value, and two opcodes POSTINC_MAP_REF and POSTDEC_MAP_REF (replacing POSTFIX_OPERAND, which was the last one of Opcode, so no ordinal moves) push the old numeric value. The array and its subscript are evaluated once. The four cases share one helper, AVM.incDecMapElement(). AwkTuples.serialVersionUID goes from 10 to 11.
  • Found on the way, not in the issues: $n-- evaluated $n and then its index again, so it left the old field value on the operand stack and evaluated the index twice. print "A", $1--, "B" printed 8 8 B, $(i++)-- incremented i twice, and $2-- in a long loop ran out of memory. The new code emits index, DEC_DOLLAR_REF like $n++.
  • The Opcode Javadoc of INC, DEC, INC_ARRAY_REF, DEC_ARRAY_REF, INC_DOLLAR_REF, DEC_DOLLAR_REF, POSTINC and POSTDEC now states what they actually push. The wrong "pushes x+1" on INC_DOLLAR_REF is how ++$n and --$n evaluate to another field instead of the updated value #667 came about.

Line numbers (#670)

  • The lexer records the line of each token (tokenLine), and currentSourceLineNumber() returns the line of the last consumed token (lastTokenLine) instead of the reader's position, which is one token ahead. A node built once its last token is consumed takes the line where it ends. The EOF token takes the last line of the source, so the LineNumberReader end-of-stream bump no longer matters (a one-line eval() reports 1, not 2).
  • Hand-made anchors removed: ArrayReferenceAst(int lineNo, ...), extensionCallLineNumber(), FunctionCallAst.warningLineNo().
  • Explicit anchors where gawk reports the start: a call (user, builtin, extension, indirect) and print/printf take the line of their name or keyword, if and while the line of their condition (the )), for the line where its condition ends. Before, these nodes were built after their body or arguments and reported a later line.
  • ParserException takes the line of the lookahead token instead of the 0-based reader line, which was one line early unless the offending token ended its line (y = = 2 on line 3 was reported on line 2).

All cases checked against gawk 5.0 give the same line, except one, accepted: a binary operator split with a backslash continuation (y = "abc" + \ then 1) reports the line where the operation ends, gawk the operator's line. Anchoring every operator node at its operator would fix it; it is pinned by a test.

Documentation

Four bullets under Unreleased in behavior-changes.md.

Tests

  • New IncrementDecrementTest (14 tests): prefix and postfix on numeric, text, empty and beyond-NF fields, in statements and expressions, $(i++)++ and $(i++)--, $n-- next to other operands; x++ on a text global, local and special variable; the script of ++ and -- on an array element evaluate the subscript twice #669, a[i][j], array parameters.
  • AwkTest: the l.awk of A runtime error of an operation that ends its line is reported on the next line #670, an operation before }, a one-line program without a final newline, a rule pattern followed by blank lines, if, while and a multi-line for header, multi-line sprintf(), printf, @f() and subscript, a second -f source and an @included file without a final newline (lines counted in their own source).
  • StrictNumbersTest: getLineNumber() of a one-line eval() (1), of the last operation of a line, before }, x++ ending a line, and two operations spanning two lines.
  • AwkParserTest: the line of a ParserException, and of a missing } followed by blank lines and a comment.
  • mvn verify site: BUILD SUCCESS, 1002 unit tests, 0 failure. Checkstyle, PMD, SpotBugs: 0.
  • gawk compatibility ITs (ignored by the build): the same 136 failing test names on main and on this branch.
  • Differential sweep against gawk 5.0: 2105 scripts (4 operators x 30 lvalue kinds x 8 contexts, subscripts with side effects). 903 differed on main. On this branch, every remaining difference is A number assigned to FS, OFS, SUBSEP and other special variables is converted with toString(), not CONVFMT #672 (a number assigned to FS, OFS, SUBSEP... prints 4.0, filed separately) or the documented exact 64-bit integers; no ++/-- value differs.

🤖 Generated with Claude Code

bertysentry and others added 3 commits November 27, 2026 14:08
One IncDecAst replaces PreIncAst, PreDecAst, PostIncAst and PostDecAst,
and evaluates the lvalue once in every form:

- ++$n and --$n drop the old value pushed by INC_DOLLAR_REF before
  reading the field, so they evaluate to the updated field (#667).
- inc() and dec() return the numeric value of the variable, and the
  postfix forms on special variables convert it with UNARY_PLUS, so
  x++ on a text evaluates to its number (#668).
- INC_MAP_REF and DEC_MAP_REF push the new value, and the new
  POSTINC_MAP_REF and POSTDEC_MAP_REF (replacing POSTFIX_OPERAND) push
  the old numeric value, so the array and its subscript are evaluated
  once (#669). $(expr)-- no longer evaluates expr twice either.

AwkTuples.serialVersionUID is bumped to 11.

Closes #667
Closes #668
Closes #669

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
INC_DOLLAR_REF and DEC_DOLLAR_REF push the old numeric value of the
field, not the new one (the misreading behind #667); INC, DEC,
INC_ARRAY_REF and DEC_ARRAY_REF push nothing; POSTINC and POSTDEC
replace the value on top of the stack. Test ++ and -- on an empty field
and $n-- next to other operands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The lexer records the line of each token, and currentSourceLineNumber()
returns the line of the last consumed token instead of the reader's
position, one token ahead: an operation that ends its line is no longer
reported on the next line, and a one-line eval() expression reports
line 1. The end of the source keeps its last line.

The hand-made anchors go (ArrayReferenceAst, extensionCallLineNumber(),
warningLineNo()). A call, print and printf take the line of their name
or keyword, an if or while the line of its condition, a for the line
where its condition ends, as gawk reports them. A ParserException
reports the line of the token where parsing stopped.

Closes #670

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T14:04:52.592479Z 6231e94 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@bertysentry

Copy link
Copy Markdown
Contributor Author

@codex please review

@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: 0dc33d717b

ℹ️ 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".

The EOF token kept the line of the last token when no token came after
switching to the next -f source or back from an @include, so a parse
error at the end of an empty last source named that source with a line
of the previous one. The token line restarts at 1 on each switch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bertysentry

Copy link
Copy Markdown
Contributor Author

@codex please review again

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 6231e94bef

ℹ️ 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".

@bertysentry
bertysentry merged commit 2ae3312 into main Nov 27, 2026
5 checks passed
@bertysentry
bertysentry deleted the feature/issues-667-670-inc-dec-line-numbers branch November 27, 2026 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant