forked from ziglang/zig
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This feature was proposed in ziglang#8220, and implemented in ziglang#21257.
- Loading branch information
1 parent
8ec68c6
commit dbc3a8f
Showing
3 changed files
with
87 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,22 @@ | ||
const std = @import("std"); | ||
const expectEqual = std.testing.expectEqual; | ||
|
||
test "switch continue" { | ||
const value: i32 = 54; | ||
const result = sw: switch (value) { | ||
10...60 => |v| continue :sw v - 10, | ||
4 => continue :sw 3, | ||
3 => continue :sw 2, | ||
2 => continue :sw 1, | ||
|
||
// A switch statement can be targeted by a break, even if the switch and | ||
// the break are unlabeled. | ||
1 => break -6, | ||
|
||
else => unreachable, | ||
}; | ||
|
||
try expectEqual(-6, result); | ||
} | ||
|
||
// 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,52 @@ | ||
const std = @import("std"); | ||
const expectEqual = std.testing.expectEqual; | ||
|
||
const Instruction = enum { | ||
mul, | ||
add, | ||
end, | ||
}; | ||
|
||
test "switch dispatch loop" { | ||
var stack = std.ArrayList(i32).init(std.testing.allocator); | ||
defer stack.deinit(); | ||
|
||
try stack.append(5); | ||
try stack.append(1); | ||
try stack.append(-1); | ||
|
||
const instructions: []const Instruction = &.{ | ||
.mul, .add, .end, | ||
}; | ||
|
||
var ip: usize = 0; | ||
|
||
const result = vm: switch (instructions[ip]) { | ||
// Because this prong always `continue`s, it's not required to produce | ||
// a result. | ||
.add => { | ||
const l = stack.pop(); | ||
const r = stack.pop(); | ||
|
||
try stack.append(l + r); | ||
|
||
ip += 1; | ||
continue :vm instructions[ip]; | ||
}, | ||
.mul => { | ||
const l = stack.pop(); | ||
const r = stack.pop(); | ||
|
||
try stack.append(l * r); | ||
|
||
ip += 1; | ||
continue :vm instructions[ip]; | ||
}, | ||
.end => stack.pop(), | ||
}; | ||
|
||
try expectEqual(4, result); | ||
} | ||
|
||
// test | ||
|