Skip to content

Commit dc2dbe8

Browse files
Release v1.1.1 (#40)
- Update Nugets - Update to use the variable list for `ForExpression` * Previous version was 'v1.1.0'. Version now 'v1.1.1'. --------- Co-authored-by: Brenton Farmer <brent.farmer@wagglebee.net> Co-authored-by: MattEdwardsWaggleBee <MattEdwardsWaggleBee@users.noreply.github.com>
1 parent e516a9b commit dc2dbe8

File tree

5 files changed

+68
-15
lines changed

5 files changed

+68
-15
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<MajorVersion>1</MajorVersion>
55
<MinorVersion>1</MinorVersion>
6-
<PatchVersion>0</PatchVersion>
6+
<PatchVersion>1</PatchVersion>
77
</PropertyGroup>
88
<!-- Disable automatic package publishing -->
99
<PropertyGroup>

src/Hyperbee.Expressions/ForExpression.cs

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using System.Linq.Expressions;
1+
using System.Collections.ObjectModel;
2+
using System.Linq.Expressions;
23

34
namespace Hyperbee.Expressions;
45

56
public class ForExpression : Expression
67
{
8+
public IEnumerable<ParameterExpression> Variables { get; }
79
public Expression Initialization { get; }
810
public Expression Test { get; }
911
public Expression Iteration { get; }
@@ -12,33 +14,36 @@ public class ForExpression : Expression
1214
public LabelTarget BreakLabel { get; } = Label( "break" );
1315
public LabelTarget ContinueLabel { get; } = Label( "continue" );
1416

15-
internal ForExpression( Expression initialization, Expression test, Expression iteration, Expression body )
17+
internal ForExpression( IEnumerable<ParameterExpression> variables, Expression initialization, Expression test, Expression iteration, Expression body )
1618
{
1719
ThrowIfInvalid( test, body );
1820

21+
Variables = variables;
1922
Initialization = initialization;
2023
Test = test;
2124
Iteration = iteration;
2225
Body = body;
2326
}
2427

25-
internal ForExpression( Expression initialization, Expression test, Expression iteration, LoopBody body )
28+
internal ForExpression( IEnumerable<ParameterExpression> variables, Expression initialization, Expression test, Expression iteration, LoopBody body )
2629
{
2730
ThrowIfInvalid( test, body );
2831

32+
Variables = variables;
2933
Initialization = initialization;
3034
Test = test;
3135
Iteration = iteration;
3236
Body = body( BreakLabel, ContinueLabel );
3337
}
3438

35-
internal ForExpression( Expression initialization, Expression test, Expression iteration, Expression body, LabelTarget breakLabel, LabelTarget continueLabel )
39+
internal ForExpression( IEnumerable<ParameterExpression> variables, Expression initialization, Expression test, Expression iteration, Expression body, LabelTarget breakLabel, LabelTarget continueLabel )
3640
{
3741
ThrowIfInvalid( test, body );
3842

3943
ArgumentNullException.ThrowIfNull( breakLabel, nameof( breakLabel ) );
4044
ArgumentNullException.ThrowIfNull( continueLabel, nameof( continueLabel ) );
4145

46+
Variables = variables;
4247
Initialization = initialization;
4348
Test = test;
4449
Iteration = iteration;
@@ -64,6 +69,7 @@ private static void ThrowIfInvalid( Expression test, object body )
6469
public override Expression Reduce()
6570
{
6671
return Block(
72+
Variables,
6773
Initialization,
6874
Loop(
6975
IfThenElse(
@@ -82,33 +88,80 @@ public override Expression Reduce()
8288

8389
protected override Expression VisitChildren( ExpressionVisitor visitor )
8490
{
91+
var newVariables = VisitCollection( visitor, Variables.ToArray() );
8592
var newInitialization = visitor.Visit( Initialization );
8693
var newTest = visitor.Visit( Test );
8794
var newIteration = visitor.Visit( Iteration );
8895
var newBody = visitor.Visit( Body );
8996

90-
if ( newInitialization == Initialization && newTest == Test && newIteration == Iteration && newBody == Body )
97+
if ( newVariables == Variables && newInitialization == Initialization && newTest == Test && newIteration == Iteration && newBody == Body )
9198
return this;
9299

93-
return new ForExpression( newInitialization, newTest, newIteration, newBody, BreakLabel, ContinueLabel );
100+
return new ForExpression( newVariables, newInitialization, newTest, newIteration, newBody, BreakLabel, ContinueLabel );
94101

95102
}
103+
104+
private static IEnumerable<T> VisitCollection<T>( ExpressionVisitor visitor, T[] nodes ) where T : Expression
105+
{
106+
T[] newNodes = null;
107+
108+
for ( int i = 0, n = nodes.Length; i < n; i++ )
109+
{
110+
var node = visitor.Visit( nodes[i] );
111+
112+
if ( newNodes != null )
113+
{
114+
newNodes[i] = (T) node;
115+
}
116+
else if ( !ReferenceEquals( node, nodes[i] ) )
117+
{
118+
newNodes = new T[n];
119+
for ( int j = 0; j < i; j++ )
120+
{
121+
newNodes[j] = nodes[j];
122+
}
123+
newNodes[i] = (T) node;
124+
}
125+
}
126+
127+
if ( newNodes == null )
128+
{
129+
return nodes;
130+
}
131+
132+
return newNodes;
133+
}
96134
}
97135

98136
public static partial class ExpressionExtensions
99137
{
100138
public static ForExpression For( Expression initialization, Expression test, Expression iteration, Expression body )
101139
{
102-
return new ForExpression( initialization, test, iteration, body );
140+
return new ForExpression( [], initialization, test, iteration, body );
103141
}
104142

105143
public static ForExpression For( Expression initialization, Expression test, Expression iteration, Expression body, LabelTarget breakLabel, LabelTarget continueLabel )
106144
{
107-
return new ForExpression( initialization, test, iteration, body, breakLabel, continueLabel );
145+
return new ForExpression( [], initialization, test, iteration, body, breakLabel, continueLabel );
108146
}
109147

110148
public static ForExpression For( Expression initialization, Expression test, Expression iteration, LoopBody body )
111149
{
112-
return new ForExpression( initialization, test, iteration, body );
150+
return new ForExpression( [], initialization, test, iteration, body );
151+
}
152+
153+
public static ForExpression For( IEnumerable<ParameterExpression> variables, Expression initialization, Expression test, Expression iteration, Expression body )
154+
{
155+
return new ForExpression( variables, initialization, test, iteration, body );
156+
}
157+
158+
public static ForExpression For( IEnumerable<ParameterExpression> variables, Expression initialization, Expression test, Expression iteration, Expression body, LabelTarget breakLabel, LabelTarget continueLabel )
159+
{
160+
return new ForExpression( variables, initialization, test, iteration, body, breakLabel, continueLabel );
161+
}
162+
163+
public static ForExpression For( IEnumerable<ParameterExpression> variables, Expression initialization, Expression test, Expression iteration, LoopBody body )
164+
{
165+
return new ForExpression( variables, initialization, test, iteration, body );
113166
}
114167
}

test/Hyperbee.Expressions.Benchmark/Hyperbee.Expressions.Benchmark.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
@@ -10,7 +10,7 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
13-
<PackageReference Include="DotNext.Metaprogramming" Version="5.16.1" />
13+
<PackageReference Include="DotNext.Metaprogramming" Version="5.17.2" />
1414
<PackageReference Include="FastExpressionCompiler" Version="5.0.1" />
1515
</ItemGroup>
1616

test/Hyperbee.Expressions.Tests/BlockAsyncBasicTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,10 @@ public async Task BlockAsync_ShouldAllowParallelBlocks_WithTaskWhenAll( Complete
493493
var iteration = PostIncrementAssign( i );
494494

495495
var block = BlockAsync(
496-
[tracker, tasks, i],
496+
[tracker, tasks],
497497
Assign( tracker, NewArrayBounds( typeof( int ), Constant( 5 ) ) ),
498498
Assign( tasks, New( typeof( List<Task> ).GetConstructors()[0] ) ),
499-
For( initIncrement, condition, iteration,
499+
For( [i], initIncrement, condition, iteration,
500500
Block(
501501
[temp],
502502
Assign( temp, i ),

test/Hyperbee.Expressions.Tests/Hyperbee.Expressions.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="coverlet.collector" Version="6.0.2">
16+
<PackageReference Include="coverlet.collector" Version="6.0.3">
1717
<PrivateAssets>all</PrivateAssets>
1818
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1919
</PackageReference>

0 commit comments

Comments
 (0)