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

Printer: add space between + and ++ #405

Merged
merged 5 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/printer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,15 @@ type PrinterImpl(withLocations) =
// Binary operators.
| Op op, [a1; a2] ->
let prec = precedence.[op]
let res =
let e1, e2 =
if prec = precedence.["="] then // "=", "+=", or other operator with right-associativity
out "%s%s%s" (exprToSLevel indent (prec+1) a1) op (exprToSLevel indent prec a2)
(exprToSLevel indent (prec+1) a1), (exprToSLevel indent prec a2)
else
out "%s%s%s" (exprToSLevel indent prec a1) op (exprToSLevel indent (prec+1) a2)
(exprToSLevel indent prec a1), (exprToSLevel indent (prec+1) a2)

// Add a space to avoid "+" or "-" to be parsed as "++" or "--".
let op = if (op = "+" || op = "-") && e2.StartsWith(op) then op + " " else op
let res = out "%s%s%s" e1 op e2
if prec < level then out "(%s)" res
else res

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/operators.expected
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
"x+cos(x):"
"x*sin(x);"
"return x*x;"
"}"
"float cool_ops(float g)"
"{"
"g=0.;"
"g+=g+++ ++g;"
"g/=-g+g;"
"g-=-g--- --g;"
"g*=g+g;"
"return--g-++g+g;"
"}",

#endif
13 changes: 12 additions & 1 deletion tests/unit/operators.frag
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,15 @@ float desugar_compound_assignment_for_ternary(float x)
else
x *= sin(x);
return x * x;
}
}

float cool_ops(float g)
{
float f = 0.0;
f += +f++ + ++f;
f /= -f - -f;
f -= -f-- - --f;
f *= +f + +f;
f += --f + (- +(++f));
return f;
}
2 changes: 0 additions & 2 deletions tests/unit/shadowing.frag
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//NOCOMPILE - https://github.com/laurentlb/shader-minifier/issues/403

float f(float g)
{
float f = 0.0;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/shadowing.frag.expected
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
float f(float g)
{
g=0.;
return g+++++g+g;
return g+++ ++g+g;
}
float g(float f)
{
Expand Down