Skip to content

Commit 0da0664

Browse files
Release v1.1.4 (#65)
* [FEATURE]: Add Support for IEnumerable Yield (#62) * Add Support for IEnumerable Yield (#62) * Added new YieldStateMachineBuilder to handle IEnumerable * Refactored lowering into subclasses * Refactored StateMachineContext * Initial document and tests for enumerable For, Foreach, While --------- Co-authored-by: Matt Edwards <matthew.edwards@wagglebee.net>
1 parent b9c8bf5 commit 0da0664

32 files changed

+2665
-897
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ trees to handle asynchronous workflows and other language constructs.
99
* `AwaitExpression`: An expression that represents an await operation.
1010
* `AsyncBlockExpression`: An expression that represents an asynchronous code block.
1111

12+
* **Yield Expressions**
13+
* `YieldExpression`: An expression that represents a yield return or break statement.
14+
* `EnumerableBlockExpression`: An expression that represents an enumerable code block.
15+
1216
* **Using Expression**
1317
* `UsingExpression`: An expression that automatically disposes IDisposable resources.
1418

@@ -81,6 +85,41 @@ public class Example
8185
}
8286
```
8387

88+
### Yield Expressions
89+
90+
The following example demonstrates how to create a yield expression tree.
91+
92+
When the expression tree is compiled, the `EnumerableBlockExpression` will auto-generate a state machine that executes
93+
`YieldExpressions` in the block.
94+
95+
```csharp
96+
public class Example
97+
{
98+
public void ExampleYield()
99+
{
100+
// Create an enumerable block that yields values
101+
var index = Variable( typeof(int), "index" );
102+
103+
var enumerableBlock = BlockEnumerable(
104+
[index],
105+
For( Assign( index, Constant( 0 ) ), LessThan( index, Constant( 10 ) ), PostIncrementAssign( index ),
106+
Yield( index )
107+
)
108+
);
109+
110+
// Compile and execute the enumerable block
111+
var lambda = Lambda<Func<IEnumerable<int>>>( enumerableBlock );
112+
var compiledLambda = lambda.Compile();
113+
var enumerable = compiledLambda();
114+
115+
foreach( var value in enumerable )
116+
{
117+
Console.WriteLine( $"Yielded value: {value}" );
118+
}
119+
}
120+
}
121+
```
122+
84123
### Using Expression
85124

86125
The following example demonstrates how to create a Using expression.

docs/docs.projitems

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
<Import_RootNamespace>docs</Import_RootNamespace>
1010
</PropertyGroup>
1111
<ItemGroup>
12-
<None Include="$(MSBuildThisFileDirectory)state-machine\overview.md" />
13-
<None Include="$(MSBuildThisFileDirectory)state-machine\state-machine-builder.md" />
14-
<None Include="$(MSBuildThisFileDirectory)state-machine\lowering-visitor.md" />
15-
<None Include="$(MSBuildThisFileDirectory)state-machine\state-machine.md" />
1612
<None Include="$(MSBuildThisFileDirectory)index.md" />
1713
<None Include="$(MSBuildThisFileDirectory).todo.md" />
1814
<None Include="$(MSBuildThisFileDirectory)_config.yml" />

docs/index.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ trees to handle asynchronous workflows and other language constructs.
1414
* `AwaitExpression`: An expression that represents an await operation.
1515
* `AsyncBlockExpression`: An expression that represents an asynchronous code block.
1616

17+
* **Yield Expressions**
18+
* `YieldExpression`: An expression that represents a yield return or break statement.
19+
* `EnumerableBlockExpression`: An expression that represents an enumerable code block.
20+
1721
* **Using Expression**
1822
* `UsingExpression`: An expression that automatically disposes IDisposable resources.
1923

@@ -86,6 +90,41 @@ public class Example
8690
}
8791
```
8892

93+
### Yield Expressions
94+
95+
The following example demonstrates how to create a yield expression tree.
96+
97+
When the expression tree is compiled, the `EnumerableBlockExpression` will auto-generate a state machine that executes
98+
`YieldExpressions` in the block.
99+
100+
```csharp
101+
public class Example
102+
{
103+
public void ExampleYield()
104+
{
105+
// Create an enumerable block that yields values
106+
var index = Variable( typeof(int), "index" );
107+
108+
var enumerableBlock = BlockEnumerable(
109+
[index],
110+
For( Assign( index, Constant( 0 ) ), LessThan( index, Constant( 10 ) ), PostIncrementAssign( index ),
111+
Yield( index )
112+
)
113+
);
114+
115+
// Compile and execute the enumerable block
116+
var lambda = Lambda<Func<IEnumerable<int>>>( enumerableBlock );
117+
var compiledLambda = lambda.Compile();
118+
var enumerable = compiledLambda();
119+
120+
foreach( var value in enumerable )
121+
{
122+
Console.WriteLine( $"Yielded value: {value}" );
123+
}
124+
}
125+
}
126+
```
127+
89128
### Using Expression
90129

91130
The following example demonstrates how to create a Using expression.

docs/state-machine/lowering-visitor.md

Lines changed: 0 additions & 146 deletions
This file was deleted.

docs/state-machine/overview.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)