Skip to content

Commit

Permalink
Printer: add space between + and ++ (#405)
Browse files Browse the repository at this point in the history
Fixes #403 

Co-authored-by: Eldritch Conundrum <eldritch.conundrum@gmail.com>
  • Loading branch information
laurentlb and eldritchconundrum authored May 24, 2024
1 parent d0fefb2 commit 4c33ec7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
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

0 comments on commit 4c33ec7

Please sign in to comment.