-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added initial channel select statement handling
- Loading branch information
1 parent
1752b7a
commit 3d87f14
Showing
24 changed files
with
791 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
namespace go; | ||
|
||
using fmt = fmt_package; | ||
|
||
public static partial class main_package { | ||
|
||
private static void g1(channel<nint> ch) { | ||
ch.ᐸꟷ(12); | ||
} | ||
|
||
private static void g2(channel<nint> ch) { | ||
ch.ᐸꟷ(32); | ||
} | ||
|
||
private static void sum(slice<nint> s, channel<nint> c) { | ||
nint sum = 0; | ||
foreach (var (_, v) in s) { | ||
sum += v; | ||
} | ||
c.ᐸꟷ(sum); | ||
} | ||
|
||
private static void fibonacci(channel<nint> f, channel<nint> quit) { | ||
nint x = 0; | ||
nint y = 1; | ||
while (ᐧ) { | ||
switch (WhenAny(f.ᐸꟷ(x), quit.Receiving)) { | ||
case 0: | ||
(x, y) = (y, x + y); | ||
break; | ||
case 1 when ᐸꟷ(quit, OK).ok: | ||
fmt.Println("quit"); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
private static void Main() { | ||
var ch = new channel<nint>(2); | ||
ch.ᐸꟷ(1); | ||
ch.ᐸꟷ(2); | ||
fmt.Println(ᐸꟷ(ch)); | ||
fmt.Println(ᐸꟷ(ch)); | ||
var ch1 = new channel<nint>(1); | ||
var ch2 = new channel<nint>(1); | ||
goǃ(_ => g1(ch1)); | ||
goǃ(_ => g2(ch2)); | ||
switch (WhenAny(ch1.Receiving, ch2.Receiving)) { | ||
case 0 when ch1.ꟷᐳ(out var v1): | ||
fmt.Println("Got: ", v1); | ||
break; | ||
case 1 when ch2.ꟷᐳ(out var v2): | ||
fmt.Println("Got: ", v2); | ||
break; | ||
} | ||
var s = new nint[]{7, 2, 8, -9, 4, 0}.slice(); | ||
var c = new channel<nint>(1); | ||
goǃ(_ => sum(s[..(int)(len(s) / 2)], c)); | ||
goǃ(_ => sum(s[(int)(len(s) / 2)..], c)); | ||
goǃ(_ => sum(s[2..5], c)); | ||
nint x = ᐸꟷ(c); | ||
nint y = ᐸꟷ(c); | ||
nint z = ᐸꟷ(c); | ||
fmt.Println(x, y, x + y, z); | ||
var f = new channel<nint>(1); | ||
var quit = new channel<nint>(1); | ||
goǃ(() => { | ||
for (nint i = 0; i < 10; i++) { | ||
fmt.Println(ᐸꟷ(f)); | ||
} | ||
quit.ᐸꟷ(0); | ||
}); | ||
fibonacci(f, quit); | ||
} | ||
|
||
} // end main_package |
61 changes: 61 additions & 0 deletions
61
src/Tests/Behavioral/SelectStatement/SelectStatement.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>net9.0</TargetFrameworks> | ||
<RootNamespace>go</RootNamespace> | ||
<AssemblyName>SelectStatement</AssemblyName> | ||
<Product>go2cs</Product> | ||
<Copyright>Copyright © 2024</Copyright> | ||
<PackageProjectUrl>https://github.com/GridProtectionAlliance/go2cs</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/GridProtectionAlliance/go2cs</RepositoryUrl> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<ApplicationIcon>go2cs.ico</ApplicationIcon> | ||
<Nullable>enable</Nullable> | ||
<NoWarn>660;661;IDE1006;CS8981</NoWarn> | ||
<Version>0.1.4</Version> | ||
<LangVersion>latest</LangVersion> | ||
<PublishReadyToRun>true</PublishReadyToRun> | ||
<PublishTrimmed>True</PublishTrimmed> | ||
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract> | ||
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile> | ||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> | ||
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(OutDir)'==''"> | ||
<OutDir>bin\$(Configuration)\$(TargetFramework)\</OutDir> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="go.builtin" Static="True" /> | ||
<Using Include="System.Byte" Alias="uint8" /> | ||
<Using Include="System.UInt16" Alias="uint16" /> | ||
<Using Include="System.UInt32" Alias="uint32" /> | ||
<Using Include="System.UInt64" Alias="uint64" /> | ||
<Using Include="System.SByte" Alias="int8" /> | ||
<Using Include="System.Int16" Alias="int16" /> | ||
<Using Include="System.Int32" Alias="int32" /> | ||
<Using Include="System.Int64" Alias="int64" /> | ||
<Using Include="System.Single" Alias="float32" /> | ||
<Using Include="System.Double" Alias="float64" /> | ||
<Using Include="System.Numerics.Complex" Alias="complex128" /> | ||
<Using Include="System.Int32" Alias="rune" /> | ||
<Using Include="System.UIntPtr" Alias="uintptr" /> | ||
<Using Include="System.Numerics.BigInteger" Alias="GoUntyped" /> | ||
<Using Include="System.ComponentModel.DescriptionAttribute" Alias="GoTag" /> | ||
|
||
<!-- Exclude the output of source generators from the compilation --> | ||
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" /> | ||
|
||
<!-- TODO: Add references to required projects --> | ||
<ProjectReference Include="..\..\..\gocore\golib\golib.csproj" /> | ||
<ProjectReference Include="..\..\..\gocore\fmt\fmt.csproj" /> | ||
<ProjectReference Include="..\..\..\gocore\math\math.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\go2cs.CodeGenerators\go2cs.CodeGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="All" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func g1(ch chan int) { | ||
ch <- 12 | ||
} | ||
|
||
func g2(ch chan int) { | ||
ch <- 32 | ||
} | ||
|
||
func sum(s []int, c chan int) { | ||
sum := 0 | ||
for _, v := range s { | ||
sum += v | ||
} | ||
c <- sum // send sum to c | ||
} | ||
|
||
func fibonacci(f, quit chan int) { | ||
x, y := 0, 1 | ||
for { | ||
select { | ||
case f <- x: | ||
x, y = y, x+y | ||
case <-quit: | ||
fmt.Println("quit") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
ch := make(chan int, 2) | ||
|
||
ch <- 1 | ||
ch <- 2 | ||
|
||
fmt.Println(<-ch) | ||
fmt.Println(<-ch) | ||
|
||
ch1 := make(chan int) | ||
ch2 := make(chan int) | ||
|
||
go g1(ch1) | ||
go g2(ch2) | ||
|
||
select { | ||
case v1 := <-ch1: | ||
fmt.Println("Got: ", v1) | ||
case v2 := <-ch2: | ||
fmt.Println("Got: ", v2) | ||
//default: | ||
// fmt.Println("Default") | ||
} | ||
|
||
s := []int{7, 2, 8, -9, 4, 0} | ||
|
||
c := make(chan int) | ||
go sum(s[:len(s)/2], c) | ||
go sum(s[len(s)/2:], c) | ||
go sum(s[2:5], c) | ||
x, y := <-c, <-c // receive from c | ||
z := <-c | ||
fmt.Println(x, y, x+y, z) | ||
|
||
f := make(chan int) | ||
quit := make(chan int) | ||
go func() { | ||
for i := 0; i < 10; i++ { | ||
fmt.Println(<-f) | ||
} | ||
quit <- 0 | ||
}() | ||
fibonacci(f, quit) | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.