dansanderson / picotool

Tools and Python libraries for manipulating Pico-8 game files. http://www.lexaloffle.com/pico-8.php
MIT License
370 stars 46 forks source link

Edge case for luamin and short-ifs #11

Closed dansanderson closed 7 years ago

dansanderson commented 7 years ago

luamin collapses space between non-word characters and keywords. This is mostly correct except it breaks edge cases involving Pico-8's short-if. This feature is invisibly implemented by Pico-8 as a preprocessor replacement, and does not match the "if" if it has a non-word non-space character to its left.

x=true
y=(0)
if (x) y=1
print(y)

minifies to

x=true y=(0)if(x) y=1
print(y)

which would be correct if the "if" were expanded to its long form, but Pico-8 fails to expand it.

This works:

x=true y=(0)if(x) then y=1 end
print(y)

The workaround is to either avoid short-if, or to make sure that the previous line to a short-if ends in a word char.

luamin should either recognize short-if and preserve the leading space or just always preserve the leading space of the "if" keyword.

dansanderson commented 7 years ago

There appears to be a similar issue with the space before "local" getting collapsed.

local x=t[0]local y=1
dansanderson commented 7 years ago

I submitted a quick fix to always insert a space between closing brackets and names/tokens. The luamin command currently uses a tokens-only approach, so I can't do something more sophisticated like inserting spaces before statements specifically. (There's an AST-based minifier in the code but I gave up on it temporarily because it wasn't producing better results than a tokens-only minifier.) The quick fix should handle the common problem cases, and might actually be a complete fix but I'm not sure. :)