Add ex 35,36 enums; updated README

I'm changing the order of some more topics. Trying to explain the value
of pointers when we're mostly dealing with stack-sized values like
integers feels convoluted. So I'm starting with enums (which also has a
nice segue from an earlier "switch" exercise). Then structs. Then unions
(just in keeping with the order of these items on ziglearn.org) and THEN
pointers and multi-pointers and slices.
This commit is contained in:
Dave Gauer 2021-02-06 09:29:49 -05:00
parent 738a9f6cda
commit 2cded107cd
4 changed files with 131 additions and 5 deletions

49
35_enums.zig Normal file
View File

@ -0,0 +1,49 @@
//
// Remember that little mathematical virtual machine we made using the
// "unreachable" statement? Well, there were two problems with the
// way we were using op codes:
//
// 1. Having to remember op codes by number is no good.
// 2. We had to use "unreachable" because Zig had no way of knowing
// how many valid op codes there were.
//
// An "enum" is a Zig construct that lets you give names to numeric
// values and store them in a set. They look a lot like error sets:
//
// const Fruit = enum{ apple, pear, orange };
//
// const my_fruit = Fruit.apple;
//
// Let's use an enum in place of the numbers we were using in the
// previous version!
//
const std = @import("std");
// Please complete the enum!
const Ops = enum{ ??? };
pub fn main() void {
const operations = [_]Ops{
Ops.inc,
Ops.inc,
Ops.inc,
Ops.pow,
Ops.dec,
Ops.dec
};
var current_value: u32 = 0;
for (operations) |op| {
switch (op) {
Ops.inc => { current_value += 1; },
Ops.dec => { current_value -= 1; },
Ops.pow => { current_value *= current_value; },
// No "else" needed! Why is that?
}
std.debug.print("{} ", .{current_value});
}
std.debug.print("\n", .{});
}

61
36_enums2.zig Normal file
View File

@ -0,0 +1,61 @@
//
// Enums are really just a set of numbers. You can leave the
// numbering up to the compiler, or you can assign them
// explicitly. You can even specify the numeric type used.
//
// const Stuff = enum(u8){ foo = 16 };
//
// You can get the integer out with a built-in function:
//
// var my_stuff: u8 = @enumToInt(Stuff.foo);
//
// Note how that built-in function starts with "@" just like the
// @import() function we've been using.
//
const std = @import("std");
// Zig lets us write integers in hexadecimal format:
//
// 0xf (is the value 15 in hex)
//
// Web browsers let us specify colors using a hexadecimal
// number where each byte represents the brightness of the
// Red, Green, or Blue component (RGB) where two hex digits
// are one byte with a value range of 0-255:
//
// #RRGGBB
//
// Please define and use a pure blue value Color:
const Color = enum(u32){
red = 0xff0000,
green = 0x00ff00,
blue = ???,
};
pub fn main() void {
// Remeber Zig's multi-line strings? Here they are again.
// Also, check out this cool format string:
//
// {x:0>6}
// ^
// x type ('x' is lower-case hexadecimal)
// : separator (needed for format syntax)
// 0 padding character (default is ' ')
// > alignment ('>' aligns right)
// 6 width (use padding to force width)
//
// Please add this formatting to the blue value.
// (Even better, experiment without it, or try parts of it
// to see what prints!)
std.debug.print(
\\<p>
\\ <span style="color: #{x:0>6}">Red</span>
\\ <span style="color: #{x:0>6}">Green</span>
\\ <span style="color: #{}">Blue</span>
\\</p>
, .{
@enumToInt(Color.red),
@enumToInt(Color.green),
@enumToInt(???), // Oops! We're missing something!
});
}

View File

@ -9,6 +9,18 @@ This project was directly inspired by the brilliant and fun
[rustlings](https://github.com/rust-lang/rustlings)
project for the [Rust](https://www.rust-lang.org/) language.
## Intended Audience
This will probably be quite difficult if you've _never_ programmed before.
However, no specific programming experience is required. And in particular,
you are _not_ expected to know C or other "systems programming" language.
Each exercise is self-contained and self-explained. However, you're encouraged
to also check out these Zig language resources for more detail:
* https://ziglearn.org/
* https://ziglang.org/documentation/master/
## Getting Started
_Note: This currently uses a shell (Bash) script to automate the "game". A
@ -53,7 +65,7 @@ the learning resource I wished for. There will be tons of room for improvement:
* Wording of explanations
* Idiomatic usage of Zig
* Additional exercises
* Re-write the `ziglings` script using the Zig build system (???)
* Re-write the `ziglings` script using the Zig build system (or just a Zig application)
Planned exercises:
@ -70,13 +82,13 @@ Planned exercises:
* [x] Defer (and errdefer)
* [x] Switch
* [x] Unreachable
* [x] Enums
* [ ] Structs
* [ ] Unions
* [ ] Pointers
* [ ] Pointer sized integers
* [ ] Multi pointers
* [ ] Slices
* [ ] Enums
* [ ] Structs
* [ ] Unions
* [ ] Integer rules
* [ ] Floats
* [ ] Labelled blocks
@ -91,4 +103,6 @@ Planned exercises:
* [ ] Imports
The initial topics for these exercises were unabashedly cribbed from
[ziglearn.org](https://ziglearn.org/).
[ziglearn.org](https://ziglearn.org/). I've since moved things around
in an order that I think best lets each topic build upon each other.

View File

@ -102,6 +102,8 @@ check_it 31_switch2.zig "ZIG!"
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?"
check_it 35_enums.zig "1 2 3 9 8 7" "This problem seems familiar..."
check_it 36_enums2.zig "#0000ff" "I'm feeling blue about this."
echo
echo " __ __ _ "