Skip to content

Commit 085a6e2

Browse files
Code / comment cleanup
1 parent 009fdfa commit 085a6e2

File tree

6 files changed

+89
-17
lines changed

6 files changed

+89
-17
lines changed

src/Tests/Behavioral/ExprSwitch/ExprSwitch.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,54 @@ private static void Main() {
163163
var matchꞥ2 = false;
164164
if (next is <= -1) { matchꞥ2 = true;
165165
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+
}
179+
}
180+
166181
}
167182
if (next is 0) { matchꞥ2 = true;
168183
fmt.Println("zero");
184+
{
185+
var nextɅ1 = getNext();
186+
var matchꞥ4 = false;
187+
if (nextɅ1 is 1 or <= 2) { matchꞥ4 = true;
188+
fmt.Println("sub1 one or two");
189+
}
190+
if (nextɅ1 is 3) { matchꞥ4 = true;
191+
fmt.Println("sub1 three");
192+
fallthrough = true;
193+
}
194+
if (fallthrough || !matchꞥ4) { /* default: */
195+
fmt.Println("sub1 default");
196+
}
197+
}
198+
169199
}
170200
if (next is 1 or 2) { matchꞥ2 = true;
171201
fmt.Println("one or two");
202+
switch (next) {
203+
case 1 or 2:
204+
fmt.Println("sub2 one or two");
205+
break;
206+
case 3:
207+
fmt.Println("sub2 three");
208+
break;
209+
default:
210+
fmt.Println("sub2 default");
211+
break;
212+
}
213+
172214
fallthrough = true;
173215
}
174216
if (fallthrough || next >= 3 && next < 100) { matchꞥ2 = true;

src/Tests/Behavioral/ExprSwitch/ExprSwitch.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,40 @@ func main() {
137137
switch next := getNext(); {
138138
case next <= -1:
139139
fmt.Println("negative")
140-
case next == 0:
140+
141+
switch getNext() {
142+
case 1, 2:
143+
fmt.Println("sub0 one or two")
144+
case 3:
145+
fmt.Println("sub0 three")
146+
fallthrough
147+
default:
148+
fmt.Println("sub0 default")
149+
}
150+
case next == 0:
141151
fmt.Println("zero")
152+
153+
switch next := getNext(); {
154+
case next == 1, next <= 2:
155+
fmt.Println("sub1 one or two")
156+
case next == 3:
157+
fmt.Println("sub1 three")
158+
fallthrough
159+
default:
160+
fmt.Println("sub1 default")
161+
}
142162
case next == 1, next == 2:
143163
fmt.Println("one or two")
164+
165+
switch next {
166+
case 1, 2:
167+
fmt.Println("sub2 one or two")
168+
case 3:
169+
fmt.Println("sub2 three")
170+
default:
171+
fmt.Println("sub2 default")
172+
}
173+
144174
fallthrough
145175
case next >= 3 && next < 100:
146176
fmt.Printf("three or greater < 100: %d\n", x)

src/go2cs2/visitStmt.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ func (v *Visitor) visitStmt(stmt ast.Stmt, parentBlock *ast.BlockStmt) {
1313
v.visitBlockStmt(stmtType, true, true)
1414
case *ast.BranchStmt:
1515
v.visitBranchStmt(stmtType)
16-
// CaseClause visited in visitSwitchStmt
17-
// case *ast.CaseClause:
18-
// v.visitCaseClause(stmtType)
1916
case *ast.CommClause:
2017
v.visitCommClause(stmtType)
2118
case *ast.DeclStmt:

src/gocore/golib/array.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public interface IArray : IEnumerable, ICloneable
4141

4242
bool IndexIsValid(nint index)
4343
{
44-
return index >= 0 && index < Length;
44+
return index > -1 && index < Length;
4545
}
4646
}
4747

src/gocore/golib/builtin.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ private static class Zero<T>
5757
public const nint iota = 0;
5858

5959
/// <summary>
60-
/// Defines a constant used to represent an untyped integer value.
61-
/// Commonly used for expression-based switch target / case values.
60+
/// Defines a constant used to represent an always true state.
6261
/// </summary>
63-
public const int = 0;
62+
/// <remarks>
63+
/// Common use is for expression-based switch values where focus is
64+
/// on <c>when</c> conditions and not the value itself.
65+
/// </remarks>
66+
public const bool = true;
6467

6568
/// <summary>
6669
/// Defines a constant to return a tuple that includes a boolean success indicator.

src/gocore/golib/ptr.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public ptr(in T value)
6767
internal ptr(IArray arrayRef, int index)
6868
{
6969
m_arrayRefIndex = (arrayRef, index);
70+
m_val = default!;
7071
}
7172

7273
/// <summary>
@@ -107,17 +108,16 @@ public ref T val
107108
/// <param name="index">Index of element to get pointer for.</param>
108109
/// <returns>Pointer to element at specified index.</returns>
109110
/// <exception cref="InvalidOperationException">Cannot get pointer element at index, type is not an array or slice.</exception>
111+
/// <exception cref="IndexOutOfRangeException">Index is out of range for array or slice.</exception>
110112
public ptr<Telem> at<Telem>(int index)
111113
{
112-
if (m_val is IArray<Telem> array)
113-
{
114-
if (!array.IndexIsValid(index))
115-
throw new IndexOutOfRangeException("Index is out of range for array or slice.");
116-
117-
return new ptr<Telem>(array, index);
118-
}
114+
if (m_val is not IArray<Telem> array)
115+
throw new InvalidOperationException("Cannot get pointer to element at index, type is not an array or slice.");
116+
117+
if (!array.IndexIsValid(index))
118+
throw new IndexOutOfRangeException("Index is out of range for array or slice.");
119119

120-
throw new InvalidOperationException("Cannot get pointer to element at index, type is not an array or slice.");
120+
return new ptr<Telem>(array, index);
121121
}
122122

123123
/// <inheritdoc />
@@ -206,7 +206,7 @@ public override int GetHashCode()
206206
return value == nil;
207207
}
208208

209-
public static bool operator !=(NilType nil, in ptr<T>? value)
209+
public static bool operator !=(NilType nil, ptr<T>? value)
210210
{
211211
return value != nil;
212212
}

0 commit comments

Comments
 (0)