Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect parsing of complex ++ expressions #79

Closed
benhoyt opened this issue Dec 24, 2021 · 8 comments · Fixed by #215
Closed

Incorrect parsing of complex ++ expressions #79

benhoyt opened this issue Dec 24, 2021 · 8 comments · Fixed by #215

Comments

@benhoyt
Copy link
Owner

benhoyt commented Dec 24, 2021

Two cases:

  1. echo 2 3 4 | goawk '{ $$0++; print $0 }' prints 2 4 4 instead of 3. I think this is happening because we parse this as ($($0))++ rather than ($($0++)).
  2. BEGIN { $0="3 4 5 6 7 8 9"; a=3; print $$a++++; print } gives a parsing error instead of printing 7\n3 4 6 6 8 8 9.

There are two commented-out (TODO) tests in interp/interp_test.go for this, as well as a commented-out Gawk test (parse1).

@benhoyt benhoyt added the bug Something isn't working label Dec 24, 2021
@benhoyt
Copy link
Owner Author

benhoyt commented Dec 25, 2021

I've started playing with this on the fix-incr-decr branch.

@benhoyt benhoyt removed the bug Something isn't working label Feb 2, 2022
@vegarsti
Copy link

Can you walk through what is supposed to happen in 1? I don't understand it, sorry.

@benhoyt
Copy link
Owner Author

benhoyt commented Aug 18, 2022

gawk, original-awk, and mawk all print 3 for that example. It's because they parse the expression as $0++ (which returns the current value of $0 but also post-increments $0); then the outer $ does fetches $2 but drops the result.

Whereas GoAWK parses it differently, so executes the inner $0 first (which is 2 3 4), then another $, which fetches the second field 2 (the 3), then the ++ increments that field, creating 2 4 4.

So it's a parsing/precedence issue. Hope that helps!

@vegarsti
Copy link

vegarsti commented Aug 18, 2022

That helps a lot, thanks!

@benhoyt benhoyt changed the title Incorrect parsing of complex ++ expressions Incorrect parsing of complex ++ expressions or $++a Jan 23, 2023
@benhoyt benhoyt changed the title Incorrect parsing of complex ++ expressions or $++a Incorrect parsing of complex ++ expressions Jan 23, 2023
@vegarsti
Copy link

Was this solved by the newest release? Specifically #172

@benhoyt
Copy link
Owner Author

benhoyt commented Apr 12, 2023

@vegarsti No, unfortunately not -- this is a different issue.

@vegarsti
Copy link

Okay!

@fioriandrea
Copy link
Contributor

This also produces wrong results:

./goawk 'BEGIN { $0="3 4 5 6 7 8 9"; a=3; print ($($a++)++); print $0; print a }' prints 7\n3 4 7 6 7 8 9\n3, whereas
gawk 'BEGIN { $0="3 4 5 6 7 8 9"; a=3; print ($($a++)++); print $0; print a }' prints 7\n3 4 6 6 8 8 9\n3

benhoyt pushed a commit that referenced this issue Oct 19, 2023
Fix incorrect parsing and compiling of complex ++ expressions.
Needed to add a new Rote opcode to make this happen.
In addition, fix similar evaluation issues with += and sub/gsub.
Add new regression tests for all of the above.
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 a pull request may close this issue.

3 participants