Log In  

There seems to be an odd interaction between shorthand if, an end from another loop and shorthand print. (whether the if is true or not doesn't seem to matter)

When arranged like this:

for do

if(true)end

?"⁶1⁶c"

or without the if statement:

for do

end?"⁶1⁶c"

it works fine.

But, putting the if statement, the for's end, then the shorthand print on the same line makes PICO-8 put the print statement INSIDE the for loop, so this:

for k,i in pairs(n) do

if(true)end?"⁶1⁶c"

is translated into something like this by the interpreter:

for k,i in pairs(n) do

  if true then
  end --(for's end, becomes if's end at runtime)

  print("⁶1⁶c")

end --(if's end, becomes for's end at runtime)

...with the p8scii flip()cls() being run on each loop!

What I think is happening here is the if is taking the [ end?"⁶1⁶c" ] after it as part of its block, and putting its own "end" after it, where PICO-8 sees the for's end as the if's, and the newly added end as the for's.

Full code:

n={}
::l::
add(n,{t="hi"})

for k,i in pairs(n) do

?i.t,1,k*7

if(true)end

?"⁶1⁶c"

goto l

P#97964 2021-09-29 08:27 ( Edited 2021-10-03 11:05)

:: GPI
1

I don't think this is a bug

if (condition) commands

is a short for

if condition then
  commands
end

I don't know how exact it is solved in Pico8, but I would simple replace it before the lua-"Interpreter" will get the code.

so your

for k,i in pairs(n) do

if(true)end?"⁶1⁶c"

will translated to

for k,i in pairs(n) do

if true then
  end
  ?"⁶1⁶c"
end

so the "end" you thougt is for the for-loop is in fact the end for the if!

I personal think, that a

if(true)end

Should result in an "end without start" - error

Btw. when you need to end a loop before the end condition, there is a command in lua: "break"!
it is not possible to "if" and "end"-statment out.

something

for k,i in pairs(n) do

  if(not true)break
end
?"⁶1⁶c"
P#98144 2021-10-03 07:12 ( Edited 2021-10-03 07:14)
:: cubee

@GPI

Yes, the if(true)end was just an example, not to cancel the loop like break does. In the original code where I found this there is other code between them for the if. (e.g. if(a>b)add(n,2)end )

In the translated code we can see the ?"⁶1⁶c" is now inside the loop, when it was outside before translation.

I added a bit to the end of the main post to hopefully show what I think is happening better.

P#98157 2021-10-03 11:13
:: GPI

I think thats the problem, it was never inside. It "Seems" to be inside, but only because of the "shortcut-hack".

Think about the Shortcut more like a macro. Replace the short with the long version and then interpret it.

As i said, I personaly think that things like this should give a compiler/interpreter error.
An "end" for an "for"-loop can't be inside a shorted if-statement. It is impossible, that an end-statment could be "optional" and it would be optional in the shorted if. Its more a logical error. It the end in the if-statement would be valid, the lua-interpreter simple doesn't know what to do. It could for example produce a memory-leak, because you leave the for-loop without cleaning for-datas.

Luckly pico8 hat a version-string, so old cards can be run in a "compatible"-modus and new carts should rise an error.

btw. in other languages like basic, this would look like:

for i=1 to 10
  if true then
  next
endif

and will rise a "next without for"-error, at least every modern basic version.

edit: And think about it, the shortcut should be an replacement for the longer version, it shouldn't allow new things. When the "end" in the "shorted if" is the "end" for "for", than you can do here things, that would be complete impossible without the shortcut. As I said, the handling is in my opinion correct, but it should raise an compiler-error.

P#98158 2021-10-03 11:28 ( Edited 2021-10-03 11:36)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2021-10-19 15:15:57 | 0.032s | Q:12