-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Labeled switch documentation (#21383)
Add langref docs for labeled switch This feature was proposed in #8220, and implemented in #21257. Co-authored-by: Andrew Kelley <andrew@ziglang.org>
- Loading branch information
1 parent
e17dfb9
commit cf69154
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const std = @import("std"); | ||
|
||
test "switch continue" { | ||
sw: switch (@as(i32, 5)) { | ||
5 => continue :sw 4, | ||
|
||
// `continue` can occur multiple times within a single switch prong. | ||
2...4 => |v| { | ||
if (v > 3) { | ||
continue :sw 2; | ||
} else if (v == 3) { | ||
|
||
// `break` can target labeled loops. | ||
break :sw; | ||
} | ||
|
||
continue :sw 1; | ||
}, | ||
|
||
1 => return, | ||
|
||
else => unreachable, | ||
} | ||
} | ||
|
||
// test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const std = @import("std"); | ||
|
||
test "switch continue, equivalent loop" { | ||
var sw: i32 = 5; | ||
while (true) { | ||
switch (sw) { | ||
5 => { | ||
sw = 4; | ||
continue; | ||
}, | ||
2...4 => |v| { | ||
if (v > 3) { | ||
sw = 2; | ||
continue; | ||
} else if (v == 3) { | ||
break; | ||
} | ||
|
||
sw = 1; | ||
continue; | ||
}, | ||
1 => return, | ||
else => unreachable, | ||
} | ||
} | ||
} | ||
|
||
// test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const std = @import("std"); | ||
const expectEqual = std.testing.expectEqual; | ||
|
||
const Instruction = enum { | ||
add, | ||
mul, | ||
end, | ||
}; | ||
|
||
fn evaluate(initial_stack: []const i32, code: []const Instruction) !i32 { | ||
var stack = try std.BoundedArray(i32, 8).fromSlice(initial_stack); | ||
var ip: usize = 0; | ||
|
||
return vm: switch (code[ip]) { | ||
// Because all code after `continue` is unreachable, this branch does | ||
// not provide a result. | ||
.add => { | ||
try stack.append(stack.pop() + stack.pop()); | ||
|
||
ip += 1; | ||
continue :vm code[ip]; | ||
}, | ||
.mul => { | ||
try stack.append(stack.pop() * stack.pop()); | ||
|
||
ip += 1; | ||
continue :vm code[ip]; | ||
}, | ||
.end => stack.pop(), | ||
}; | ||
} | ||
|
||
test "evaluate" { | ||
const result = try evaluate(&.{ 7, 2, -3 }, &.{ .mul, .add, .end }); | ||
try expectEqual(1, result); | ||
} | ||
|
||
// test |