From 007d87e932368ecaa60d81ee236119e381016a01 Mon Sep 17 00:00:00 2001 From: "J. Ritchie Carroll" Date: Thu, 17 Oct 2024 01:21:37 -0500 Subject: [PATCH] Updated ExprSwitch to handle CallExpr cases --- src/Tests/Behavioral/ExprSwitch/ExprSwitch.cs | 79 +++++++++---------- src/Tests/Behavioral/ExprSwitch/ExprSwitch.go | 13 +++ .../Behavioral/ForVariants/ForVariants.cs | 9 --- src/go2cs2/.vscode/launch.json | 4 +- src/go2cs2/visitSwitchStmt.go | 10 +-- src/gocore/golib/builtin.cs | 3 + 6 files changed, 58 insertions(+), 60 deletions(-) diff --git a/src/Tests/Behavioral/ExprSwitch/ExprSwitch.cs b/src/Tests/Behavioral/ExprSwitch/ExprSwitch.cs index 56207378..f699172a 100644 --- a/src/Tests/Behavioral/ExprSwitch/ExprSwitch.cs +++ b/src/Tests/Behavioral/ExprSwitch/ExprSwitch.cs @@ -1,4 +1,3 @@ -// _Switch statements_ express conditionals across many// branches. namespace go; using fmt = fmt_package; @@ -25,11 +24,15 @@ private static @string getStr3(@string format, params object[] a) { return fmt.Sprintf(format, a); } +public static nint Foo(nint n) { + fmt.Println(n); + return n; +} + private static void Main() { fmt.Println(getStr("test"u8)); fmt.Println(getStr2("hello, ", "world"u8)); fmt.Println(getStr3("hello, %s"u8, "world")); - // Here's a basic `switch`. nint i = 2; fmt.Print("Write ", i, " as "); switch (i) { @@ -61,9 +64,6 @@ private static void Main() { } fmt.Println(x); - // You can use commas to separate multiple expressions - // in the same `case` statement. We use the optional - // `default` case in this example as well. switch (time.Now().Weekday()) { case time.Saturday or time.Sunday: fmt.Println("It's the weekend"); @@ -76,25 +76,16 @@ private static void Main() { break; } - // Case Mon comment - // `switch` without an expression is an alternate way - // to express if/else logic. Here we also show how the - // `case` expressions can be non-constants. var t = time.Now(); - { - switch (ᐧ) { - case ᐧ when t.Hour() is < 12: - fmt.Println("It's before noon"); - break; - default: - fmt.Println("It's after noon"); - break; - } + switch (ᐧ) { + case ᐧ when t.Hour() is < 12: + fmt.Println("It's before noon"); + break; + default: + fmt.Println("It's after noon"); + break; } - // Before noon - // After noon - // Here is a more complex switch nint hour = 1; nint hour1 = time.Now().Hour(); { @@ -121,7 +112,6 @@ private static void Main() { } } - // missing expression means "true" fmt.Println(hour); var c = '\r'; switch (c) { @@ -130,24 +120,22 @@ private static void Main() { break; } - // "i" before should be saved fmt.Printf("i before = %d\n"u8, i); - // Here is a switch with simple statement and a redeclared identifier plus a fallthrough { nint iɅ1 = 1; var exprꞥ1 = getNext(); var matchꞥ1 = false; - if (exprꞥ1 is -1) { matchꞥ1 = true; + if (exprꞥ1 == -1) { matchꞥ1 = true; fmt.Println("negative"); } - if (exprꞥ1 is 0) { matchꞥ1 = true; + if (exprꞥ1 == 0) { matchꞥ1 = true; fmt.Println("zero"); } - if (exprꞥ1 is 1 or 2) { matchꞥ1 = true; + if (exprꞥ1 == 1 || exprꞥ1 == 2) { matchꞥ1 = true; fmt.Println("one or two"); fallthrough = true; } - if (fallthrough || exprꞥ1 is 3) { matchꞥ1 = true; + if (fallthrough || exprꞥ1 == 3) { matchꞥ1 = true; fmt.Printf("three, but x=%d and i now = %d\n"u8, x, iɅ1); fallthrough = true; } @@ -156,26 +144,23 @@ private static void Main() { } } - // "i" after should be restored fmt.Printf("i after = %d\n"u8, i); { var next = getNext(); var matchꞥ2 = false; if (next is <= -1) { matchꞥ2 = true; fmt.Println("negative"); - { - var exprꞥ3 = getNext(); - var matchꞥ3 = false; - if (exprꞥ3 is 1 or 2) { matchꞥ3 = true; - fmt.Println("sub0 one or two"); - } - if (exprꞥ3 is 3) { matchꞥ3 = true; - fmt.Println("sub0 three"); - fallthrough = true; - } - if (fallthrough || !matchꞥ3) { /* default: */ - fmt.Println("sub0 default"); - } + var exprꞥ3 = getNext(); + var matchꞥ3 = false; + if (exprꞥ3 == 1 || exprꞥ3 == 2) { matchꞥ3 = true; + fmt.Println("sub0 one or two"); + } + if (exprꞥ3 == 3) { matchꞥ3 = true; + fmt.Println("sub0 three"); + fallthrough = true; + } + if (fallthrough || !matchꞥ3) { /* default: */ + fmt.Println("sub0 default"); } } @@ -222,6 +207,16 @@ private static void Main() { } } + var exprꞥ5 = Foo(2); + var matchꞥ5 = false; + if (exprꞥ5 == Foo(1) || exprꞥ5 == Foo(2) || exprꞥ5 == Foo(3)) { matchꞥ5 = true; + fmt.Println("First case"); + fallthrough = true; + } + if (fallthrough || exprꞥ5 == Foo(4)) { matchꞥ5 = true; + fmt.Println("Second case"); + } + } } // end main_package diff --git a/src/Tests/Behavioral/ExprSwitch/ExprSwitch.go b/src/Tests/Behavioral/ExprSwitch/ExprSwitch.go index 4b08d672..b9cf1426 100644 --- a/src/Tests/Behavioral/ExprSwitch/ExprSwitch.go +++ b/src/Tests/Behavioral/ExprSwitch/ExprSwitch.go @@ -27,6 +27,11 @@ func getStr3(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) } +func Foo(n int) int { + fmt.Println(n) + return n +} + func main() { fmt.Println(getStr("test")) @@ -178,4 +183,12 @@ func main() { default: fmt.Println("plus, always a default here because of fallthrough") } + + switch Foo(2) { + case Foo(1), Foo(2), Foo(3): + fmt.Println("First case") + fallthrough + case Foo(4): + fmt.Println("Second case") + } } diff --git a/src/Tests/Behavioral/ForVariants/ForVariants.cs b/src/Tests/Behavioral/ForVariants/ForVariants.cs index a742541b..671aac87 100644 --- a/src/Tests/Behavioral/ForVariants/ForVariants.cs +++ b/src/Tests/Behavioral/ForVariants/ForVariants.cs @@ -9,15 +9,10 @@ private static void Main() { i = 0; while (i < 10) { - // Inner comment f(Ꮡi); - // Call function - // Increment i i++; } - // Post i comment - // Post for comment fmt.Println(); fmt.Println("i =", i); fmt.Println(); @@ -38,9 +33,7 @@ private static void Main() { ref var iɅ1 = ref heap(new nint(), out var ᏑiɅ1); for (iɅ1 = 0; iɅ1 < 5; iɅ1++) { - // a f(ᏑiɅ1); - // b ref var iɅ2 = ref heap(new nint(), out var ᏑiɅ2); for (iɅ2 = 12; iɅ2 < 15; iɅ2++) { @@ -48,7 +41,6 @@ private static void Main() { goto @out_break; } - //c if (iɅ1 > 13) { goto @out_continue; @@ -59,7 +51,6 @@ private static void Main() { @out_continue:; } @out_break:; - //d fmt.Println(); fmt.Println("i =", i); fmt.Println(); diff --git a/src/go2cs2/.vscode/launch.json b/src/go2cs2/.vscode/launch.json index 1173f36d..bbf3cfd4 100644 --- a/src/go2cs2/.vscode/launch.json +++ b/src/go2cs2/.vscode/launch.json @@ -15,9 +15,9 @@ "-tree", //"..\\Tests\\Behavioral\\SortArrayType\\SortArrayType.go", //"..\\Tests\\Behavioral\\ArrayPassByValue\\ArrayPassByValue.go", - //"..\\Tests\\Behavioral\\ExprSwitch\\ExprSwitch.go", + "..\\Tests\\Behavioral\\ExprSwitch\\ExprSwitch.go", //"..\\Tests\\Behavioral\\InterfaceCasting\\InterfaceCasting.go", - "..\\Tests\\Behavioral\\ForVariants\\ForVariants.go", + //"..\\Tests\\Behavioral\\ForVariants\\ForVariants.go", ] } ] diff --git a/src/go2cs2/visitSwitchStmt.go b/src/go2cs2/visitSwitchStmt.go index e8a07f3a..6b268085 100644 --- a/src/go2cs2/visitSwitchStmt.go +++ b/src/go2cs2/visitSwitchStmt.go @@ -49,17 +49,13 @@ func (v *Visitor) visitSwitchStmt(switchStmt *ast.SwitchStmt, source ParentBlock } } - hasSwitchInit := switchStmt.Init != nil || hasFallthroughs || !allConst && switchStmt.Tag != nil - - if hasSwitchInit { + if switchStmt.Init != nil { // Any declared variable will be scoped to switch statement, so create a sub-block for it v.targetFile.WriteString(v.newline) v.writeOutput("{") v.indentLevel++ - if switchStmt.Init != nil { - v.visitStmt(switchStmt.Init, []StmtContext{source}) - } + v.visitStmt(switchStmt.Init, []StmtContext{source}) } v.targetFile.WriteString(v.newline) @@ -270,7 +266,7 @@ func (v *Visitor) visitSwitchStmt(switchStmt *ast.SwitchStmt, source ParentBlock } // Close any locally scoped declared variable sub-block - if hasSwitchInit { + if switchStmt.Init != nil { v.indentLevel-- v.writeOutputLn("}") } diff --git a/src/gocore/golib/builtin.cs b/src/gocore/golib/builtin.cs index dfb92ebe..09cd6e06 100644 --- a/src/gocore/golib/builtin.cs +++ b/src/gocore/golib/builtin.cs @@ -754,6 +754,9 @@ public static T _(this object target) { try { + if (target is string str && typeof(T) == typeof(@string)) + return (T)(object)(new @string(str)); + return (T)target; } catch (InvalidCastException ex)