Log In  


I’m writing a minifier and I still can’t really wrap my head around the parser.

Here are two similar snippets that differ only in whitespace and do not do the same thing:

> for i=1,2 do
>  if(i)print(i) end
> print(3)
1
2
3
> for i=1,2 do
>  if(i)print(i) end print(3)
1
3
2
3


I think in most cases it just puts the rest of the line between "then " and " end"

> for i=1,2 do
>  if(i)then print(i) end end
> print(3)

> for i=1,2 do
>  if(i)then print(i) end print(3) end

of course there could be more to it though


Yes that's basically what it's doing. One side effect I had to struggle with in picotool is that this accidentally allows for a not uncommon mistake:

x = 8
if (x > 7) do
 print("hi")
 print("what's up")
end

There is no "if-do-end" construction, but with the condition in parens and the lack of a then, this becomes:

x = 8
if (x > 7) then do end
 print("hi")
 print("what's up")
end

The "do-end" is a no-op block, and the "then" block continues to the next "end".

Multiple early carts from multiple authors had "if-do-end" in them. It works if you match PICO-8's preprocessing behavior, but gets messy if you're treating short-if like part of the syntax.


P.S. Strictly speaking a minifier doesn't need a parser, just a lexer. To support the newline-sensitive syntax you have to preserve a single newline when compressing whitespace that contains a newline, but this is no less minified than the equivalent one-space.


Well maybe I’m not writing a real minifier, but I definitely need a parser if I want to do replacements such as (a*b)+3 → a*b+3 or flr(z) → z\1 or print("foo") → print"foo"…


nod One of the main reasons I tried for a parser in picotool was to do constant folding, dead code elimination, and other transformations. (As we've discussed it's not a good parser and I never got around to redoing it properly beyond the proof of concept. Maybe someday.)



[Please log in to post a comment]