diff options
-rw-r--r-- | CONTRIBUTING.md | 83 | ||||
-rw-r--r-- | build.zig | 3 | ||||
-rw-r--r-- | exercises/042_pointers4.zig | 11 | ||||
-rw-r--r-- | exercises/043_pointers5.zig | 40 | ||||
-rw-r--r-- | exercises/047_methods.zig | 6 | ||||
-rwxr-xr-x | patches/gollum.sh | 4 | ||||
-rw-r--r-- | patches/patches/042_pointers4.patch | 2 | ||||
-rw-r--r-- | patches/patches/043_pointers5.patch | 2 |
8 files changed, 93 insertions, 58 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index eb80688..7d15cc6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,79 +1,84 @@ # Contributing -By reading this document, you have already entered the Elite Hall of Ziglings -Maintenance! +By reading this document, you have already entered the Elite Hall +of Ziglings Maintenance! ## The Ziglings Audience -Ziglings is intended for programmers of all experience levels. No specific -language knowledge is expected. If you can install the current Zig snapshot, -setup a copy of Ziglings, and understand if/then/else, loops, and functions, -then you're ready. +Ziglings is intended for programmers of all experience levels. No +specific language knowledge is expected. Anyone who can install +the current Zig snapshot, setup a copy of Ziglings, and knows +common language building blocks (if/then/else, loops, and +functions) is ready or Ziglings. -Experience with strong typing, manual memory management, and certain language -constructs and idioms will greatly increase the speed at which you'll be able -to tackle each exercise. But speed isn't important, only learning is important. +Ziglings is intended to be completely self-contained. If you +can't solve an exercise from the information you've gleaned so +far from Ziglings, then the exercise probably needs some +additional work. Please file an issue! -Ziglings is intended to be completely self-contained. If you can't solve an -exercise from the information you've gleaned so far from Ziglings, then the -exercise probably needs some additional work. Please file an issue! - -If an example doesn't match a description or if something is unclear, please -file an issue! +If an example doesn't match a description or if something is +unclear, please file an issue! ## Spelling/Grammar -If you see any typos, please file an issue or make a pull request. +If you see any typos, please file an issue...or make a pull +request! -No mistake is too small. The Ziglings must be perfect. +No mistake is too small. The Ziglings must be perfect. :-) ## Ideas -If you have ideas for new lessons or a way Ziglings could be improved, don't -hesitate to file an issue. +If you have ideas for new lessons or a way Ziglings could be +improved, don't hesitate to file an issue. -I prefer to actually write all of the content myself at this time (part of the -reason I'm building Ziglings is to learn Zig myself!), but I'm always open to -ideas. +I prefer to write the bulk of the content myself at this time +(part of the reason I'm building Ziglings is to learn Zig +myself!), but I'm always open to ideas. ## Platforms and Zig Versions -Because it uses the Zig build system, Ziglings should work wherever Zig does. +Because it uses the Zig build system, Ziglings should work +wherever Zig does. -Since Ziglings is a Zig language learning resource, it tracks the current -development of Zig. +Since Ziglings is a Zig language learning resource, it tracks the +current development snapshots of Zig from the official website +downloads page. -If you run into an error in Ziglings due to language changes (and you have the -latest development build of Zig and the latest commit to Ziglings), that's a -bug! Please file an issue. +If you run into an error in Ziglings caused by breaking changes +in the latest development build of Zig, that's a new bug in +Ziglings. Please file an issue...or make a pull request! ## Formatting -All exercises are (or should be) formatted with `zig fmt`. +All exercises should conformt to `zig fmt`. I often forget to do +this. ## Pull Request Workflow -Ziglings uses the "standard" Github workflow as guided by the Web interface. -Specifically: +Ziglings uses the "standard" Github workflow as guided by the Web +interface. Specifically: * Fork this repository -* Create a branch from `main` for your work: `git checkout -b my-branch` +* Create a branch from `main` for your work: + `git checkout -b my-branch` * Make changes, commit them -* When your changes are ready for review, push your branch: `git push origin - my-branch` +* When your changes are ready for review, push your branch: + `git push origin my-branch` * Create a pull request from your branch to `ziglings/main` -* Your faithful Ziglings maintainer "ratfactor" (that's me!) will take a look - at your request ASAP -* Once the changes are reviewed, your request will be merged and eternal - Ziglings contributor glory is yours! +* Your faithful Ziglings maintainer "ratfactor" (that's me!) will + take a look at your request ASAP (we don't talk about May-July + 2022, LOL) +* Once the changes are reviewed, your request will be merged and + eternal Ziglings contributor glory is yours! ## The Secrets -If you want to peek at the secrets, take a look at the `patches/` directory. +If you want to peek at the secrets, take a look at the `patches/` +directory. @@ -240,7 +240,8 @@ const exercises = [_]Exercise{ }, .{ .main_file = "043_pointers5.zig", - .output = "Wizard (G:10 H:100 XP:20)", + .output = "Wizard (G:10 H:100 XP:20)\n Mentor: Wizard (G:10000 H:100 XP:2340)", + }, .{ .main_file = "044_quiz5.zig", diff --git a/exercises/042_pointers4.zig b/exercises/042_pointers4.zig index 261dbc1..359a2f1 100644 --- a/exercises/042_pointers4.zig +++ b/exercises/042_pointers4.zig @@ -1,6 +1,15 @@ // // Now let's use pointers to do something we haven't been -// able to do before: pass a value by reference to a function! +// able to do before: pass a value by reference to a function. +// +// Why would we wish to pass a pointer to an integer variable +// rather than the integer value itself? Because then we are +// allowed to *change* the value of the variable! +// +// +-----------------------------------------------+ +// | Pass by reference when you want to change the | +// | pointed-to value. Otherwise, pass the value. | +// +-----------------------------------------------+ // const std = @import("std"); diff --git a/exercises/043_pointers5.zig b/exercises/043_pointers5.zig index f3a9ca0..9e2fa6f 100644 --- a/exercises/043_pointers5.zig +++ b/exercises/043_pointers5.zig @@ -1,8 +1,7 @@ // -// Passing integer pointers around is generally not something you're going -// to do. Integers are cheap to copy. -// -// But you know what IS useful? Pointers to structs: +// As with integers, you can pass a pointer to a struct when you +// will wish to modify that struct. Pointers are also useful when +// you need to store a reference to a struct (a "link" to it). // // const Vertex = struct{ x: u32, y: u32, z: u32 }; // @@ -16,7 +15,8 @@ // YES: pv.x // NO: pv.*.x // -// We can write functions that take pointer arguments: +// We can write functions that take pointers to structs as +// arguments. This foo() function modifies struct v: // // fn foo(v: *Vertex) void { // v.x += 2; @@ -24,13 +24,13 @@ // v.z += 7; // } // -// And pass references to them: +// And call them like so: // // foo(&v1); // -// // Let's revisit our RPG example and make a printCharacter() function -// that takes a Character pointer. +// that takes a Character by reference and prints it...*and* +// prints a linked "mentor" Character, if there is one. // const std = @import("std"); @@ -44,19 +44,30 @@ const Class = enum { const Character = struct { class: Class, gold: u32, - health: u8 = 100, // <--- You can also provide fields a default value! + health: u8 = 100, // You can provide default values experience: u32, + + // I need to use the '?' here to allow for a null value. But + // I don't explain it until later. Please don't tell anyone. + mentor: ?*Character = null, }; pub fn main() void { - var glorp = Character{ + var mighty_krodor = Character{ + .class = Class.wizard, + .gold = 10000, + .experience = 2340, + }; + + var glorp = Character{ // Glorp! .class = Class.wizard, .gold = 10, .experience = 20, + .mentor = &mighty_krodor, // Glorp's mentor is the Mighty Krodor }; // FIX ME! - // Please pass our Character "glorp" to printCharacter(): + // Please pass Glorp to printCharacter(): printCharacter(???); } @@ -78,4 +89,11 @@ fn printCharacter(c: *Character) void { c.health, c.experience, }); + + // Checking an "optional" value and capturing it will be + // explained later (this pairs with the '?' mentioned above.) + if (c.mentor) |mentor| { + std.debug.print(" Mentor: ", .{}); + printCharacter(mentor); + } } diff --git a/exercises/047_methods.zig b/exercises/047_methods.zig index 048cfa0..a3ed220 100644 --- a/exercises/047_methods.zig +++ b/exercises/047_methods.zig @@ -10,7 +10,7 @@ // pub fn hello() void { // std.debug.print("Foo says hello!\n", .{}); // } -// } +// }; // // 2. A function that is a member of a struct is a "method" and is // called with the "dot syntax" like so: @@ -23,10 +23,10 @@ // const Bar = struct{ // number: u32, // -// pub fn printMe(self: *Bar) void { +// pub fn printMe(self: Bar) void { // std.debug.print("{}\n", .{self.number}); // } -// } +// }; // // (Actually, you can name the first parameter anything, but // please follow convention and use "self".) diff --git a/patches/gollum.sh b/patches/gollum.sh index 495aa44..74f7626 100755 --- a/patches/gollum.sh +++ b/patches/gollum.sh @@ -22,7 +22,9 @@ p=patches/patches/$f.patch if [ ! -f $b ]; then echo "No $f! We hates it!"; exit 1; fi if [ ! -f $a ]; then echo "No $a! Where is it? Where is the answer, precious?"; exit; fi -echo "Hissss!\tbefore: '$b'\n\t after: '$a'\n\t patch: '$p'\n" +echo "Hissss! before: '$b'" +echo " after: '$a'" +echo " patch: '$p'" diff $b $a > $p diff --git a/patches/patches/042_pointers4.patch b/patches/patches/042_pointers4.patch index 29ca2d0..8e21b81 100644 --- a/patches/patches/042_pointers4.patch +++ b/patches/patches/042_pointers4.patch @@ -1,4 +1,4 @@ -31c31 +40c40 < ??? = 5; // fix me! --- > x.* = 5; // fix me! diff --git a/patches/patches/043_pointers5.patch b/patches/patches/043_pointers5.patch index 8a73551..ac6a8ca 100644 --- a/patches/patches/043_pointers5.patch +++ b/patches/patches/043_pointers5.patch @@ -1,4 +1,4 @@ -60c60 +71c71 < printCharacter(???); --- > printCharacter(&glorp); |