Skip to content

Commit 4b4e58d

Browse files
committed
Translating checked operators using checked() or checked {}
1 parent 094adbe commit 4b4e58d

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

ReadableExpressions.UnitTests/WhenTranslatingMathsOperations.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void ShouldTranslateACheckedAdditionExpression()
3131

3232
var translated = checkedAdditionLambda.ToReadableString();
3333

34-
Assert.AreEqual("(a, b) => a + b", translated);
34+
Assert.AreEqual("(a, b) => checked(a + b)", translated);
3535
}
3636

3737
[TestMethod]
@@ -58,7 +58,7 @@ public void ShouldTranslateACheckedSubtractionExpression()
5858

5959
var translated = checkedSubtractionLambda.ToReadableString();
6060

61-
Assert.AreEqual("(a, b) => a - b", translated);
61+
Assert.AreEqual("(a, b) => checked(a - b)", translated);
6262
}
6363

6464
[TestMethod]
@@ -105,9 +105,11 @@ public void ShouldTranslateACheckedMultiplicationExpression()
105105
intParameter1,
106106
intParameter2);
107107

108+
//Expression<Func<int, int, int>> ass = (a, b) => checked(a * b);
109+
108110
var translated = checkedMultiplicationLambda.ToReadableString();
109111

110-
Assert.AreEqual("(a, b) => a * b", translated);
112+
Assert.AreEqual("(a, b) => checked(a * b)", translated);
111113
}
112114

113115
[TestMethod]

ReadableExpressions/Extensions/InternalExpressionExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace AgileObjects.ReadableExpressions.Extensions
22
{
3+
using System;
4+
using System.Collections.Generic;
35
using System.Linq;
46
using System.Linq.Expressions;
57

@@ -80,5 +82,13 @@ public static bool IsAssignment(this Expression expression)
8082

8183
return false;
8284
}
85+
86+
public static ExpressionType[] GetCheckedExpressionTypes(this Dictionary<ExpressionType, string> valuesByExpressionTypes)
87+
{
88+
return valuesByExpressionTypes
89+
.Keys
90+
.Where(nt => nt.ToString().EndsWith("Checked", StringComparison.Ordinal))
91+
.ToArray();
92+
}
8393
}
8494
}

ReadableExpressions/Translators/AssignmentExpressionTranslator.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace AgileObjects.ReadableExpressions.Translators
22
{
3-
using System;
43
using System.Collections.Generic;
54
using System.Linq;
65
using System.Linq.Expressions;
6+
using Extensions;
77

88
internal class AssignmentExpressionTranslator : ExpressionTranslatorBase
99
{
@@ -27,10 +27,8 @@ internal class AssignmentExpressionTranslator : ExpressionTranslatorBase
2727
[ExpressionType.SubtractAssignChecked] = "-="
2828
};
2929

30-
private static readonly ExpressionType[] _checkedAssignmentTypes = _symbolsByNodeType
31-
.Keys
32-
.Where(nt => nt.ToString().EndsWith("Checked", StringComparison.Ordinal))
33-
.ToArray();
30+
private static readonly ExpressionType[] _checkedAssignmentTypes =
31+
_symbolsByNodeType.GetCheckedExpressionTypes();
3432

3533
private readonly DefaultExpressionTranslator _defaultTranslator;
3634

@@ -62,7 +60,7 @@ internal string GetAssignment(
6260

6361
var assignment = $"{target} {symbol} {valueString}";
6462

65-
assignment = AdjustForCheckedAssignmentIfAppropriate(assignment, assignmentType);
63+
assignment = AdjustForCheckedAssignmentIfAppropriate(assignmentType, assignment);
6664

6765
return assignment;
6866
}
@@ -85,8 +83,8 @@ private string GetValueTranslation(Expression value, TranslationContext context)
8583
}
8684

8785
private static string AdjustForCheckedAssignmentIfAppropriate(
88-
string assignment,
89-
ExpressionType assignmentType)
86+
ExpressionType assignmentType,
87+
string assignment)
9088
{
9189
if (!_checkedAssignmentTypes.Contains(assignmentType))
9290
{

ReadableExpressions/Translators/BinaryExpressionTranslator.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ internal class BinaryExpressionTranslator : ExpressionTranslatorBase
3737
[ExpressionType.SubtractChecked] = "-"
3838
};
3939

40+
private static readonly ExpressionType[] _checkedOperatorTypes =
41+
_operatorsByNodeType.GetCheckedExpressionTypes();
42+
4043
private readonly NegationExpressionTranslator _negationTranslator;
4144
private readonly Dictionary<ExpressionType, BinaryTranslator> _translatorsByNodeType;
4245

@@ -70,7 +73,9 @@ private static string Translate(BinaryExpression binary, TranslationContext cont
7073
var @operator = _operatorsByNodeType[binary.NodeType];
7174
var right = GetTranslation(binary.Right, context);
7275

73-
return $"({left} {@operator} {right})";
76+
var operation = $"{left} {@operator} {right}";
77+
78+
return AdjustForCheckedOperatorIfAppropriate(binary.NodeType, operation);
7479
}
7580

7681
private static string GetTranslation(Expression expression, TranslationContext context)
@@ -85,6 +90,27 @@ private static string GetTranslation(Expression expression, TranslationContext c
8590
return translation;
8691
}
8792

93+
private static string AdjustForCheckedOperatorIfAppropriate(
94+
ExpressionType operatorType,
95+
string operation)
96+
{
97+
if (!_checkedOperatorTypes.Contains(operatorType))
98+
{
99+
return operation.WithSurroundingParentheses();
100+
}
101+
102+
if (operation.IsMultiLine())
103+
{
104+
return $@"
105+
checked
106+
{{
107+
{operation.Indent()}
108+
}}".TrimStart();
109+
}
110+
111+
return $"checked({operation})";
112+
}
113+
88114
private static string TranslateAddition(BinaryExpression binary, TranslationContext context)
89115
{
90116
if ((binary.Left.Type != typeof(string)) && (binary.Right.Type != typeof(string)))

0 commit comments

Comments
 (0)