-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Labeled switch documentation #21383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andrewrk
merged 20 commits into
ziglang:master
from
LiterallyVoid:literallyvoid/21375-labeled-switch-langref
Sep 13, 2024
Merged
Labeled switch documentation #21383
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
32c9b09
Add langref docs for labeled switch
LiterallyVoid b069396
Fix inaccurate comment
LiterallyVoid 6b33e7a
Clarify labeled switch semantics in langref
LiterallyVoid 6e4ad3c
Add clarification to langref for semantics vs performance
LiterallyVoid e734ee1
Wrap at 80 columns
LiterallyVoid 424cb69
Move semantics note below test
LiterallyVoid d92450e
More wording from the release notes
LiterallyVoid 2efad44
Move comment to first example
LiterallyVoid 2216489
Grammar?
LiterallyVoid df5e104
Use clearer wording and describe `break`
LiterallyVoid feb45ed
Shorten switch continue example
LiterallyVoid e7d8fe7
Re-add comment
LiterallyVoid 5158db4
typo: Re-order enum for consistency
LiterallyVoid 177ec04
typo: Tiny wording adjustments
LiterallyVoid 826a58e
Compact dispatch loop example as far as it'll go
LiterallyVoid 9dfdcb3
Remove unused import from switch continue tests
LiterallyVoid a109fd9
Rename switch continue equivalent example
LiterallyVoid 01dced6
Change switch continue equivalent test name
LiterallyVoid a427a72
Add text about state machines and intended use
LiterallyVoid de07d8b
Update doc/langref.html.in
andrewrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.