Skip to content

Commit 54eb589

Browse files
committed
Translating return Goto expressions as code blocks
1 parent a223070 commit 54eb589

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

ReadableExpressions.UnitTests/WhenTranslatingGotos.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void ShouldNotIncludeLabelNamesWithoutAGoto()
140140
var returnTrue = Expression.Return(returnLabelTarget, Expression.Constant(true));
141141

142142
var ifLessThanOneReturnTrue = Expression.IfThen(variableLessThanOne, returnTrue);
143-
143+
144144
var testBlock = Expression.Block(
145145
ifLessThanOneReturnTrue,
146146
Expression.Label(returnLabelTarget, Expression.Constant(false)));
@@ -157,5 +157,43 @@ public void ShouldNotIncludeLabelNamesWithoutAGoto()
157157

158158
Assert.AreEqual(EXPECTED.TrimStart(), translated);
159159
}
160+
161+
162+
[TestMethod]
163+
public void ShouldTranslateAReturnStatementWithABlock()
164+
{
165+
var returnLabelTarget = Expression.Label(typeof(int));
166+
167+
var intVariable = Expression.Variable(typeof(int), "i");
168+
var variableInit = Expression.Assign(intVariable, Expression.Constant(0));
169+
var variablePlusOne = Expression.Add(intVariable, Expression.Constant(1));
170+
var variableAdditionOne = Expression.Assign(intVariable, variablePlusOne);
171+
var variablePlusTwo = Expression.Add(intVariable, Expression.Constant(2));
172+
var variableAdditionTwo = Expression.Assign(intVariable, variablePlusTwo);
173+
174+
var variableBlock = Expression.Block(
175+
new[] { intVariable },
176+
variableInit,
177+
variableAdditionOne,
178+
variableAdditionTwo,
179+
intVariable);
180+
181+
var returnVariableBlock = Expression.Return(returnLabelTarget, variableBlock);
182+
183+
var returnBlock = Expression.Block(returnVariableBlock);
184+
185+
const string EXPECTED = @"
186+
return
187+
{
188+
var i = 0;
189+
i = i + 1;
190+
i = i + 2;
191+
return i;
192+
}";
193+
194+
var translated = returnBlock.ToReadableString();
195+
196+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
197+
}
160198
}
161199
}

ReadableExpressions/Translators/GotoExpressionTranslator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ public override string Translate(Expression expression, TranslationContext conte
2727
return _gotoKindHandlers[gotoExpression.Kind].Invoke(gotoExpression, context);
2828
}
2929

30-
private static string TranslateBreak(GotoExpression gotoExpression, TranslationContext context)
30+
private static string TranslateBreak(GotoExpression breakExpression, TranslationContext context)
3131
=> "break;";
3232

33-
private static string TranslateContinue(GotoExpression gotoExpression, TranslationContext context)
33+
private static string TranslateContinue(GotoExpression continueExpression, TranslationContext context)
3434
=> "continue;";
3535

3636
private static string TranslateGoto(GotoExpression gotoExpression, TranslationContext context)
3737
=> $"goto {gotoExpression.Target.Name};";
3838

39-
private static string TranslateReturn(GotoExpression gotoExpression, TranslationContext context)
39+
private static string TranslateReturn(GotoExpression returnExpression, TranslationContext context)
4040
{
41-
if (gotoExpression.Value == null)
41+
if (returnExpression.Value == null)
4242
{
4343
return "return;";
4444
}
4545

46-
var value = context.Translate(gotoExpression.Value);
46+
var value = context.TranslateAsCodeBlock(returnExpression.Value);
4747

4848
return $"return {value}";
4949
}

ReadableExpressions/Translators/LabelExpressionTranslator.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ public override string Translate(Expression expression, TranslationContext conte
1616

1717
var labelNamePart = GetLabelNamePart(label, context);
1818

19-
var labelValuePart = (label.DefaultValue != null)
20-
? $"{Environment.NewLine}return {context.Translate(label.DefaultValue)};"
21-
: null;
19+
if (label.DefaultValue == null)
20+
{
21+
return labelNamePart;
22+
}
23+
var labelValuePart = $"{Environment.NewLine}return {context.TranslateAsCodeBlock(label.DefaultValue)};";
2224

2325
return labelNamePart + labelValuePart;
2426
}

0 commit comments

Comments
 (0)