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 +1print count
count, datum =3,"ab"
count -=#datum +1print count
When using MoonScript version 0.5.0, moonc generates this:
localcount, minuend=3, 2count=count- (minuend+1)
print(count)
localdatumcount, datum=3, "ab"count=count-#datum+1returnprint(count)
which, in Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio, prints this when interpreted:
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 $.
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:
When using
MoonScript version 0.5.0,mooncgenerates this:which, in
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio, prints this when interpreted: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 + 1is incorrect by the standard, and it's in general safe to always convertLHS $= RHStoLHS = LHS $ (RHS)for all operators$.