Skip to content

Commit

Permalink
Fixes #385
Browse files Browse the repository at this point in the history
  • Loading branch information
eldritchconundrum committed May 10, 2024
1 parent 25c5e6b commit 29ceb3c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ let rec private sideEffects = function
match fct.Declaration with
| Declaration.UserFunction fd when not fd.hasExternallyVisibleSideEffects -> args |> List.collect sideEffects
| _ -> [e]
| FunCall(Op "?:", [condExpr; thenExpr; elseExpr]) ->
let condEffects = sideEffects condExpr
let thenEffects = sideEffects thenExpr
let elseEffects = sideEffects elseExpr
if thenEffects = [] && elseEffects = []
then condEffects
else [FunCall(Op "?:", [condExpr; commaSeparatedExprs thenEffects; commaSeparatedExprs elseEffects])]
| FunCall(Op op, args) when not(Builtin.assignOps.Contains(op)) -> args |> List.collect sideEffects
| FunCall(Dot(_, field) as e, args) when field = "length" -> (e :: args) |> List.collect sideEffects
| FunCall(Subscript _ as e, args) -> (e :: args) |> List.collect sideEffects
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/ternary.frag
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,18 @@ float f1() {
a = true ? b = 2. : b = 3.;
return a;
}

// Fix for #385

out vec4 O;
uniform int k;
void main() {
int d=0,e=0;
k==0 ? (sin(O.x),d=1) : (cos(O.x),e=2) ;
O.x=d;
O.y=e;
}
int f2() {
k++ == 0 ? 1 : 2 ;
return k;
}
16 changes: 16 additions & 0 deletions tests/unit/ternary.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,19 @@ float f1()
float a=1.;
return a=2.;
}
out vec4 O;
uniform int k;
void main()
{
int d=0,e=0;
k==0?
(d=1):
(e=2);
O.x=d;
O.y=e;
}
int f2()
{
k++;
return k;
}

0 comments on commit 29ceb3c

Please sign in to comment.