Skip to content

Commit

Permalink
Remove unnecessary parentheses between ? and : in ternary expressions (
Browse files Browse the repository at this point in the history
…#396)

The rules for how `? :` parse:
- everything between `?` and `:` is parsed as if grouped (just like `(
)` and `{ }`), so the parens can always be omitted from the middle
expression.
- The entire `? expr :` is parsed as if it is one whole binary operator
in relation to the expressions on either side of it, according to normal
precedence rules, thus `?:` gets its spot in the precedence table in the
spec despite its more complex grammar.
  • Loading branch information
therontarigo authored May 13, 2024
1 parent 1f8e7a9 commit 5b9066c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/printer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ type PrinterImpl(withLocations) =
| Op "?:", [a1; a2; a3] ->
let prec = precedence.["?:"]
let res = out "%s?%s%s:%s%s" (exprToSLevel indent (prec+1) a1)
(nl (indent+1)) (exprToSLevel (indent+1) prec a2)
// The middle expression of ?: is parsed as if grouped: precedence doesn't apply to it.
(nl (indent+1)) (exprToSLevel (indent+1) 0 a2)
(nl (indent+1)) (exprToSLevel (indent+1) prec a3)
if prec < level then out "(%s)" res else res

Expand Down
10 changes: 5 additions & 5 deletions tests/real/controllable-machinery.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ vec4 BPos(float t)
float a;
t=mod(t,tCyc);
p=t<5.?
(a=0.,vec3(-1.018+2.118*t/5.,bEdge,0)):
a=0.,vec3(-1.018+2.118*t/5.,bEdge,0):
t<10.?
(a=1.57079635*(t-5.)/5.,vec3(1.1,bEdge+sin(a),1.-cos(a))):
a=1.57079635*(t-5.)/5.,vec3(1.1,bEdge+sin(a),1.-cos(a)):
t<15.?
(a=1.57079635,vec3(1.1-2.118*(t-10.)/5.,1.+bEdge,1)):
a=1.57079635,vec3(1.1-2.118*(t-10.)/5.,1.+bEdge,1):
t<17.5?
(a=1.57079635,vec3(-1.018,1.+bEdge,1.-(t-15.)/2.5)):
a=1.57079635,vec3(-1.018,1.+bEdge,1.-(t-15.)/2.5):
(t-=17.5,a=-1.57079635*t,vec3(-1.018,1.+bEdge-t*t,0));
return vec4(p,a);
}
Expand Down Expand Up @@ -355,7 +355,7 @@ vec3 ShowScene(vec3 ro,vec3 rd)
idObjT=idObj;
dstGear=GearRay(ro,rd);
idObj=dstGear<min(dstObj,dstFar)?
(dstObj=dstGear,1):
dstObj=dstGear,1:
idObjT;
dstBlk=BlkHit(ro,rd);
if(min(dstBlk,dstObj)<dstFar)
Expand Down
2 changes: 1 addition & 1 deletion tests/real/frozen-wasteland.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void mainImage(out vec4 fragColor,vec2 fragCoord)
p.x*=iResolution.x/iResolution.y;
vec2 mo=iMouse.xy/iResolution.xy-.5;
mo=mo==vec2(-.5)?
(mo=vec2(-.1,.07)):
mo=vec2(-.1,.07):
mo;
mo.x*=iResolution.x/iResolution.y;
vec3 ro=vec3(smoothstep(0.,1.,tri(time*1.5)*.3)*1.5,smoothstep(0.,1.,tri(time*3.)*3.)*.08,-time*3.5-130.);
Expand Down
4 changes: 2 additions & 2 deletions tests/real/kinder_painter.expected
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
"{"
"vec3 z;"
"b=m.w>2.5?"
"(z.xz=r.xz-v.xz,z.y=0.,z/=v.w,vec2(z.x,r.y)):"
"z.xz=r.xz-v.xz,z.y=0.,z/=v.w,vec2(z.x,r.y):"
"m.w>1.5?"
"(z=v.xyz,r.xz*.2):"
"z=v.xyz,r.xz*.2:"
"(z=(r-v.xyz)/v.w,z.xy);"
"return z;"
"}"
Expand Down
4 changes: 2 additions & 2 deletions tests/real/mouton/mouton.expected
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ const char *mouton_vert =
"f=vec3(0);"
"x=vec3(0,0,-8);"
"e=mod(v,3.)<1.5?"
"(d=0.,c=vec3(0,2,-8),vec3(0,3,0)):"
"d=0.,c=vec3(0,2,-8),vec3(0,3,0):"
"(c=vec3(0,5.5,2),vec3(0,5.75,0));"
"l=vec3(0,-.1,1);"
"o=3.+v*.1;"
Expand Down Expand Up @@ -361,7 +361,7 @@ const char *mouton_frag =
"v.y-=-z.y;"
"float y=v.y+smoothstep(1.,20.,length(v.xz));"
"return y<.4?"
"(y-=pow((B(v*.9)*.5+B(v*1.6)*.3+B(v*2.7)*.1)*.5+.5,3.)*.45*(1.-exp((-t+137.3)*3.)),vec2(y,12)):"
"y-=pow((B(v*.9)*.5+B(v*1.6)*.3+B(v*2.7)*.1)*.5+.5,3.)*.45*(1.-exp((-t+137.3)*3.)),vec2(y,12):"
"vec2(9e7,0);"
"}"
"mat3 A(vec3 v,vec3 y)"
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/ternary.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void main()
{
int d=0,e=0;
k==0?
(sin(O.x),d=1):
sin(O.x),d=1:
(cos(O.x),e=2);
O.x=float(d);
O.y=float(e);
Expand All @@ -25,3 +25,4 @@ int f2()
k++;
return k;
}

0 comments on commit 5b9066c

Please sign in to comment.