Skip to content

Commit

Permalink
Release v1.1.1 (#40)
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
3 people authored Jan 6, 2025
1 parent e516a9b commit dc2dbe8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<MajorVersion>1</MajorVersion>
<MinorVersion>1</MinorVersion>
<PatchVersion>0</PatchVersion>
<PatchVersion>1</PatchVersion>
</PropertyGroup>
<!-- Disable automatic package publishing -->
<PropertyGroup>
Expand Down
71 changes: 62 additions & 9 deletions src/Hyperbee.Expressions/ForExpression.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Linq.Expressions;
using System.Collections.ObjectModel;
using System.Linq.Expressions;

namespace Hyperbee.Expressions;

public class ForExpression : Expression
{
public IEnumerable<ParameterExpression> Variables { get; }
public Expression Initialization { get; }
public Expression Test { get; }
public Expression Iteration { get; }
Expand All @@ -12,33 +14,36 @@ public class ForExpression : Expression
public LabelTarget BreakLabel { get; } = Label( "break" );
public LabelTarget ContinueLabel { get; } = Label( "continue" );

internal ForExpression( Expression initialization, Expression test, Expression iteration, Expression body )
internal ForExpression( IEnumerable<ParameterExpression> variables, Expression initialization, Expression test, Expression iteration, Expression body )
{
ThrowIfInvalid( test, body );

Variables = variables;
Initialization = initialization;
Test = test;
Iteration = iteration;
Body = body;
}

internal ForExpression( Expression initialization, Expression test, Expression iteration, LoopBody body )
internal ForExpression( IEnumerable<ParameterExpression> variables, Expression initialization, Expression test, Expression iteration, LoopBody body )
{
ThrowIfInvalid( test, body );

Variables = variables;
Initialization = initialization;
Test = test;
Iteration = iteration;
Body = body( BreakLabel, ContinueLabel );
}

internal ForExpression( Expression initialization, Expression test, Expression iteration, Expression body, LabelTarget breakLabel, LabelTarget continueLabel )
internal ForExpression( IEnumerable<ParameterExpression> variables, Expression initialization, Expression test, Expression iteration, Expression body, LabelTarget breakLabel, LabelTarget continueLabel )
{
ThrowIfInvalid( test, body );

ArgumentNullException.ThrowIfNull( breakLabel, nameof( breakLabel ) );
ArgumentNullException.ThrowIfNull( continueLabel, nameof( continueLabel ) );

Variables = variables;
Initialization = initialization;
Test = test;
Iteration = iteration;
Expand All @@ -64,6 +69,7 @@ private static void ThrowIfInvalid( Expression test, object body )
public override Expression Reduce()
{
return Block(
Variables,
Initialization,
Loop(
IfThenElse(
Expand All @@ -82,33 +88,80 @@ public override Expression Reduce()

protected override Expression VisitChildren( ExpressionVisitor visitor )
{
var newVariables = VisitCollection( visitor, Variables.ToArray() );
var newInitialization = visitor.Visit( Initialization );
var newTest = visitor.Visit( Test );
var newIteration = visitor.Visit( Iteration );
var newBody = visitor.Visit( Body );

if ( newInitialization == Initialization && newTest == Test && newIteration == Iteration && newBody == Body )
if ( newVariables == Variables && newInitialization == Initialization && newTest == Test && newIteration == Iteration && newBody == Body )
return this;

return new ForExpression( newInitialization, newTest, newIteration, newBody, BreakLabel, ContinueLabel );
return new ForExpression( newVariables, newInitialization, newTest, newIteration, newBody, BreakLabel, ContinueLabel );

}

private static IEnumerable<T> VisitCollection<T>( ExpressionVisitor visitor, T[] nodes ) where T : Expression
{
T[] newNodes = null;

for ( int i = 0, n = nodes.Length; i < n; i++ )
{
var node = visitor.Visit( nodes[i] );

if ( newNodes != null )
{
newNodes[i] = (T) node;
}
else if ( !ReferenceEquals( node, nodes[i] ) )
{
newNodes = new T[n];
for ( int j = 0; j < i; j++ )
{
newNodes[j] = nodes[j];
}
newNodes[i] = (T) node;
}
}

if ( newNodes == null )
{
return nodes;
}

return newNodes;
}
}

public static partial class ExpressionExtensions
{
public static ForExpression For( Expression initialization, Expression test, Expression iteration, Expression body )
{
return new ForExpression( initialization, test, iteration, body );
return new ForExpression( [], initialization, test, iteration, body );
}

public static ForExpression For( Expression initialization, Expression test, Expression iteration, Expression body, LabelTarget breakLabel, LabelTarget continueLabel )
{
return new ForExpression( initialization, test, iteration, body, breakLabel, continueLabel );
return new ForExpression( [], initialization, test, iteration, body, breakLabel, continueLabel );
}

public static ForExpression For( Expression initialization, Expression test, Expression iteration, LoopBody body )
{
return new ForExpression( initialization, test, iteration, body );
return new ForExpression( [], initialization, test, iteration, body );
}

public static ForExpression For( IEnumerable<ParameterExpression> variables, Expression initialization, Expression test, Expression iteration, Expression body )
{
return new ForExpression( variables, initialization, test, iteration, body );
}

public static ForExpression For( IEnumerable<ParameterExpression> variables, Expression initialization, Expression test, Expression iteration, Expression body, LabelTarget breakLabel, LabelTarget continueLabel )
{
return new ForExpression( variables, initialization, test, iteration, body, breakLabel, continueLabel );
}

public static ForExpression For( IEnumerable<ParameterExpression> variables, Expression initialization, Expression test, Expression iteration, LoopBody body )
{
return new ForExpression( variables, initialization, test, iteration, body );
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="DotNext.Metaprogramming" Version="5.16.1" />
<PackageReference Include="DotNext.Metaprogramming" Version="5.17.2" />
<PackageReference Include="FastExpressionCompiler" Version="5.0.1" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions test/Hyperbee.Expressions.Tests/BlockAsyncBasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,10 +493,10 @@ public async Task BlockAsync_ShouldAllowParallelBlocks_WithTaskWhenAll( Complete
var iteration = PostIncrementAssign( i );

var block = BlockAsync(
[tracker, tasks, i],
[tracker, tasks],
Assign( tracker, NewArrayBounds( typeof( int ), Constant( 5 ) ) ),
Assign( tasks, New( typeof( List<Task> ).GetConstructors()[0] ) ),
For( initIncrement, condition, iteration,
For( [i], initIncrement, condition, iteration,
Block(
[temp],
Assign( temp, i ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PackageReference Include="coverlet.collector" Version="6.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit dc2dbe8

Please sign in to comment.