SkyTemple / ExplorerScript

ExplorerScript and SSBScript: Script languages for decompiled SSB (Pokémon Mystery Dungeon Explorers of Sky)
MIT License
16 stars 6 forks source link

Fix forever #40

Closed theCapypara closed 2 months ago

theCapypara commented 3 months ago

This fixes that forever blocks without a continue statement would not actually loop.

:warning: This is a breaking change, but also a bugfix, since this was never supposed to be the behaviour.

Example:

print("before loop");
forever {
    print("in loop");
}
print("after loop");

Assuming print is a function that prints the const string, this would print before:

before loop
in loop
after loop

It will now print:

before loop
in loop
in loop
in loop

The last line repeats indefinitely.

:information_source: This does not change the fact that the decompiler will still add continue statements to forevers, even if they may seem redundant.

Frostbyte0x70 commented 3 months ago

I find this example a bit confusing. You say it wouldn't loop without continue;, which implies it would loop if continue; is present. But then you show an example that does have continue; and say that wouldn't have looped before?

theCapypara commented 3 months ago

Whoops, I removed it. I had that in there while testing. There isn't supposed to be a continue in the example.

Frostbyte0x70 commented 3 months ago

Whoops, I removed it. I had that in there while testing. There isn't supposed to be a continue in the example.

Oh, there we go. It makes sense now.

This does not change the fact that the decompiler will still add continue statements to forevers, even if they may seem redundant.

Why is this btw? Aren't they unnecessary now?

theCapypara commented 3 months ago

No. The reason they were added had nothing to do with this bug, except for the fact that they kinda hid it.

I need to reexamine if this is true, but according to the TODO comment in the decompiler, there is no reliable way in the optimization phase to know if the place where the dummy continues are inserted are really going to be the last statement in hhe generated ExplorerScript code and would actually cause a loop. I don't know anymore why that would be, in theory no matter how the code ends up being generated it should match the graph and thus always be effectively the last statement, so it should loop even without a continue. I'll do some testing.

theCapypara commented 3 months ago

Yeah basically there is no way to know if the marker that generates continues is actually required (a real continue) or not because it was auto-inserted by previous steps in the decompliation. It might be possible to mark these somehow.