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
P#77774 2020-06-07 22:07

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

P#77778 2020-06-07 23:39

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#77797 2020-06-08 16:42

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.

P#77798 2020-06-08 16:48

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"…

P#77803 2020-06-08 17:40 ( Edited 2020-06-08 17:41)

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.)

P#77857 2020-06-09 17:54

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2021-06-25 07:34:27 | 0.008s | Q:17