diff --git a/src/printer.fs b/src/printer.fs index 8f3d3eec..f5daee68 100644 --- a/src/printer.fs +++ b/src/printer.fs @@ -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 diff --git a/tests/unit/operators.expected b/tests/unit/operators.expected index dcd3cab7..549ea972 100644 --- a/tests/unit/operators.expected +++ b/tests/unit/operators.expected @@ -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 diff --git a/tests/unit/operators.frag b/tests/unit/operators.frag index 883bdccb..900429f4 100644 --- a/tests/unit/operators.frag +++ b/tests/unit/operators.frag @@ -49,4 +49,15 @@ float desugar_compound_assignment_for_ternary(float x) else x *= sin(x); return x * x; -} \ No newline at end of file +} + +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; +} diff --git a/tests/unit/shadowing.frag b/tests/unit/shadowing.frag index c8df756e..f356dfe9 100644 --- a/tests/unit/shadowing.frag +++ b/tests/unit/shadowing.frag @@ -1,5 +1,3 @@ -//NOCOMPILE - https://github.com/laurentlb/shader-minifier/issues/403 - float f(float g) { float f = 0.0; diff --git a/tests/unit/shadowing.frag.expected b/tests/unit/shadowing.frag.expected index b964710d..77cee670 100644 --- a/tests/unit/shadowing.frag.expected +++ b/tests/unit/shadowing.frag.expected @@ -1,7 +1,7 @@ float f(float g) { g=0.; - return g+++++g+g; + return g+++ ++g+g; } float g(float f) {