Log In  

This throws a syntax error in PICO-8, but is valid Lua.

x = 4
y = 1

if (x == 1 and y == 1) or x == 2 or
   x == 3 or x == 4 then
  print('yo')
end

As far as I can tell, the parser seems to not like the linebreak in the series of ORs, if the first group has parentheses around it.

btw this is a contrived example just to demonstrate the syntax ;) but I actually did run into this bug in real code

P#26612 2016-08-06 20:38 ( Edited 2016-08-26 09:40)

It's because of the shorthand single-line if, which differentiates itself from multi-line by having parens around the condition and then having no 'then' keyword. The two following fragments are effectively identical:

if (cond) code()

if cond then code() end

Your code is unintentionally equivalent to this longhand version:

if x == 1 and y == 1 [b]then[/b] or x == 2 or
  x == 3 or x == 4) then
  print('yo')
end

Which is obviously not syntactically whole.

You can add parens around the whole condition, which do not cost performance but do cost one token, and it will parse correctly:

if ((x == 1 and y == 1) or x == 2 or
   x == 3 or x == 4) then
  print('yo')
end

I guess zep could take note of whether or not the next token after the closing paren is a binary operator and then cancel the single-line-if parsing, but that's his call, not mine. :)

P#26658 2016-08-08 03:07 ( Edited 2016-08-08 07:10)

Totally should be valid -- fixed for 0.1.9

> take note of whether or not the next token after the closing paren is a binary operator

Exactly; When I wrote this I was too C-brained to imagine anything could happen after the closing parenthesis!

P#26663 2016-08-08 09:28 ( Edited 2016-08-08 13:28)

Please consider the following valid Lua code as well; it does not have a binary operator but fails in PICO-8 too:

function f() return true end

if (f) (x)
   then print("lol") end
P#27383 2016-08-26 05:40 ( Edited 2016-08-26 09:52)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 13:22:59 | 0.009s | Q:13