Repository navigation
Fix ++ and -- on fields, texts and array elements, and the line of runtime errors - #673
Conversation
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>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex please review |
There was a problem hiding this comment.
💡 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>
|
@codex please review again |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Closes #667, closes #668, closes #669, closes #670.
What changes
++and--(#667, #668, #669)PreIncAst,PreDecAst,PostIncAstandPostDecAstare replaced by oneIncDecAst(inAwkParser), 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.++$nand--$nevaluate to another field instead of the updated value #667:++$nand--$nemitindex, DUP, INC_DOLLAR_REF, POP, GET_INPUT_FIELD.INC_DOLLAR_REFpushes the old value, so the field read was$(old value)and the duplicated index leaked on the operand stack.x++andx--on a variable that holds a text evaluate to the text, not to its number #668:AVM.inc()anddec()returnnumericValueOf()of the old value. The postfix forms on a special variable (NR,FS,OFS,CONVFMT...) convert it withUNARY_PLUSbefore theDUP, which had the same bug (FS = "3x"; y = FS++gave3x).++and--on an array element evaluate the subscript twice #669:INC_MAP_REFandDEC_MAP_REFpush the new value, and two opcodesPOSTINC_MAP_REFandPOSTDEC_MAP_REF(replacingPOSTFIX_OPERAND, which was the last one ofOpcode, 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.serialVersionUIDgoes from 10 to 11.$n--evaluated$nand then its index again, so it left the old field value on the operand stack and evaluated the index twice.print "A", $1--, "B"printed8 8 B,$(i++)--incrementeditwice, and$2--in a long loop ran out of memory. The new code emitsindex, DEC_DOLLAR_REFlike$n++.OpcodeJavadoc ofINC,DEC,INC_ARRAY_REF,DEC_ARRAY_REF,INC_DOLLAR_REF,DEC_DOLLAR_REF,POSTINCandPOSTDECnow states what they actually push. The wrong "pushes x+1" onINC_DOLLAR_REFis how++$nand--$nevaluate to another field instead of the updated value #667 came about.Line numbers (#670)
tokenLine), andcurrentSourceLineNumber()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. TheEOFtoken takes the last line of the source, so theLineNumberReaderend-of-stream bump no longer matters (a one-lineeval()reports 1, not 2).ArrayReferenceAst(int lineNo, ...),extensionCallLineNumber(),FunctionCallAst.warningLineNo().print/printftake the line of their name or keyword,ifandwhilethe line of their condition (the)),forthe line where its condition ends. Before, these nodes were built after their body or arguments and reported a later line.ParserExceptiontakes 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 = = 2on 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" + \then1) 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
Unreleasedinbehavior-changes.md.Tests
IncrementDecrementTest(14 tests): prefix and postfix on numeric, text, empty and beyond-NFfields, 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: thel.awkof 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,whileand a multi-lineforheader, multi-linesprintf(),printf,@f()and subscript, a second-fsource and an@included file without a final newline (lines counted in their own source).StrictNumbersTest:getLineNumber()of a one-lineeval()(1), of the last operation of a line, before},x++ending a line, and two operations spanning two lines.AwkParserTest: the line of aParserException, 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.mainand on this branch.main. On this branch, every remaining difference is A number assigned toFS,OFS,SUBSEPand other special variables is converted withtoString(), notCONVFMT#672 (a number assigned toFS,OFS,SUBSEP... prints4.0, filed separately) or the documented exact 64-bit integers; no++/--value differs.🤖 Generated with Claude Code