Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToCsharpCode: format block inside expression in { ... } #354

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6948,7 +6948,8 @@ internal static StringBuilder ToCSharpString(this Expression e, StringBuilder sb
sb.NewLineIdentCs(body, EnclosedIn.LambdaBody, lineIdent + identSpaces, stripNamespace, printType, identSpaces, notRecognizedToCode);
else
{
sb.NewLine(lineIdent, identSpaces).Append('{');
sb.NewLineIdent(lineIdent).Append('{');

// Body handles `;` itself
if (body is BlockExpression bb)
bb.BlockToCSharpString(sb, lineIdent + identSpaces, stripNamespace, printType, identSpaces, notRecognizedToCode,
Expand All @@ -6959,7 +6960,7 @@ internal static StringBuilder ToCSharpString(this Expression e, StringBuilder sb
if (isBodyExpression)
sb.AddSemicolonIfFits();
}
sb.NewLine(lineIdent, identSpaces).Append('}');
sb.NewLineIdent(lineIdent).Append('}');
}
return sb.Append(')');
}
Expand All @@ -6977,7 +6978,6 @@ internal static StringBuilder ToCSharpString(this Expression e, StringBuilder sb
var x = (ConditionalExpression)e;
if (e.Type == typeof(void)) // otherwise output as ternary expression
{
sb.NewLine(lineIdent, identSpaces);
sb.Append("if (");
x.Test.ToCSharpString(sb, EnclosedIn.IfTest, lineIdent, stripNamespace, printType, identSpaces, notRecognizedToCode);
sb.Append(')');
Expand Down Expand Up @@ -7011,7 +7011,20 @@ internal static StringBuilder ToCSharpString(this Expression e, StringBuilder sb
}
case ExpressionType.Block:
{
return BlockToCSharpString((BlockExpression)e, sb, lineIdent, stripNamespace, printType, identSpaces, notRecognizedToCode: notRecognizedToCode);
if (enclosedIn == EnclosedIn.Block)
return BlockToCSharpString((BlockExpression)e, sb, lineIdent, stripNamespace, printType, identSpaces, notRecognizedToCode: notRecognizedToCode);
else
{
var isExpressionBlock = e.Type != typeof(void) && sb.Length > 0;
sb.Append("{");
if (isExpressionBlock)
sb.Append(" /* BlockExpression cannot be written in C#. Please rewrite the code inside these braces as a C# expression, or reorganize the parent expression as a block. */");
BlockToCSharpString((BlockExpression)e, sb, lineIdent + identSpaces, stripNamespace, printType, identSpaces, notRecognizedToCode: notRecognizedToCode);

if (isExpressionBlock)
sb.Append("/* <- block result */");
return sb.NewLineIdent(lineIdent).Append('}');
}
}
case ExpressionType.Loop:
{
Expand Down
78 changes: 77 additions & 1 deletion test/FastExpressionCompiler.UnitTests/ToCSharpStringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,82 @@ public void Outputs_closed_generic_type_constant_correctly()
Assert.AreEqual(typeof(A<string>), f());
}

[Test]
public void Lambda_with_block_body()
{
var variable = Parameter(typeof(int), "variable");
var e = Lambda<Func<int>>(Block(new [] { variable }, Assign(variable, Constant(1)), Add(variable, Constant(2))));

var cs = e.ToCSharpString();

Assert.AreEqual("""
(Func<int>)(() =>
{
int variable = default;
variable = 1;
return (variable + 2);
});
""", cs);
}

[Test]
public void Nested_blocks()
{
var v1 = Parameter(typeof(int), "v1");
var v2 = Parameter(typeof(int), "v2");
var cs = Block(new [] { v1 },
Assign(v1, Constant(2)),
Block(new [] { v2 },
Assign(v2, Constant(3)),
AddAssign(v1, v2),
IfThen(
Equal(v1, Constant(5)),
Block(
Assign(v2, Constant(7)),
AddAssign(v1, v2)
)
)
),
v1
).ToCSharpString();
Console.WriteLine(cs);
Assert.AreEqual("""
{
int v1 = default;
v1 = 2;
int v2 = default;
v2 = 3;
v1 += v2;
if (v1 == 5)
{
v2 = 7;
v1 += v2;
}
v1;
};
""", cs);
}


[Test]
public void Somehow_handles_block_in_expression()
{
// it's probably not possible to output compilable C# for expressions like this, but at least it can be easy to read
var variable = Parameter(typeof(int), "variable");
var cs = Add(Constant(1), Block(new [] { variable },
Assign(variable, Constant(2)),
variable
)).ToCSharpString();
Assert.AreEqual("""
(1 + { /* BlockExpression cannot be written in C#. Please rewrite the code inside these braces as a C# expression, or reorganize the parent expression as a block. */
int variable = default;
variable = 2;
variable;/* <- block result */
});
""", cs);
}


class A<X> {}
}
}
}