-
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.
- Loading branch information
1 parent
affb332
commit dee191b
Showing
19 changed files
with
657 additions
and
65 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
101 changes: 101 additions & 0 deletions
101
src/Tests/Behavioral/RangeStatements/RangeStatements.cs
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,101 @@ | ||
namespace go; | ||
|
||
using fmt = fmt_package; | ||
|
||
public static partial class main_package { | ||
|
||
private static void Main() { | ||
var nums = new nint[] {2, 3, 4 }.slice(); | ||
nint sum = 0; | ||
nint i = default; | ||
nint num = default; | ||
nint total = default; | ||
for (i = 0; i < len(nums); i++) { | ||
num = nums[i]; | ||
|
||
sum += num; | ||
total += i; | ||
} | ||
fmt.Println("sum:", sum, "total:", total); | ||
for (var iΔ1 = 0; iΔ1 < len(nums); iΔ1++) { | ||
var numΔ1 = nums[iΔ1]; | ||
|
||
if (numΔ1 == 3) { | ||
fmt.Println("index:", iΔ1); | ||
} | ||
} | ||
foreach (var numΔ2 in nums) { | ||
fmt.Println("num:", numΔ2); | ||
} | ||
for (var iΔ2 = 0; iΔ2 < len(nums); iΔ2++) { | ||
fmt.Println("index:", iΔ2); | ||
} | ||
total = 0; | ||
foreach (var _ in nums) { | ||
total++; | ||
} | ||
fmt.Println("Total:", total); | ||
var kvs = new map<@string, @string>{ ["a"u8] = "apple"u8, ["b"u8] = "banana"u8 }; | ||
foreach (var (k1, v1) in kvs) { | ||
fmt.Printf("%s -> %s\n"u8, k1, v1); | ||
} | ||
foreach (var (k1, _) in kvs) { | ||
fmt.Println("key:", k1); | ||
} | ||
foreach (var (v1, _) in kvs) { | ||
fmt.Println("value:", v1); | ||
} | ||
@string k = default; | ||
@string v = default; | ||
foreach (var (kᴛ1, vᴛ1) in kvs) { | ||
k = kᴛ1; | ||
v = vᴛ1; | ||
|
||
fmt.Printf("%s2 -> %s\n"u8, k, v); | ||
} | ||
foreach (var (kᴛ2, _) in kvs) { | ||
k = kᴛ2; | ||
|
||
fmt.Println("key:", k); | ||
} | ||
foreach (var (_, vᴛ2) in kvs) { | ||
v = vᴛ2; | ||
|
||
fmt.Println("val:", v); | ||
} | ||
total = 0; | ||
foreach (var (_, _) in kvs) { | ||
total++; | ||
} | ||
fmt.Println("Total:", total); | ||
foreach (var (iΔ3, c) in @string("go"u8)) { | ||
fmt.Println(iΔ3, c); | ||
} | ||
@string str = "test"u8; | ||
nint i1 = default; | ||
rune c1 = default; | ||
foreach (var (iᴛ1, rᴛ1) in str) { | ||
i1 = iᴛ1; | ||
c1 = str[iᴛ1]; | ||
|
||
fmt.Println(i1, c1); | ||
} | ||
var arr = new nint[5]; | ||
arr[2] = 42; | ||
arr[4] = 100; | ||
for (var iΔ4 = 0; iΔ4 < len(arr); iΔ4++) { | ||
var vΔ1 = arr[iΔ4]; | ||
|
||
fmt.Println(iΔ4, vΔ1); | ||
} | ||
var slice = new slice<nint>(5); | ||
slice[2] = 42; | ||
slice[4] = 100; | ||
for (var iΔ5 = 0; iΔ5 < len(slice); iΔ5++) { | ||
var vΔ2 = slice[iΔ5]; | ||
|
||
fmt.Println(iΔ5, vΔ2); | ||
} | ||
} | ||
|
||
} // end main_package |
50 changes: 50 additions & 0 deletions
50
src/Tests/Behavioral/RangeStatements/RangeStatements.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,50 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>net9.0</TargetFrameworks> | ||
<PublishReadyToRun>true</PublishReadyToRun> | ||
<RootNamespace>go</RootNamespace> | ||
<AssemblyName>RangeStatements</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</NoWarn> | ||
<Version>0.1.0</Version> | ||
</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" /> | ||
|
||
<!-- 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" /> | ||
</ItemGroup> | ||
|
||
</Project> |
131 changes: 131 additions & 0 deletions
131
src/Tests/Behavioral/RangeStatements/RangeStatements.go
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,131 @@ | ||
// _range_ iterates over elements in a variety of data | ||
// structures. Let's see how to use `range` with some | ||
// of the data structures we've already learned. | ||
|
||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
|
||
// Here we use `range` to sum the numbers in a slice. | ||
// Arrays work like this too. | ||
nums := []int{2, 3, 4} | ||
sum := 0 | ||
|
||
var i, num, total int | ||
|
||
for i, num = range nums { | ||
sum += num | ||
total += i | ||
} | ||
fmt.Println("sum:", sum, "total:", total) | ||
|
||
// `range` on arrays and slices provides both the | ||
// index and value for each entry. Above we didn't | ||
// need the index, so we ignored it with the | ||
// blank identifier `_`. Sometimes we actually want | ||
// the indexes though. | ||
for i, num := range nums { | ||
if num == 3 { | ||
fmt.Println("index:", i) | ||
} | ||
} | ||
|
||
for _, num := range nums { | ||
fmt.Println("num:", num) | ||
} | ||
|
||
for i, _ := range nums { | ||
fmt.Println("index:", i) | ||
} | ||
|
||
total = 0 | ||
for range nums { | ||
total++ | ||
} | ||
fmt.Println("Total:", total) | ||
|
||
// `range` on map iterates over key/value pairs. | ||
kvs := map[string]string{"a": "apple", "b": "banana"} | ||
for k1, v1 := range kvs { | ||
fmt.Printf("%s -> %s\n", k1, v1) | ||
} | ||
|
||
// `range` can also iterate over just the keys of a map. | ||
for k1 := range kvs { | ||
fmt.Println("key:", k1) | ||
} | ||
|
||
// `range` can also iterate over just the keys of a map. | ||
for v1 := range kvs { | ||
fmt.Println("value:", v1) | ||
} | ||
|
||
/* Uncomment to test out of order shadowing | ||
kvs := map[string]string{"a": "apple", "b": "banana"} | ||
for k, v := range kvs { | ||
fmt.Printf("%s -> %s\n", k, v) | ||
} | ||
for k := range kvs { | ||
fmt.Println("key:", k) | ||
} | ||
for v := range kvs { | ||
fmt.Println("value:", v) | ||
} | ||
*/ | ||
|
||
var k, v string | ||
|
||
for k, v = range kvs { | ||
fmt.Printf("%s2 -> %s\n", k, v) | ||
} | ||
|
||
for k, _ = range kvs { | ||
fmt.Println("key:", k) | ||
} | ||
|
||
for _, v = range kvs { | ||
fmt.Println("val:", v) | ||
} | ||
|
||
total = 0 | ||
for range kvs { | ||
total++ | ||
} | ||
fmt.Println("Total:", total) | ||
|
||
// `range` on strings iterates over Unicode code | ||
// points. The first value is the starting byte index | ||
// of the `rune` and the second the `rune` itself. | ||
// See [Strings and Runes](strings-and-runes) for more | ||
// details. | ||
for i, c := range "go" { | ||
fmt.Println(i, c) | ||
} | ||
|
||
str := "test" | ||
|
||
var i1 int | ||
var c1 rune | ||
|
||
for i1, c1 = range str { | ||
fmt.Println(i1, c1) | ||
} | ||
|
||
arr := [...]int{2: 42, 4: 100} | ||
|
||
for i, v := range arr { | ||
fmt.Println(i, v) | ||
} | ||
|
||
// Creating a slice with indexed values | ||
slice := []int{2: 42, 4: 100} | ||
|
||
for i, v := range slice { | ||
fmt.Println(i, v) | ||
} | ||
|
||
} |
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
Oops, something went wrong.