Skip to content

Commit e33dffc

Browse files
committed
Replacing Linq Select, Where and Concat with simple iterators / Switching translators to structs and removing ExpressionTranslatorBase
1 parent 8325fb9 commit e33dffc

File tree

48 files changed

+393
-405
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+393
-405
lines changed

ReadableExpressions/ExpressionExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace AgileObjects.ReadableExpressions
22
{
33
using System;
4+
using Extensions;
45
#if NET35
56
using Microsoft.Scripting.Ast;
67
using Translators;
@@ -18,7 +19,7 @@ public static class ExpressionExtensions
1819

1920
#if NET35
2021
/// <summary>
21-
/// Translates the given <paramref name="expression"/> to source-code string.
22+
/// Translates the given Linq <paramref name="expression"/> to a source-code string.
2223
/// </summary>
2324
/// <param name="expression">The Expression to translate.</param>
2425
/// <param name="configuration">The configuration to use for the translation, if required.</param>
@@ -33,7 +34,7 @@ public static string ToReadableString(
3334
}
3435
#endif
3536
/// <summary>
36-
/// Translates the given <paramref name="expression"/> to source-code string.
37+
/// Translates the given <paramref name="expression"/> to a source-code string.
3738
/// </summary>
3839
/// <param name="expression">The Expression to translate.</param>
3940
/// <param name="configuration">The configuration to use for the translation, if required.</param>

ReadableExpressions/ExpressionTranslatorRegistry.cs

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Expression = Microsoft.Scripting.Ast.Expression;
1010
using ExpressionType = Microsoft.Scripting.Ast.ExpressionType;
1111
#endif
12+
using Extensions;
1213
using Translators;
1314

1415
internal class ExpressionTranslatorRegistry
@@ -17,49 +18,41 @@ internal class ExpressionTranslatorRegistry
1718

1819
public ExpressionTranslatorRegistry()
1920
{
20-
var defaultTranslator = new DefaultExpressionTranslator();
21-
var assignmentTranslator = new AssignmentExpressionTranslator(defaultTranslator);
22-
var negationTranslator = new NegationExpressionTranslator();
23-
var parameterTranslator = new ParameterExpressionTranslator();
24-
var memberAccessTranslator = new MemberAccessExpressionTranslator();
25-
var indexAccessTranslator = new IndexAccessExpressionTranslator();
26-
var methodCallTranslator = new MethodCallExpressionTranslator(indexAccessTranslator);
27-
28-
var translators = new List<IExpressionTranslator>
21+
var translators = new IExpressionTranslator[]
2922
{
30-
new ArrayLengthExpressionTranslator(),
31-
assignmentTranslator,
32-
new BinaryExpressionTranslator(negationTranslator),
33-
new BlockExpressionTranslator(parameterTranslator),
34-
new CastExpressionTranslator(),
35-
new ConditionalExpressionTranslator(),
36-
new ConstantExpressionTranslator(),
37-
new DebugInfoExpressionTranslator(),
38-
defaultTranslator,
39-
new DynamicExpressionTranslator(memberAccessTranslator, assignmentTranslator, methodCallTranslator),
40-
new ExtensionExpressionTranslator(),
41-
new GotoExpressionTranslator(),
42-
indexAccessTranslator,
43-
new InitialisationExpressionTranslator(methodCallTranslator),
44-
new LabelExpressionTranslator(),
45-
new LambdaExpressionTranslator(),
46-
new LoopExpressionTranslator(),
47-
memberAccessTranslator,
48-
methodCallTranslator,
49-
negationTranslator,
50-
new NewArrayExpressionTranslator(),
51-
new NewExpressionTranslator(),
52-
parameterTranslator,
53-
new QuotedLambdaExpressionTranslator(),
54-
new RuntimeVariablesExpressionTranslator(),
55-
new SwitchExpressionTranslator(),
56-
new TryCatchExpressionTranslator(),
57-
new TypeEqualExpressionTranslator(),
58-
new UnaryExpressionTranslator()
23+
default(ArrayLengthExpressionTranslator),
24+
default(AssignmentExpressionTranslator),
25+
default(BinaryExpressionTranslator),
26+
default(BlockExpressionTranslator),
27+
default(CastExpressionTranslator),
28+
default(ConditionalExpressionTranslator),
29+
default(ConstantExpressionTranslator),
30+
default(DebugInfoExpressionTranslator),
31+
default(DefaultExpressionTranslator),
32+
default(DynamicExpressionTranslator),
33+
default(ExtensionExpressionTranslator),
34+
default(GotoExpressionTranslator),
35+
default(IndexAccessExpressionTranslator),
36+
default(InitialisationExpressionTranslator),
37+
default(LabelExpressionTranslator),
38+
default(LambdaExpressionTranslator),
39+
default(LoopExpressionTranslator),
40+
default(MemberAccessExpressionTranslator),
41+
default(MethodCallExpressionTranslator),
42+
default(NegationExpressionTranslator),
43+
default(NewArrayExpressionTranslator),
44+
default(NewExpressionTranslator),
45+
default(ParameterExpressionTranslator),
46+
default(QuotedLambdaExpressionTranslator),
47+
default(RuntimeVariablesExpressionTranslator),
48+
default(SwitchExpressionTranslator),
49+
default(TryCatchExpressionTranslator),
50+
default(TypeEqualExpressionTranslator),
51+
default(UnaryExpressionTranslator)
5952
};
6053

6154
_translatorsByType = translators
62-
.SelectMany(t => t.NodeTypes.Select(nt => new
55+
.SelectMany(t => t.NodeTypes.Project(nt => new
6356
{
6457
NodeType = nt,
6558
Translator = t
Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,61 @@
1-
#if NET35
2-
namespace AgileObjects.ReadableExpressions.Extensions
1+
namespace AgileObjects.ReadableExpressions.Extensions
32
{
3+
using System;
44
using System.Collections.Generic;
5+
#if NET35
56
using System.Linq;
7+
#endif
68

79
internal static class InternalEnumerableExtensions
810
{
11+
public static IEnumerable<TResult> Project<TItem, TResult>(this IEnumerable<TItem> items, Func<TItem, TResult> projector)
12+
{
13+
foreach (var item in items)
14+
{
15+
yield return projector.Invoke(item);
16+
}
17+
}
18+
19+
public static IEnumerable<TResult> Project<TItem, TResult>(this IEnumerable<TItem> items, Func<TItem, int, TResult> projector)
20+
{
21+
var i = 0;
22+
23+
foreach (var item in items)
24+
{
25+
yield return projector.Invoke(item, i++);
26+
}
27+
}
28+
29+
public static IEnumerable<T> Combine<T>(this IEnumerable<T> first, IEnumerable<T> second)
30+
{
31+
foreach (var item in first)
32+
{
33+
yield return item;
34+
}
35+
36+
foreach (var item in second)
37+
{
38+
yield return item;
39+
}
40+
}
41+
42+
public static IEnumerable<T> Filter<T>(this IEnumerable<T> items, Func<T, bool> predicate)
43+
{
44+
foreach (var item in items)
45+
{
46+
if (predicate.Invoke(item))
47+
{
48+
yield return item;
49+
}
50+
}
51+
}
52+
53+
#if NET35
954
public static bool Contains<TContained, TItem>(this ICollection<TContained> items, TItem item)
1055
where TContained : TItem
1156
{
1257
return items.Any(i => i.Equals(item));
1358
}
59+
#endif
1460
}
15-
}
16-
#endif
61+
}

ReadableExpressions/Extensions/InternalExpressionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static ExpressionType[] GetCheckedExpressionTypes(this Dictionary<Express
102102
{
103103
return valuesByExpressionTypes
104104
.Keys
105-
.Where(nt => nt.ToString().EndsWith("Checked", StringComparison.Ordinal))
105+
.Filter(nt => nt.ToString().EndsWith("Checked", StringComparison.Ordinal))
106106
.ToArray();
107107
}
108108
}

ReadableExpressions/Extensions/PublicTypeExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
using System;
44
using System.Collections.Generic;
5-
using System.Linq;
65
using NetStandardPolyfills;
76

87
/// <summary>
@@ -116,7 +115,7 @@ private static string GetGenericTypeName(
116115
IEnumerable<Type> typeArguments)
117116
{
118117
var typeGenericTypeArgumentFriendlyNames =
119-
typeArguments.Select(GetFriendlyName).Join(", ");
118+
typeArguments.Project(GetFriendlyName).Join(", ");
120119

121120
typeName = typeName.Replace(
122121
"`" + numberOfParameters,

ReadableExpressions/Extensions/StringExtensions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace AgileObjects.ReadableExpressions
1+
namespace AgileObjects.ReadableExpressions.Extensions
22
{
33
using System;
44
using System.Collections.Generic;
@@ -11,7 +11,6 @@ namespace AgileObjects.ReadableExpressions
1111
using InvocationExpression = Microsoft.Scripting.Ast.InvocationExpression;
1212
using MethodCallExpression = Microsoft.Scripting.Ast.MethodCallExpression;
1313
#endif
14-
using Extensions;
1514
using static System.Environment;
1615

1716
internal static class InternalStringExtensionstensionstensions
@@ -115,7 +114,7 @@ public static string Indented(this string line)
115114

116115
if (line.IsMultiLine())
117116
{
118-
return line.SplitToLines().Select(l => l.Indented()).Join(NewLine);
117+
return line.SplitToLines().Project(l => l.Indented()).Join(NewLine);
119118
}
120119

121120
return IndentSpaces + line;
@@ -268,7 +267,7 @@ public static bool IsComment(this string codeLine)
268267
}
269268

270269
public static string ToStringConcatenation(this IEnumerable<Expression> strings, TranslationContext context)
271-
=> strings.Select(str => GetStringValue(str, context)).Join(" + ");
270+
=> strings.Project(str => GetStringValue(str, context)).Join(" + ");
272271

273272
private static string GetStringValue(Expression value, TranslationContext context)
274273
{

ReadableExpressions/ReadableExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#else
66
using ConstantExpression = Microsoft.Scripting.Ast.ConstantExpression;
77
using Expression = Microsoft.Scripting.Ast.Expression;
8-
98
#endif
9+
using Extensions;
1010

1111
/// <summary>
1212
/// A factory class providing the Comment Expression

ReadableExpressions/TranslationContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ protected override Expression VisitParameter(ParameterExpression variable)
291291
}
292292

293293
var joinedAssignmentData = _constructsByAssignment
294-
.Where(kvp => kvp.Key.Left == variable)
295-
.Select(kvp => new
294+
.Filter(kvp => kvp.Key.Left == variable)
295+
.Project(kvp => new
296296
{
297297
Assignment = kvp.Key,
298298
Construct = kvp.Value

ReadableExpressions/Translators/ArrayLengthExpressionTranslator.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace AgileObjects.ReadableExpressions.Translators
22
{
3+
using System.Collections.Generic;
34
#if !NET35
45
using System.Linq.Expressions;
56
#else
@@ -8,14 +9,14 @@ namespace AgileObjects.ReadableExpressions.Translators
89
using UnaryExpression = Microsoft.Scripting.Ast.UnaryExpression;
910
#endif
1011

11-
internal class ArrayLengthExpressionTranslator : ExpressionTranslatorBase
12+
internal struct ArrayLengthExpressionTranslator : IExpressionTranslator
1213
{
13-
internal ArrayLengthExpressionTranslator()
14-
: base(ExpressionType.ArrayLength)
14+
public IEnumerable<ExpressionType> NodeTypes
1515
{
16+
get { yield return ExpressionType.ArrayLength; }
1617
}
1718

18-
public override string Translate(Expression expression, TranslationContext context)
19+
public string Translate(Expression expression, TranslationContext context)
1920
{
2021
var arrayAccess = context.Translate(((UnaryExpression)expression).Operand);
2122

ReadableExpressions/Translators/AssignmentExpressionTranslator.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace AgileObjects.ReadableExpressions.Translators
22
{
3+
using System;
34
using System.Collections.Generic;
4-
using System.Linq;
55
#if !NET35
66
using System.Linq.Expressions;
77
#else
@@ -12,7 +12,7 @@ namespace AgileObjects.ReadableExpressions.Translators
1212
#endif
1313
using Extensions;
1414

15-
internal class AssignmentExpressionTranslator : ExpressionTranslatorBase
15+
internal struct AssignmentExpressionTranslator : IExpressionTranslator
1616
{
1717
private static readonly Dictionary<ExpressionType, string> _symbolsByNodeType =
1818
new Dictionary<ExpressionType, string>
@@ -37,23 +37,17 @@ internal class AssignmentExpressionTranslator : ExpressionTranslatorBase
3737
private static readonly ExpressionType[] _checkedAssignmentTypes =
3838
_symbolsByNodeType.GetCheckedExpressionTypes();
3939

40-
private readonly DefaultExpressionTranslator _defaultTranslator;
40+
public IEnumerable<ExpressionType> NodeTypes => _symbolsByNodeType.Keys;
4141

42-
internal AssignmentExpressionTranslator(DefaultExpressionTranslator defaultTranslator)
43-
: base(_symbolsByNodeType.Keys.ToArray())
44-
{
45-
_defaultTranslator = defaultTranslator;
46-
}
47-
48-
public override string Translate(Expression expression, TranslationContext context)
42+
public string Translate(Expression expression, TranslationContext context)
4943
{
5044
var assignment = (BinaryExpression)expression;
5145
var target = context.Translate(assignment.Left);
5246

5347
return GetAssignment(target, expression.NodeType, assignment.Right, context);
5448
}
5549

56-
internal string GetAssignment(
50+
internal static string GetAssignment(
5751
string target,
5852
ExpressionType assignmentType,
5953
Expression value,
@@ -62,7 +56,7 @@ internal string GetAssignment(
6256
var symbol = _symbolsByNodeType[assignmentType];
6357

6458
var valueString = (value.NodeType == ExpressionType.Default)
65-
? _defaultTranslator.Translate((DefaultExpression)value)
59+
? DefaultExpressionTranslator.Translate((DefaultExpression)value)
6660
: GetValueTranslation(value, context);
6761

6862
var assignment = target + " " + symbol;
@@ -101,7 +95,7 @@ private static string AdjustForCheckedAssignmentIfAppropriate(
10195
ExpressionType assignmentType,
10296
string assignment)
10397
{
104-
if (!_checkedAssignmentTypes.Contains(assignmentType))
98+
if (Array.IndexOf(_checkedAssignmentTypes, assignmentType) == -1)
10599
{
106100
return assignment;
107101
}

0 commit comments

Comments
 (0)