moonscript
moonscript copied to clipboard
Bug in the table literal layout parser
The following table:
getters:
{ pos: => hsEntPos(@ent)
talkColor: => do
hsEntTalkColor(@ent) or rgb(1, 0, 1)
}
results in the following lua:
getters = {
pos = function(self)
return hsEntPos(self.ent)({
talkColor = function(self)
do
return hsEntTalkColor(self.ent) or rgb(1, 0, 1)
end
end
})
end
},
note that talkColor has become an argument to the function, rather than an entry in the table.
Adding a newline after the { fixes this:
getters:
{
pos: => hsEntPos(@ent)
talkColor: => do
hsEntTalkColor(@ent) or rgb(1, 0, 1)
}
getters = {
pos = function(self)
return hsEntPos(self.ent)
end,
talkColor = function(self)
do
return hsEntTalkColor(self.ent) or rgb(1, 0, 1)
end
end
},
I know that this could still technically be a bug, but I'd like to point out a couple of nitpicks:
{ pos: => hsEntPos(@ent)
talkColor: => do
Here, the second line is technically indented more than the first line. If you move the { to be after the colon, it should work fine.
getters:
{
pos: => hsEntPos(@ent)
talkColor: => do
hsEntTalkColor(@ent) or rgb(1, 0, 1)
}
You can feel perfectly free to just avoid using { and } at all, which would save two completely wasted lines and make your code look cleaner:
getters:
pos: => hsEntPos(@ent)
talkColor: => do
hsEntTalkColor(@ent) or rgb(1, 0, 1)
If it's not a bug, it's certainly surprising. Other layout-based languages I know cause { to open a new layout, and then begin indenting relative to that (rather than relative to the line). When viewed like this, pos and talkColor do in fact have the same indentation.
That being said, the solution above is good enough for me. Thanks!
It's due to the indentation change like @RyanSquared said. Not sure if it's a bug, just an unexpected side effect of how indentation is associated to the line. I do agree that the compiled output is unexpected but I'm not sure what the best approach for the grammar to identify cases like these is. I personally don't use that style when writing tables so I've never run across it.
I checked coffeescript and it appears they suffer from the same issue