Skip to content

Commit

Permalink
Fix precedence of the ?: operator (#306)
Browse files Browse the repository at this point in the history
Fixes #305

Also fix compilation tests
  • Loading branch information
laurentlb committed May 1, 2023
1 parent debce88 commit e097fa4
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/printer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type PrinterImpl(outputFormat) =
match f, args with
| Op "?:", [a1; a2; a3] ->
let prec = precedence.["?:"]
let res = out "%s?%s%s:%s%s" (exprToSLevel indent prec a1)
let res = out "%s?%s%s:%s%s" (exprToSLevel indent (prec+1) a1)
(nl (indent+1)) (exprToSLevel (indent+1) prec a2)
(nl (indent+1)) (exprToSLevel (indent+1) prec a3)
if prec < level then out "(%s)" res else res
Expand Down
1 change: 1 addition & 0 deletions tests/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
--preprocess --no-remove-unused --no-renaming --format indented -o tests/unit/preprocess.frag.expected tests/unit/preprocess.frag
--preprocess --no-remove-unused --no-renaming --format indented -o tests/unit/preprocess_if.frag.expected tests/unit/preprocess_if.frag
--no-remove-unused --no-renaming --format indented -o tests/unit/shadowing.frag.expected tests/unit/shadowing.frag
--no-remove-unused --no-renaming --format indented -o tests/unit/precedence.frag.expected tests/unit/precedence.frag

# Unused removal tests

Expand Down
10 changes: 5 additions & 5 deletions tests/unit/arg-inlining.expected
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ float noinline_cannotInlineWhenNotResolvable(float a)
}
float noinline_cannotInlineWhenNotResolvable(vec2 a)
{
return a+11.;
return length(a)+11.;
}
float noinline_canInlineWhenInParameter()
{
Expand All @@ -32,19 +32,19 @@ float noinline_cannotInlineWhenArgsAreDifferent(float a)
}
float noinline_canInlineWhenArgIsInlinable1()
{
return true+40.;
return 20.;
}
float noinline_canInlineWhenArgIsInlinable2()
{
return-18+40.;
return float(-18)+40.;
}
float noinline_canInlineWhenArgIsInlinable3()
{
return 496.;
}
float noinline_canInlineWhenArgIsInlinable4()
{
return vec3(9)+40.;
return vec3(9).x;
}
float noinline_canInlineWhenArgIsInlinable5()
{
Expand All @@ -60,7 +60,7 @@ float noinline_cannotInlineWhenArgIsNotInlinable(float a)
}
float f()
{
float s=0;
float s=0.;
s+=noinline_canInlineWhenResolvable();
s+=noinline_cannotInlineWhenNotResolvable(1.);
s+=noinline_canInlineWhenInParameter();
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/arg-inlining.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

float noinline_canInlineWhenResolvable(float a) { return a+10.; }
float noinline_cannotInlineWhenNotResolvable(float a) { return a+11.; }
float noinline_cannotInlineWhenNotResolvable(vec2 a) { return a+11.; }
float noinline_cannotInlineWhenNotResolvable(vec2 a) { return length(a)+11.; }

float noinline_canInlineWhenInParameter(in float a) { return a+20.; }
float noinline_cannotInlineWhenOutParameter(out float a) { return a+21.; }
Expand All @@ -11,17 +11,17 @@ float noinline_cannotInlineWhenInOutParameter(inout float a) { return a+22.; }
float noinline_canInlineWhenArgIsAlwaysTheSame(float a) { return a+30.; }
float noinline_cannotInlineWhenArgsAreDifferent(float a) { return a+31.; }

float noinline_canInlineWhenArgIsInlinable1(float a) { return a+40.; }
float noinline_canInlineWhenArgIsInlinable2(float a) { return a+40.; }
float noinline_canInlineWhenArgIsInlinable1(bool a) { return a?20.:40.; }
float noinline_canInlineWhenArgIsInlinable2(int a) { return float(a)+40.; }
float noinline_canInlineWhenArgIsInlinable3(float a) { return a+40.; }
float noinline_canInlineWhenArgIsInlinable4(float a) { return a+40.; }
float noinline_canInlineWhenArgIsInlinable4(vec3 a) { return a.x; }
float noinline_canInlineWhenArgIsInlinable5(float a) { return a+40.; }
float noinline_canInlineWhenArgIsInlinable6(float a) { return a+40.; }
float noinline_cannotInlineWhenArgIsNotInlinable(float a) { return a+41.; }

float f()
{
float s = 0;
float s = 0.;

s += noinline_canInlineWhenResolvable(1.);
s += noinline_cannotInlineWhenNotResolvable(1.);
Expand Down
26 changes: 14 additions & 12 deletions tests/unit/overload.expected
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# define VAR_ZH "t"
# define VAR__V "x"
# define VAR_a_ "i"
# define VAR_output "f"
# define VAR_stopinlining "v"

const char *overload_frag =
Expand All @@ -12,51 +13,52 @@ const char *overload_frag =
"uniform vec2 i;"
"in vec2 t;"
"out float v;"
"out vec4 f;"
"float h()"
"{"
"return v+.1;"
"}"
"float f()"
"float m()"
"{"
"return v+.2;"
"}"
"float h(int t)"
"{"
"return v+.3;"
"}"
"float f(float t)"
"float m(float t)"
"{"
"return v+.4;"
"}"
"float f(bool t)"
"float h(bool t)"
"{"
"return v+.5;"
"}"
"float f(int t,int f)"
"float h(int t,int m)"
"{"
"return v+.6;"
"}"
"float f(int t,int f,int x,int h)"
"float h(int t,int m,int x,int f)"
"{"
"return v+.7;"
"}"
"float f(sampler2D t,float v)"
"float h(sampler2D t,float v)"
"{"
"return texelFetch(t,ivec2(255.*v)%256,0).x;"
"}"
"float f(sampler2D t,vec2 v)"
"float h(sampler2D t,vec2 v)"
"{"
"return texelFetch(t,ivec2(255.*v)%256,0).x;"
"}"
"float f(sampler2D t,vec3 v)"
"float h(sampler2D t,vec3 v)"
"{"
"float f=texelFetch(t,ivec2(255.*v.yz)%256,0).x;"
"return texelFetch(t,ivec2(255.*v.x,255.*f)%256,0).x;"
"float m=texelFetch(t,ivec2(255.*v.yz)%256,0).x;"
"return texelFetch(t,ivec2(255.*v.x,255.*m)%256,0).x;"
"}"
"void main()"
"{"
"float v=0.,r=f(true)+f(0,1)-f(0,1,2,3),C=f(x,i*t);"
"gl_FragColor=vec4(h()+2.*h(0),2.*f(1.2)-f(),r,C+v++);"
"float v=0.,D=h(true)+h(0,1)-h(0,1,2,3),F=h(x,i*t);"
"f=vec4(h()+2.*h(0),2.*m(1.2)-m(),D,F+v++);"
"}";

#endif // OVERLOAD_EXPECTED_
3 changes: 2 additions & 1 deletion tests/unit/overload.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ uniform sampler2D _V;
uniform vec2 a_;
in vec2 ZH;
out float stopinlining;
out vec4 output;

float f1() { return stopinlining+0.1; }
float f2(void) { return stopinlining+0.2; }
Expand Down Expand Up @@ -33,5 +34,5 @@ void main()
float b = 2. * f2(1.2) - f2();
float c = f3(true) + f3(0, 1) - f3(0, 1, 2, 3);
float d = hashTex(_V, a_ * ZH);
gl_FragColor=vec4(a,b,c,d+stopinlining++);
output=vec4(a,b,c,d+stopinlining++);
}
4 changes: 4 additions & 0 deletions tests/unit/precedence.frag
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
float foo(bool a, bool b, bool c) {
return (a?b:c)?.1:.2;
}

void main()
{
float f1 = 2. * 3. / 4. - 5. / 6.;
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/precedence.frag.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
float foo(bool a,bool b,bool c)
{
return(a?
b:
c)?
.1:
.2;
}
void main()
{
float f1=1.5-5./6.,n=1./(f1+10.+12.);
gl_FragColor=vec4(n,1./(float(171)*((2.+f1)*64.-7.+12.)),n,0);
}
2 changes: 2 additions & 0 deletions tests/unit/simple.frag
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#version 120

void main()
{
gl_FragColor = vec4(0.2, 0.4, 0.6, 0.);
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/simplify.expected
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#version 330

out vec3 output;
void main()
{
gl_fragColor.xyz=vec3(92);
output.xyz=vec3(92);
}
5 changes: 4 additions & 1 deletion tests/unit/simplify.frag
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#version 330

int n = 2;
float y = 47.;
out vec3 output;

float foo(float a) { if (n == 1) return 0.; if (n == 2) return a; return 1.; }
float foo(float a, float b) { if (y > a) return 0.; if (y < b) return a; return 1.; }

void main() {
vec3 v = vec3(foo(42.) + foo(50., 70.));
gl_fragColor.rgb = v;
output.rgb = v;
}
5 changes: 4 additions & 1 deletion tests/unit/unused_removal.frag
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ float foo(float hidden) {
}

void main(){
if (false) return noinline_actually_unreachable2();
if (false) {
noinline_actually_unreachable2();
return;
}
gl_FragColor = vec4(foo(3.), g());
}
5 changes: 2 additions & 3 deletions tests/unit/vectors.frag
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ float withExtraComponents() {

float withExtraComponentsUnsafe(in vec3 a) {
vec3 v1 = vec3(a.x);
vec3 v2 = vec3(1.0, a.x);
vec2 v3 = vec3(1.0, a.y);
vec2 v3 = vec2(1.0, a.y);
vec3 v4 = vec3(1.0, 2.0, a.y);
return v1.x + v2.x + v3.x + v4.x;
return v1.x + v3.x + v4.x;
}

struct S{
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/vectors.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ float withExtraComponents()
}
float withExtraComponentsUnsafe(vec3 a)
{
vec3 v1=vec3(a.x),v2=vec3(1,a.x);
vec2 v3=vec3(1,a.y);
vec3 v1=vec3(a.x);
vec2 v3=vec2(1,a.y);
vec3 v4=vec3(1,2,a.y);
return v1.x+v2.x+v3.x+v4.x;
return v1.x+v3.x+v4.x;
}struct S{vec2 p1;vec2 cp1;vec2 cp2;vec2 p2;};
vec2 calc(S points,float t)
{
Expand Down

0 comments on commit e097fa4

Please sign in to comment.