Skip to content

Commit

Permalink
Improved range cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchiecarroll committed Nov 14, 2024
1 parent affb332 commit dee191b
Show file tree
Hide file tree
Showing 19 changed files with 657 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,14 @@ private static @string ratioString(params nint[] vals) {
private static void Main() {
var strategies = new slice<strategy>(win);
nint k = default;
for (var kΔ1 = 0; kΔ1 < len(strategies); kΔ1++) {
strategies[k] = stayAtK(kΔ1 + 1);
for (var k1 = 0; k < len(strategies); k++) {
strategies[k] = stayAtK(k + 1);
}
var (wins, games) = roundRobin(strategies);
for (var k = 0; k < len(strategies); k++) {
for (k = 0; k < len(strategies); k++) {
fmt.Printf("Wins, losses staying at k =% 4d: %s\n"u8,
k + 1, ratioString(wins[k], games - wins[k]));
}

}

} // end main_package
101 changes: 101 additions & 0 deletions src/Tests/Behavioral/RangeStatements/RangeStatements.cs
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 src/Tests/Behavioral/RangeStatements/RangeStatements.csproj
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 src/Tests/Behavioral/RangeStatements/RangeStatements.go
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 added src/Tests/Behavioral/RangeStatements/go2cs.ico
Binary file not shown.
7 changes: 7 additions & 0 deletions src/go2cs.sln
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FirstClassFunctions", "Test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "math", "gocore\math\math.csproj", "{4F7B7DCD-C13E-417F-B930-BDEC3541604D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RangeStatements", "Tests\Behavioral\RangeStatements\RangeStatements.csproj", "{63F33680-54ED-4317-85FF-05A0E9C0D513}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -246,6 +248,10 @@ Global
{4F7B7DCD-C13E-417F-B930-BDEC3541604D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F7B7DCD-C13E-417F-B930-BDEC3541604D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F7B7DCD-C13E-417F-B930-BDEC3541604D}.Release|Any CPU.Build.0 = Release|Any CPU
{63F33680-54ED-4317-85FF-05A0E9C0D513}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{63F33680-54ED-4317-85FF-05A0E9C0D513}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63F33680-54ED-4317-85FF-05A0E9C0D513}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63F33680-54ED-4317-85FF-05A0E9C0D513}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -289,6 +295,7 @@ Global
{A78DC0F0-49F9-4E0B-A29E-77241B95A5E5} = {F5D0C4D5-7217-4572-A76C-AAC5BEF6219B}
{ACA466A0-4D34-409B-BF97-D04E3A4CA2D9} = {F5D0C4D5-7217-4572-A76C-AAC5BEF6219B}
{4F7B7DCD-C13E-417F-B930-BDEC3541604D} = {D59EEF6C-E2EA-4A78-82CB-BD028A4F0D3D}
{63F33680-54ED-4317-85FF-05A0E9C0D513} = {F5D0C4D5-7217-4572-A76C-AAC5BEF6219B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {96A9E85F-A473-4202-B41E-F0E5F3B9AF22}
Expand Down
3 changes: 2 additions & 1 deletion src/go2cs2/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
//"..\\Tests\\Behavioral\\IfStatements\\IfStatements.go",
//"..\\Tests\\Behavioral\\PointerToPointer\\PointerToPointer.go",
//"..\\Tests\\Behavioral\\LambdaFunctions",
"..\\Tests\\Behavioral\\FirstClassFunctions",
//"..\\Tests\\Behavioral\\FirstClassFunctions",
"..\\Tests\\Behavioral\\RangeStatements",
]
}
]
Expand Down
11 changes: 10 additions & 1 deletion src/go2cs2/convArrayType.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"go/ast"
)

Expand All @@ -9,5 +10,13 @@ func (v *Visitor) convArrayType(arrayType *ast.ArrayType, context ArrayTypeConte
return "slice<" + convertToCSTypeName(v.convExpr(arrayType.Elt, nil)) + ">"
}

return convertToCSTypeName(v.convExpr(arrayType.Elt, nil)) + "[]"
var arraySuffix string

if context.maxLength > 0 {
arraySuffix = fmt.Sprintf("[%d]", context.maxLength)
} else {
arraySuffix = "[]"
}

return convertToCSTypeName(v.convExpr(arrayType.Elt, nil)) + arraySuffix
}
4 changes: 2 additions & 2 deletions src/go2cs2/convCallExpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (v *Visitor) convCallExpr(callExpr *ast.CallExpr) string {
if ident, ok := callExpr.Fun.(*ast.Ident); ok && ident.Name == "make" {
typeParam := v.info.TypeOf(callExpr.Args[0])
typeName := v.convExpr(callExpr.Args[0], nil)
remainingArgs := v.convExprList(callExpr.Args[1:], callExpr.Lparen, &context)
remainingArgs := v.convExprList(callExpr.Args[1:], callExpr.Lparen, context)

if typeParam != nil {
if _, ok := typeParam.(*types.Slice); ok {
Expand All @@ -62,7 +62,7 @@ func (v *Visitor) convCallExpr(callExpr *ast.CallExpr) string {
return fmt.Sprintf("make<%s>(%s)", typeName, remainingArgs)
}

return fmt.Sprintf("%s%s(%s)", constructType, getSanitizedIdentifier(v.convExpr(callExpr.Fun, nil)), v.convExprList(callExpr.Args, callExpr.Lparen, &context))
return fmt.Sprintf("%s%s(%s)", constructType, getSanitizedIdentifier(v.convExpr(callExpr.Fun, nil)), v.convExprList(callExpr.Args, callExpr.Lparen, context))
}

func (v *Visitor) isTypeConversion(callExpr *ast.CallExpr) (bool, string) {
Expand Down
Loading

0 comments on commit dee191b

Please sign in to comment.