From de2bf204bea0dadfe220590156279e735676517c Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Sat, 25 May 2024 00:48:26 +0200 Subject: [PATCH 1/5] Printer: add space between + and ++ --- src/printer.fs | 9 ++++++--- tests/unit/shadowing.frag.expected | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/printer.fs b/src/printer.fs index 8f3d3eec..41db1acb 100644 --- a/src/printer.fs +++ b/src/printer.fs @@ -128,11 +128,14 @@ 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) + + let op = if op.EndsWith("+") && e2.StartsWith("+") 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/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) { From f5472b274e21b3c15d7dff6f451a43ab46d5ad8f Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Sat, 25 May 2024 00:51:05 +0200 Subject: [PATCH 2/5] update --- src/printer.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/printer.fs b/src/printer.fs index 41db1acb..c38e85a1 100644 --- a/src/printer.fs +++ b/src/printer.fs @@ -134,7 +134,7 @@ type PrinterImpl(withLocations) = else (exprToSLevel indent prec a1), (exprToSLevel indent (prec+1) a2) - let op = if op.EndsWith("+") && e2.StartsWith("+") then op + " " else op + let op = if op = "+" && e2.StartsWith("+") then op + " " else op let res = out "%s%s%s" e1 op e2 if prec < level then out "(%s)" res else res From b46397b6d0eb49fe2d35307798e943850925f05f Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Sat, 25 May 2024 00:55:27 +0200 Subject: [PATCH 3/5] handle -- --- src/printer.fs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/printer.fs b/src/printer.fs index c38e85a1..f5daee68 100644 --- a/src/printer.fs +++ b/src/printer.fs @@ -134,7 +134,8 @@ type PrinterImpl(withLocations) = else (exprToSLevel indent prec a1), (exprToSLevel indent (prec+1) a2) - let op = if op = "+" && e2.StartsWith("+") then op + " " else op + // 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 From 8f495068346a967301c53d7e25f3a84f2607a576 Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Sat, 25 May 2024 00:59:25 +0200 Subject: [PATCH 4/5] remove nocompile --- tests/unit/shadowing.frag | 2 -- 1 file changed, 2 deletions(-) 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; From bf6cda6059fc504d5f11c17fa79ec31a25b67385 Mon Sep 17 00:00:00 2001 From: Eldritch Conundrum Date: Fri, 24 May 2024 16:06:50 -0700 Subject: [PATCH 5/5] Add test --- tests/unit/operators.expected | 9 +++++++++ tests/unit/operators.frag | 13 ++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) 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; +}