Skip to content

Commit

Permalink
Updated ExprSwitch to handle CallExpr cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchiecarroll committed Oct 17, 2024
1 parent e1034ed commit 007d87e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 60 deletions.
79 changes: 37 additions & 42 deletions src/Tests/Behavioral/ExprSwitch/ExprSwitch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// _Switch statements_ express conditionals across many// branches.
namespace go;

using fmt = fmt_package;
Expand All @@ -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) {
Expand Down Expand Up @@ -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");
Expand All @@ -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 () {
casewhen t.Hour() is < 12:
fmt.Println("It's before noon");
break;
default:
fmt.Println("It's after noon");
break;
}
switch () {
casewhen 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();
{
Expand All @@ -121,7 +112,6 @@ private static void Main() {
}
}

// missing expression means "true"
fmt.Println(hour);
var c = '\r';
switch (c) {
Expand All @@ -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;
}
Expand All @@ -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");
}

}
Expand Down Expand Up @@ -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
13 changes: 13 additions & 0 deletions src/Tests/Behavioral/ExprSwitch/ExprSwitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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")
}
}
9 changes: 0 additions & 9 deletions src/Tests/Behavioral/ForVariants/ForVariants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -38,17 +33,14 @@ 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++)
{
f(ᏑiɅ2);
goto @out_break;
}

//c
if (iɅ1 > 13)
{
goto @out_continue;
Expand All @@ -59,7 +51,6 @@ private static void Main() {
@out_continue:;
}
@out_break:;
//d
fmt.Println();
fmt.Println("i =", i);
fmt.Println();
Expand Down
4 changes: 2 additions & 2 deletions src/go2cs2/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
}
]
Expand Down
10 changes: 3 additions & 7 deletions src/go2cs2/visitSwitchStmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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("}")
}
Expand Down
3 changes: 3 additions & 0 deletions src/gocore/golib/builtin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,9 @@ public static T _<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)
Expand Down

0 comments on commit 007d87e

Please sign in to comment.