Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support switch statements #132

Merged
merged 2 commits into from
Apr 27, 2022
Merged

Support switch statements #132

merged 2 commits into from
Apr 27, 2022

Conversation

jwatzman
Copy link
Contributor

@jwatzman jwatzman commented Apr 27, 2022

See commit messages for details. Also incidentally fixes a bug around IndentedText to make my manual testing a bit less annoying. (I also added a test case which incidentally relies on this fix, though that can be easily changed if problematic.)

I do not have a Windows build machine handy, so this was tested entirely on Linux. I was able to confirm that Checker --skip-glsl-compile works, but I didn't want to fight to get OpenTK to work. Apologies in advance if I've done anything silly!

While I've done a lot of work with OCaml in the past, this is the first time I've touched FSharp -- once again, apologies in advance if I've done anything silly!

This oversight was causing the format to print a literal '\n' instead of
a newline. Add the correct pattern match, and also remove the wildcard
match to prevent similar bugs in the future.
This is relatively simple and straightforward.

Note that this is much more permissive than GLSL compilers seem to be;
for example I'm pretty sure this will allow much more in a case label
than it should (I think a literal int must come after). But I don't
think it will cause the minifier to emit invalid GLSL unless the input
was invalid already.

It's actually somewhat unclear what the right grammar even is here.
Looking at the 4.5 spec linked in laurentlb#18 -- section 9 (the normative
grammar), page 205 says that a `case_label` is `CASE expression COLON`,
but from testing with an actual compiler it looks like it needs to be a
literal integer. Also the definition of `switch_statement_list` doesn't
include `case_label`.  So by my reading that's a bug in the spec (or at
least a place that compilers are quite reasonably more restrictive) so
even if we did want to be perfectly correct here I'm not sure what
exactly that looks like :)

Fixes laurentlb#18.
Copy link
Owner

@laurentlb laurentlb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks really good, thanks a lot!

Thanks for adding tests as well

@@ -121,9 +121,9 @@ module private PrinterImpl =

let backslashN() =
match outputFormat with
| Options.Text | Options.JS -> "\n"
| Options.Text | Options.JS | Options.IndentedText -> "\n"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch!

@laurentlb laurentlb merged commit 342adde into laurentlb:master Apr 27, 2022
@laurentlb
Copy link
Owner

It's fine to be more permissive than compilers (it also makes the tool more future-proof), so accepting all expressions looks good.

jwatzman added a commit to jwatzman/Shader_Minifier that referenced this pull request May 7, 2022
The entire switch statement is considered one scope, so we need to
thread through the renamer environment so we don't generate duplicate
definitions.

It turns out the test case I added in laurentlb#132 already hit this -- the
output was not valid GLSL -- but I didn't notice :(
laurentlb pushed a commit that referenced this pull request May 7, 2022
)

The entire switch statement is considered one scope, so we need to
thread through the renamer environment so we don't generate duplicate
definitions.

It turns out the test case I added in #132 already hit this -- the
output was not valid GLSL -- but I didn't notice :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants