Skip to content

Commit 007d87e

Browse files
Updated ExprSwitch to handle CallExpr cases
1 parent e1034ed commit 007d87e

File tree

6 files changed

+58
-60
lines changed

6 files changed

+58
-60
lines changed

src/Tests/Behavioral/ExprSwitch/ExprSwitch.cs

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// _Switch statements_ express conditionals across many// branches.
21
namespace go;
32

43
using fmt = fmt_package;
@@ -25,11 +24,15 @@ private static @string getStr3(@string format, params object[] a) {
2524
return fmt.Sprintf(format, a);
2625
}
2726

27+
public static nint Foo(nint n) {
28+
fmt.Println(n);
29+
return n;
30+
}
31+
2832
private static void Main() {
2933
fmt.Println(getStr("test"u8));
3034
fmt.Println(getStr2("hello, ", "world"u8));
3135
fmt.Println(getStr3("hello, %s"u8, "world"));
32-
// Here's a basic `switch`.
3336
nint i = 2;
3437
fmt.Print("Write ", i, " as ");
3538
switch (i) {
@@ -61,9 +64,6 @@ private static void Main() {
6164
}
6265

6366
fmt.Println(x);
64-
// You can use commas to separate multiple expressions
65-
// in the same `case` statement. We use the optional
66-
// `default` case in this example as well.
6767
switch (time.Now().Weekday()) {
6868
case time.Saturday or time.Sunday:
6969
fmt.Println("It's the weekend");
@@ -76,25 +76,16 @@ private static void Main() {
7676
break;
7777
}
7878

79-
// Case Mon comment
80-
// `switch` without an expression is an alternate way
81-
// to express if/else logic. Here we also show how the
82-
// `case` expressions can be non-constants.
8379
var t = time.Now();
84-
{
85-
switch () {
86-
case when t.Hour() is < 12:
87-
fmt.Println("It's before noon");
88-
break;
89-
default:
90-
fmt.Println("It's after noon");
91-
break;
92-
}
80+
switch () {
81+
case when t.Hour() is < 12:
82+
fmt.Println("It's before noon");
83+
break;
84+
default:
85+
fmt.Println("It's after noon");
86+
break;
9387
}
9488

95-
// Before noon
96-
// After noon
97-
// Here is a more complex switch
9889
nint hour = 1;
9990
nint hour1 = time.Now().Hour();
10091
{
@@ -121,7 +112,6 @@ private static void Main() {
121112
}
122113
}
123114

124-
// missing expression means "true"
125115
fmt.Println(hour);
126116
var c = '\r';
127117
switch (c) {
@@ -130,24 +120,22 @@ private static void Main() {
130120
break;
131121
}
132122

133-
// "i" before should be saved
134123
fmt.Printf("i before = %d\n"u8, i);
135-
// Here is a switch with simple statement and a redeclared identifier plus a fallthrough
136124
{
137125
nint iɅ1 = 1;
138126
var exprꞥ1 = getNext();
139127
var matchꞥ1 = false;
140-
if (exprꞥ1 is -1) { matchꞥ1 = true;
128+
if (exprꞥ1 == -1) { matchꞥ1 = true;
141129
fmt.Println("negative");
142130
}
143-
if (exprꞥ1 is 0) { matchꞥ1 = true;
131+
if (exprꞥ1 == 0) { matchꞥ1 = true;
144132
fmt.Println("zero");
145133
}
146-
if (exprꞥ1 is 1 or 2) { matchꞥ1 = true;
134+
if (exprꞥ1 == 1 || exprꞥ1 == 2) { matchꞥ1 = true;
147135
fmt.Println("one or two");
148136
fallthrough = true;
149137
}
150-
if (fallthrough || exprꞥ1 is 3) { matchꞥ1 = true;
138+
if (fallthrough || exprꞥ1 == 3) { matchꞥ1 = true;
151139
fmt.Printf("three, but x=%d and i now = %d\n"u8, x, iɅ1);
152140
fallthrough = true;
153141
}
@@ -156,26 +144,23 @@ private static void Main() {
156144
}
157145
}
158146

159-
// "i" after should be restored
160147
fmt.Printf("i after = %d\n"u8, i);
161148
{
162149
var next = getNext();
163150
var matchꞥ2 = false;
164151
if (next is <= -1) { matchꞥ2 = true;
165152
fmt.Println("negative");
166-
{
167-
var exprꞥ3 = getNext();
168-
var matchꞥ3 = false;
169-
if (exprꞥ3 is 1 or 2) { matchꞥ3 = true;
170-
fmt.Println("sub0 one or two");
171-
}
172-
if (exprꞥ3 is 3) { matchꞥ3 = true;
173-
fmt.Println("sub0 three");
174-
fallthrough = true;
175-
}
176-
if (fallthrough || !matchꞥ3) { /* default: */
177-
fmt.Println("sub0 default");
178-
}
153+
var exprꞥ3 = getNext();
154+
var matchꞥ3 = false;
155+
if (exprꞥ3 == 1 || exprꞥ3 == 2) { matchꞥ3 = true;
156+
fmt.Println("sub0 one or two");
157+
}
158+
if (exprꞥ3 == 3) { matchꞥ3 = true;
159+
fmt.Println("sub0 three");
160+
fallthrough = true;
161+
}
162+
if (fallthrough || !matchꞥ3) { /* default: */
163+
fmt.Println("sub0 default");
179164
}
180165

181166
}
@@ -222,6 +207,16 @@ private static void Main() {
222207
}
223208
}
224209

210+
var exprꞥ5 = Foo(2);
211+
var matchꞥ5 = false;
212+
if (exprꞥ5 == Foo(1) || exprꞥ5 == Foo(2) || exprꞥ5 == Foo(3)) { matchꞥ5 = true;
213+
fmt.Println("First case");
214+
fallthrough = true;
215+
}
216+
if (fallthrough || exprꞥ5 == Foo(4)) { matchꞥ5 = true;
217+
fmt.Println("Second case");
218+
}
219+
225220
}
226221

227222
} // end main_package

src/Tests/Behavioral/ExprSwitch/ExprSwitch.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ func getStr3(format string, a ...interface{}) string {
2727
return fmt.Sprintf(format, a...)
2828
}
2929

30+
func Foo(n int) int {
31+
fmt.Println(n)
32+
return n
33+
}
34+
3035
func main() {
3136

3237
fmt.Println(getStr("test"))
@@ -178,4 +183,12 @@ func main() {
178183
default:
179184
fmt.Println("plus, always a default here because of fallthrough")
180185
}
186+
187+
switch Foo(2) {
188+
case Foo(1), Foo(2), Foo(3):
189+
fmt.Println("First case")
190+
fallthrough
191+
case Foo(4):
192+
fmt.Println("Second case")
193+
}
181194
}

src/Tests/Behavioral/ForVariants/ForVariants.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,10 @@ private static void Main() {
99
i = 0;
1010
while (i < 10)
1111
{
12-
// Inner comment
1312
f(Ꮡi);
14-
// Call function
15-
// Increment i
1613
i++;
1714
}
1815

19-
// Post i comment
20-
// Post for comment
2116
fmt.Println();
2217
fmt.Println("i =", i);
2318
fmt.Println();
@@ -38,17 +33,14 @@ private static void Main() {
3833
ref var iɅ1 = ref heap(new nint(), out var ᏑiɅ1);
3934
for (iɅ1 = 0; iɅ1 < 5; iɅ1++)
4035
{
41-
// a
4236
f(ᏑiɅ1);
43-
// b
4437
ref var iɅ2 = ref heap(new nint(), out var ᏑiɅ2);
4538
for (iɅ2 = 12; iɅ2 < 15; iɅ2++)
4639
{
4740
f(ᏑiɅ2);
4841
goto @out_break;
4942
}
5043

51-
//c
5244
if (iɅ1 > 13)
5345
{
5446
goto @out_continue;
@@ -59,7 +51,6 @@ private static void Main() {
5951
@out_continue:;
6052
}
6153
@out_break:;
62-
//d
6354
fmt.Println();
6455
fmt.Println("i =", i);
6556
fmt.Println();

src/go2cs2/.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
"-tree",
1616
//"..\\Tests\\Behavioral\\SortArrayType\\SortArrayType.go",
1717
//"..\\Tests\\Behavioral\\ArrayPassByValue\\ArrayPassByValue.go",
18-
//"..\\Tests\\Behavioral\\ExprSwitch\\ExprSwitch.go",
18+
"..\\Tests\\Behavioral\\ExprSwitch\\ExprSwitch.go",
1919
//"..\\Tests\\Behavioral\\InterfaceCasting\\InterfaceCasting.go",
20-
"..\\Tests\\Behavioral\\ForVariants\\ForVariants.go",
20+
//"..\\Tests\\Behavioral\\ForVariants\\ForVariants.go",
2121
]
2222
}
2323
]

src/go2cs2/visitSwitchStmt.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,13 @@ func (v *Visitor) visitSwitchStmt(switchStmt *ast.SwitchStmt, source ParentBlock
4949
}
5050
}
5151

52-
hasSwitchInit := switchStmt.Init != nil || hasFallthroughs || !allConst && switchStmt.Tag != nil
53-
54-
if hasSwitchInit {
52+
if switchStmt.Init != nil {
5553
// Any declared variable will be scoped to switch statement, so create a sub-block for it
5654
v.targetFile.WriteString(v.newline)
5755
v.writeOutput("{")
5856
v.indentLevel++
5957

60-
if switchStmt.Init != nil {
61-
v.visitStmt(switchStmt.Init, []StmtContext{source})
62-
}
58+
v.visitStmt(switchStmt.Init, []StmtContext{source})
6359
}
6460

6561
v.targetFile.WriteString(v.newline)
@@ -270,7 +266,7 @@ func (v *Visitor) visitSwitchStmt(switchStmt *ast.SwitchStmt, source ParentBlock
270266
}
271267

272268
// Close any locally scoped declared variable sub-block
273-
if hasSwitchInit {
269+
if switchStmt.Init != nil {
274270
v.indentLevel--
275271
v.writeOutputLn("}")
276272
}

src/gocore/golib/builtin.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,9 @@ public static T _<T>(this object target)
754754
{
755755
try
756756
{
757+
if (target is string str && typeof(T) == typeof(@string))
758+
return (T)(object)(new @string(str));
759+
757760
return (T)target;
758761
}
759762
catch (InvalidCastException ex)

0 commit comments

Comments
 (0)