From 738a9f6cda62f3570e44dc93f8e200afc2cc1b51 Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Wed, 3 Feb 2021 19:19:31 -0500 Subject: [PATCH] Inserted ex. 32 unreachable, added quiz4. --- 32_unreachable.zig | 38 ++++++++++++++++++++++++++++++++ 32_iferror.zig => 33_iferror.zig | 0 34_quiz4.zig | 24 ++++++++++++++++++++ README.md | 6 ++--- ziglings | 5 +++-- 5 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 32_unreachable.zig rename 32_iferror.zig => 33_iferror.zig (100%) create mode 100644 34_quiz4.zig diff --git a/32_unreachable.zig b/32_unreachable.zig new file mode 100644 index 0000000..c81efac --- /dev/null +++ b/32_unreachable.zig @@ -0,0 +1,38 @@ +// +// Zig has an "unreachable" statement. Use it when you want to tell the +// compiler that a branch of code should never be executed and that the +// mere act of reaching it is an error. +// +// if (true) { +// ... +// } else { +// unreachable; +// } +// +// Here we've made a little virtual machine that performs mathematical +// operations on a single numeric value. It looks great but there's one +// little problem: the switch statement doesn't cover every possible +// value of a u8 number! +// +// WE know there are only three operations but Zig doesn't. Use the +// unreachable statement to make the switch complete. Or ELSE. :-) +// +const std = @import("std"); + +pub fn main() void { + const operations = [_]u8{ 1, 1, 1, 3, 2, 2 }; + + var current_value: u32 = 0; + + for (operations) |op| { + switch (op) { + 1 => { current_value += 1; }, + 2 => { current_value -= 1; }, + 3 => { current_value *= current_value; }, + } + + std.debug.print("{} ", .{current_value}); + } + + std.debug.print("\n", .{}); +} diff --git a/32_iferror.zig b/33_iferror.zig similarity index 100% rename from 32_iferror.zig rename to 33_iferror.zig diff --git a/34_quiz4.zig b/34_quiz4.zig new file mode 100644 index 0000000..43734b7 --- /dev/null +++ b/34_quiz4.zig @@ -0,0 +1,24 @@ +// +// Quiz time. See if you can make this program work! +// +// Solve this any way you like, just be sure the output is: +// +// my_num=42 +// +const std = @import("std"); + +const NumError = error{ IllegalNumber }; + +pub fn main() void { + const stdout = std.io.getStdOut().writer(); + + const my_num: u32 = getNumber(); + + try stdout.print("my_num={}\n", .{my_num}); +} + +// Just don't modify this function. It's "perfect" the way it is. :-) +fn getNumber() NumError!u32 { + if( false ) return NumError.IllegalNumber; + return 42; +} diff --git a/README.md b/README.md index 49adc3e..f8b16d5 100644 --- a/README.md +++ b/README.md @@ -66,10 +66,10 @@ Planned exercises: * [x] While * [x] For * [x] Functions -* [x] Errors -* [x] Defer +* [x] Errors (error/try/catch/if-else-err) +* [x] Defer (and errdefer) * [x] Switch -* [ ] Unreachable +* [x] Unreachable * [ ] Pointers * [ ] Pointer sized integers * [ ] Multi pointers diff --git a/ziglings b/ziglings index 8570f28..be2e829 100755 --- a/ziglings +++ b/ziglings @@ -99,8 +99,9 @@ check_it 28_defer2.zig "(Goat) (Cat) (Dog) (Dog) (Goat) (Unknown) done." check_it 29_errdefer.zig "Getting number...got 5. Getting number...failed!" check_it 30_switch.zig "ZIG?" check_it 31_switch2.zig "ZIG!" -check_it 32_iferror.zig "2<4. 3<4. 4=4. 5>4. 6>4." "Seriously, what's the deal with fours?" -#check_it 33_quiz4.zig "foo" "Can you make this work?" +check_it 32_unreachable.zig "1 2 3 9 8 7" +check_it 33_iferror.zig "2<4. 3<4. 4=4. 5>4. 6>4." "Seriously, what's the deal with fours?" +check_it 34_quiz4.zig "my_num=42" "Can you make this work?" echo echo " __ __ _ "