Skip to content

Commit

Permalink
In a declaration, parenthesis are mandatory around a comma operator (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
eldritchconundrum authored Apr 29, 2024
1 parent 7a80761 commit 06d10c7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/printer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ type PrinterImpl(withLocations) =
(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
// Function calls.
| Var op, _ ->
// We set level to 1 in case in case a comma operator is used in the argument list.
out "%s(%s)" (idToS op) (commaListToS (exprToSLevel indent 1) args)

// Unary operators. _++ is prefix and $++ is postfix
| Op op, [a1] when op.[0] = '$' -> out "%s%s" (exprToSLevel indent precedence.[op] a1) op.[1..]
Expand All @@ -133,20 +129,24 @@ type PrinterImpl(withLocations) =
| Op op, [a1; a2] ->
let prec = precedence.[op]
let res =
if prec = 1 then // "=", "+=", or other operator with right-associativity
if prec = precedence.["="] then // "=", "+=", or other operator with right-associativity
out "%s%s%s" (exprToSLevel indent (prec+1) a1) op (exprToSLevel indent prec a2)
else
out "%s%s%s" (exprToSLevel indent prec a1) op (exprToSLevel indent (prec+1) a2)
if prec < level then out "(%s)" res
else res
| _ -> out "%s(%s)" (exprToS indent f) (commaListToS (exprToS indent) args)

// Function calls.
| _ -> // We set the level in case a comma operator is used in the argument list.
out "%s(%s)" (exprToS indent f) (commaListToS (exprToSLevel indent (precedence.[","] + 1)) args)
| Subscript(arr, ind) ->
out "%s[%s]" (exprToS indent arr) (exprToSOpt indent "" ind)
| Cast(id, e) ->
// Cast seems to have the same precedence as unary minus
out "(%s)%s" id.Name (exprToSLevel indent precedence.["_-"] e)
| VectorExp(li) ->
out "{%s}" (commaListToS (exprToS indent) li)
// We set the level in case a comma operator is used in the argument list.
out "{%s}" (commaListToS (exprToSLevel indent (precedence.[","] + 1)) li)
| Dot(e, field) ->
out "%s.%s" (exprToSLevel indent precedence.["."] e) field
| VerbatimExp s -> s
Expand Down Expand Up @@ -194,7 +194,8 @@ type PrinterImpl(withLocations) =
let init =
match decl.init with
| None -> ""
| Some i -> out "=%s" (exprToS indent i)
| Some i -> // We set the level in case a comma operator is used in the argument list.
out "=%s" (exprToSLevel indent (precedence.[","] + 1) i)
out "%s%s%s%s" (idToS decl.name) size (semToS decl.semantics) init

if vars.IsEmpty then ""
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/operators.expected
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"int other(int a,int b,int c,int d)"
"{"
"return a*b*(c*d)*(a*b);"
"}"
"float f(float x)"
"{"
"float a=(x+=1.,length(vec3(x)));"
"return a*a*sin((a*=a,a));"
"}",

#endif
6 changes: 6 additions & 0 deletions tests/unit/operators.frag
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ int no_parens2(int a, int b, int c) {
int other(int a, int b, int c, int d) {
return (a*b)*(c*d)*(a*b);
}

float f(float x)
{
float a = (x += 1.0, length(vec3(x)));
return a*a*sin((a*=a, a));
}
2 changes: 1 addition & 1 deletion tests/unit/simplify.expected
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ float bar(float x)
b+=x;
float arr[2]=float[2](7.,8.);
arr=float[2](5.,float(arr.length()));
float m=a*=10,a*=20,a*=30,b++,i++,58.;
float m=(a*=10,a*=20,a*=30,b++,i++,58.);
return a+b+m;
}
float baz(float a)
Expand Down

0 comments on commit 06d10c7

Please sign in to comment.