Skip to content

Commit 4102b61

Browse files
Improved switch statement conversions
1 parent 45b8732 commit 4102b61

File tree

7 files changed

+229
-70
lines changed

7 files changed

+229
-70
lines changed

src/Tests/Behavioral/ExprSwitch/ExprSwitch.cs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ private static void Main() {
5959
nint x = 5;
6060
fmt.Println(x);
6161
{
62-
nint xɅ1 = 6;
63-
fmt.Println(xɅ1);
62+
nint xΔ1 = 6;
63+
fmt.Println(xΔ1);
6464
}
6565

6666
fmt.Println(x);
@@ -89,21 +89,21 @@ private static void Main() {
8989
nint hour = 1;
9090
nint hour1 = time.Now().Hour();
9191
{
92-
nint hourɅ1 = time.Now().Hour();
92+
nint hourΔ1 = time.Now().Hour();
9393
switch () {
94-
case when hourɅ1 is 1 or < 12 or 2:
94+
case when hourΔ1 is 1 or < 12 or 2:
9595
fmt.Println("Good morning!");
9696
break;
97-
case when (hourɅ1 == 1) || (hourɅ1 < 12) || (hourɅ1 == 2 || hour1 == 4):
97+
case when (hourΔ1 == 1) || (hourΔ1 < 12) || (hourΔ1 == 2 || hour1 == 4):
9898
fmt.Println("Good morning (opt 2)!");
9999
break;
100-
case when hourɅ1 is < 17:
100+
case when hourΔ1 is < 17:
101101
fmt.Println("Good afternoon!");
102102
break;
103-
case when hourɅ1 is 0:
103+
case when hourΔ1 is 0:
104104
fmt.Println("Midnight!");
105105
break;
106-
case when hourɅ1 == 0 && hour1 == 1:
106+
case when hourΔ1 == 0 && hour1 == 1:
107107
fmt.Println("Midnight (opt 2)!");
108108
break;
109109
default:
@@ -122,67 +122,67 @@ private static void Main() {
122122

123123
fmt.Printf("i before = %d\n"u8, i);
124124
{
125-
nint iɅ1 = 1;
126-
var exprꞥ1 = getNext();
127-
var matchꞥ1 = false;
128-
if (exprꞥ1 == -1) { matchꞥ1 = true;
125+
nint iΔ1 = 1;
126+
var exprɅ1 = getNext();
127+
var matchɅ1 = false;
128+
if (exprɅ1 is -1) { matchɅ1 = true;
129129
fmt.Println("negative");
130130
}
131-
if (exprꞥ1 == 0) { matchꞥ1 = true;
131+
else if (exprɅ1 is 0) { matchɅ1 = true;
132132
fmt.Println("zero");
133133
}
134-
if (exprꞥ1 == 1 || exprꞥ1 == 2) { matchꞥ1 = true;
134+
else if (exprɅ1 is 1 or 2) { matchɅ1 = true;
135135
fmt.Println("one or two");
136136
fallthrough = true;
137137
}
138-
if (fallthrough || exprꞥ1 == 3) { matchꞥ1 = true;
139-
fmt.Printf("three, but x=%d and i now = %d\n"u8, x, iɅ1);
138+
if (fallthrough || !matchɅ1 && exprɅ1 is 3) { matchɅ1 = true;
139+
fmt.Printf("three, but x=%d and i now = %d\n"u8, x, iΔ1);
140140
fallthrough = true;
141141
}
142-
if (fallthrough || !matchꞥ1) { /* default: */
142+
if (fallthrough || !matchɅ1) { /* default: */
143143
fmt.Println("plus, always a default here because of fallthrough");
144144
}
145145
}
146146

147147
fmt.Printf("i after = %d\n"u8, i);
148148
{
149149
var next = getNext();
150-
var matchꞥ2 = false;
151-
if (next is <= -1) { matchꞥ2 = true;
150+
var matchɅ2 = false;
151+
if (next is <= -1) { matchɅ2 = true;
152152
fmt.Println("negative");
153-
var exprꞥ3 = getNext();
154-
var matchꞥ3 = false;
155-
if (exprꞥ3 == 1 || exprꞥ3 == 2) { matchꞥ3 = true;
153+
var exprɅ3 = getNext();
154+
var matchɅ3 = false;
155+
if (exprɅ3 is 1 or 2) { matchɅ3 = true;
156156
fmt.Println("sub0 one or two");
157157
}
158-
if (exprꞥ3 == 3) { matchꞥ3 = true;
158+
else if (exprɅ3 is 3) { matchɅ3 = true;
159159
fmt.Println("sub0 three");
160160
fallthrough = true;
161161
}
162-
if (fallthrough || !matchꞥ3) { /* default: */
162+
if (fallthrough || !matchɅ3) { /* default: */
163163
fmt.Println("sub0 default");
164164
}
165165

166166
}
167-
if (next is 0) { matchꞥ2 = true;
167+
else if (next is 0) { matchɅ2 = true;
168168
fmt.Println("zero");
169169
{
170-
var nextɅ1 = getNext();
171-
var matchꞥ4 = false;
172-
if (nextɅ1 is 1 or <= 2) { matchꞥ4 = true;
170+
var nextΔ1 = getNext();
171+
var matchɅ4 = false;
172+
if (nextΔ1 is 1 or <= 2) { matchɅ4 = true;
173173
fmt.Println("sub1 one or two");
174174
}
175-
if (nextɅ1 is 3) { matchꞥ4 = true;
175+
else if (nextΔ1 is 3) { matchɅ4 = true;
176176
fmt.Println("sub1 three");
177177
fallthrough = true;
178178
}
179-
if (fallthrough || !matchꞥ4) { /* default: */
179+
if (fallthrough || !matchɅ4) { /* default: */
180180
fmt.Println("sub1 default");
181181
}
182182
}
183183

184184
}
185-
if (next is 1 or 2) { matchꞥ2 = true;
185+
else if (next is 1 or 2) { matchɅ2 = true;
186186
fmt.Println("one or two");
187187
switch (next) {
188188
case 1 or 2:
@@ -198,24 +198,27 @@ private static void Main() {
198198

199199
fallthrough = true;
200200
}
201-
if (fallthrough || next >= 3 && next < 100) { matchꞥ2 = true;
201+
if (fallthrough || !matchɅ2 && (next >= 3 && next < 100)) { matchɅ2 = true;
202202
fmt.Printf("three or greater < 100: %d\n"u8, x);
203203
fallthrough = true;
204204
}
205-
if (fallthrough || !matchꞥ2) { /* default: */
205+
if (fallthrough || !matchɅ2) { /* default: */
206206
fmt.Println("plus, always a default here because of fallthrough");
207207
}
208208
}
209209

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;
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;
213213
fmt.Println("First case");
214214
fallthrough = true;
215215
}
216-
if (fallthrough || exprꞥ5 == Foo(4)) { matchꞥ5 = true;
216+
if (fallthrough || !matchɅ5 && exprɅ5 == Foo(4)) {
217217
fmt.Println("Second case");
218218
}
219+
else { /* default: */
220+
fmt.Println("Default case");
221+
}
219222

220223
}
221224

src/Tests/Behavioral/ExprSwitch/ExprSwitch.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,7 @@ func main() {
190190
fallthrough
191191
case Foo(4):
192192
fmt.Println("Second case")
193+
default:
194+
fmt.Println("Default case")
193195
}
194196
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//namespace go;
2+
3+
//using fmt = fmt_package;
4+
//using time = time_package;
5+
6+
//public static partial class main_package {
7+
8+
//private static void Main() {
9+
// nint hour1 = time.Now().Hour();
10+
// {
11+
// nint hour = time.Now().Hour();
12+
// switch (ᐧ) {
13+
// case ᐧ when hour is 1 or < 12 or 2:
14+
// fmt.Println("Good morning!");
15+
// break;
16+
// case ᐧ when (hour == 1) || (hour < 12) || (hour == 2 || hour1 == 4):
17+
// fmt.Println("Good morning (opt 2)!");
18+
// break;
19+
// case ᐧ when (hour == 1) || (hour < 12) || (hour == 2 || hour == 4):
20+
// fmt.Println("Good morning (opt 3)!");
21+
// break;
22+
// case ᐧ when hour is < 17:
23+
// fmt.Println("Good afternoon!");
24+
// break;
25+
// case ᐧ when hour is 0:
26+
// fmt.Println("Midnight!");
27+
// break;
28+
// case ᐧ when hour == 2 && hour1 == 1:
29+
// fmt.Println("Midnight (opt 2)!");
30+
// break;
31+
// case ᐧ when hour == 2 && hour > 1:
32+
// fmt.Println("Midnight (opt 2)!");
33+
// break;
34+
// default:
35+
// fmt.Println("Good evening!");
36+
// break;
37+
// }
38+
// }
39+
40+
//}
41+
42+
//} // end main_package
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// _Switch statements_ express conditionals across many
2+
// branches.
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"time"
9+
)
10+
11+
func main() {
12+
// Here is a more complex switch
13+
hour1 := time.Now().Hour()
14+
15+
switch hour := time.Now().Hour(); { // missing expression means "true"
16+
case hour == 1, hour < 12, hour == 2:
17+
fmt.Println("Good morning!")
18+
case hour == 1, hour < 12, hour == 2 || hour1 == 4:
19+
fmt.Println("Good morning (opt 2)!")
20+
case hour == 1, hour < 12, hour == 2 || hour == 4:
21+
fmt.Println("Good morning (opt 3)!")
22+
case hour < 17:
23+
fmt.Println("Good afternoon!")
24+
case hour == 0:
25+
fmt.Println("Midnight!")
26+
case hour == 2 && hour1 == 1:
27+
fmt.Println("Midnight (opt 2)!")
28+
case hour == 2 && hour > 1:
29+
fmt.Println("Midnight (opt 2)!")
30+
default:
31+
fmt.Println("Good evening!")
32+
}
33+
}

src/go2cs2/.vscode/launch.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
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",
19+
//"..\\Tests\\Behavioral\\ExprSwitch\\ExprSwitchMicro.go",
1920
//"..\\Tests\\Behavioral\\InterfaceCasting\\InterfaceCasting.go",
2021
//"..\\Tests\\Behavioral\\ForVariants\\ForVariants.go",
21-
"..\\Tests\\Behavioral\\IfStatements\\IfStatements.go",
22+
//"..\\Tests\\Behavioral\\IfStatements\\IfStatements.go",
2223
]
2324
}
2425
]

src/go2cs2/main.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const RootNamespace = "go"
7878
const ClassSuffix = "_package"
7979
const AddressPrefix = "Ꮡ" // Ꮡ ꝸ ꞥ
8080
const ShadowVarMarker = "Δ" // Δ Ʌ
81-
const TempVarMarker = "ꞥ"
81+
const TempVarMarker = "Ʌ" // Ʌ ꞥ
8282
const ExprSwitchMarker = "ᐧ"
8383

8484
var keywords = NewHashSet[string]([]string{
@@ -388,6 +388,12 @@ func (v *Visitor) getStringLiteral(str string) (result string, isRawStr bool) {
388388
return str, false
389389
}
390390

391+
func (v *Visitor) isNonCallValue(expr ast.Expr) bool {
392+
_, isCallExpr := expr.(*ast.CallExpr)
393+
394+
return v.info.Types[expr].IsValue() && !isCallExpr
395+
}
396+
391397
func getSanitizedIdentifier(identifier string) string {
392398
if strings.HasPrefix(identifier, "@") {
393399
return identifier // Already sanitized
@@ -426,9 +432,18 @@ func isDiscardedVar(varName string) bool {
426432
return len(varName) == 0 || varName == "_"
427433
}
428434

429-
func isComparisonOperator(op string) bool {
435+
func isLogicalOperator(op token.Token) bool {
436+
switch op {
437+
case token.LAND, token.LOR:
438+
return true
439+
default:
440+
return false
441+
}
442+
}
443+
444+
func isComparisonOperator(op token.Token) bool {
430445
switch op {
431-
case "==", "!=", "<", "<=", ">", ">=":
446+
case token.EQL, token.NEQ, token.LSS, token.LEQ, token.GTR, token.GEQ:
432447
return true
433448
default:
434449
return false

0 commit comments

Comments
 (0)