aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-02-03 19:19:31 -0500
committerDave Gauer <dave@ratfactor.com>2021-02-03 19:19:31 -0500
commit738a9f6cda62f3570e44dc93f8e200afc2cc1b51 (patch)
tree0f690aeaee95d317d80ee3913b0fb0b6245b1edb
parentcd80aeb19061b9d222d24cdb4d0764a210536531 (diff)
downloadziglings-738a9f6cda62f3570e44dc93f8e200afc2cc1b51.tar.gz
ziglings-738a9f6cda62f3570e44dc93f8e200afc2cc1b51.tar.bz2
ziglings-738a9f6cda62f3570e44dc93f8e200afc2cc1b51.tar.xz
ziglings-738a9f6cda62f3570e44dc93f8e200afc2cc1b51.zip
Inserted ex. 32 unreachable, added quiz4.
-rw-r--r--32_unreachable.zig38
-rw-r--r--33_iferror.zig (renamed from 32_iferror.zig)0
-rw-r--r--34_quiz4.zig24
-rw-r--r--README.md6
-rwxr-xr-xziglings5
5 files changed, 68 insertions, 5 deletions
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
index ed92e94..ed92e94 100644
--- a/32_iferror.zig
+++ b/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 " __ __ _ "