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

Order-of-operations confusion when using augmented assignment and length operator #457

Open
Grissess opened this issue Aug 28, 2024 · 0 comments

Comments

@Grissess
Copy link

Grissess commented Aug 28, 2024

Hello!

I was just bitten by a curious little code generation bug: when using an augmented assignment with the length operator, Lua is generated that doesn't follow the order of operations. It's a bit odd, because the generated Lua does follow the order of operations if the length operator isn't involved. Here's a minimal reproduction:

count, minuend = 3, 2
count -= minuend + 1
print count

count, datum = 3, "ab"
count -= #datum + 1
print count

When using MoonScript version 0.5.0, moonc generates this:

local count, minuend = 3, 2
count = count - (minuend + 1)
print(count)
local datum
count, datum = 3, "ab"
count = count - #datum + 1
return print(count)

which, in Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio, prints this when interpreted:

0
2

This behavior is a bit unexpected, as assignment (including augmented assignment) is supposed to have the lowest precedence in ANSI C (apologies for the C++ ref, but the same table is in my book), and I assume the operators here introduced are supposed to be semantically compatible with that interpretation. Indeed, if that's the case, the count = count - #datum + 1 is incorrect by the standard, and it's in general safe to always convert LHS $= RHS to LHS = LHS $ (RHS) for all operators $.

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

No branches or pull requests

1 participant