mirror of
https://codeberg.org/ziglings/exercises.git
synced 2025-09-20 22:55:38 +00:00
Compare commits
25 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
62ad50b71e | ||
![]() |
ed93882b19 | ||
![]() |
a4c1774847 | ||
![]() |
e12ee5cb9e | ||
![]() |
0dbe83b20e | ||
![]() |
e422e24215 | ||
![]() |
4134d4fa6f | ||
![]() |
ad361c4b18 | ||
![]() |
d4215ce5c6 | ||
![]() |
fd9e9aa36e | ||
![]() |
b3178e81a8 | ||
![]() |
c36fe8007d | ||
![]() |
7488727625 | ||
![]() |
a46db7e0e8 | ||
![]() |
564ea3405d | ||
![]() |
16ec207471 | ||
![]() |
04857aa75e | ||
![]() |
6364087569 | ||
![]() |
ad32e402a5 | ||
![]() |
470f695968 | ||
![]() |
5b3b0eb26b | ||
![]() |
0661d4fd48 | ||
![]() |
02db832677 | ||
![]() |
06e56be9c5 | ||
![]() |
f5d2c5124c |
|
@ -12,10 +12,10 @@ the current Zig snapshot, setup a copy of Ziglings, and knows
|
||||||
common language building blocks (if/then/else, loops, and
|
common language building blocks (if/then/else, loops, and
|
||||||
functions) is ready for Ziglings.
|
functions) is ready for Ziglings.
|
||||||
|
|
||||||
Ziglings is intended to be completely self-contained. If you
|
Zigling's excercises are self-contained. If you can't solve
|
||||||
can't solve an exercise from the information you've gleaned so
|
an exercise from the information you've gleaned so far from
|
||||||
far from Ziglings, then the exercise probably needs some
|
Ziglings, then the exercise probably needs some additional work.
|
||||||
additional work. Please file an issue!
|
Please file an issue!
|
||||||
|
|
||||||
If an example doesn't match a description or if something is
|
If an example doesn't match a description or if something is
|
||||||
unclear, please file an issue!
|
unclear, please file an issue!
|
||||||
|
@ -52,6 +52,8 @@ 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
|
in the latest development build of Zig, that's a new bug in
|
||||||
Ziglings. Please file an issue...or make a pull request!
|
Ziglings. Please file an issue...or make a pull request!
|
||||||
|
|
||||||
|
For the latter, also read "The Secrets” section.
|
||||||
|
|
||||||
|
|
||||||
## Formatting
|
## Formatting
|
||||||
|
|
||||||
|
@ -89,3 +91,23 @@ contribution for any other purpose.
|
||||||
|
|
||||||
If you want to peek at the secrets, take a look at the `patches/`
|
If you want to peek at the secrets, take a look at the `patches/`
|
||||||
directory.
|
directory.
|
||||||
|
|
||||||
|
Every Ziglings exercise contains mistakes on purpose.
|
||||||
|
To keep our automated tests happy, each exercise also
|
||||||
|
has a patch in `patches/healed` that “heals” it.
|
||||||
|
|
||||||
|
When you change an exercise, you will usually need to update
|
||||||
|
its patch too. That’s where our little helper Gollum comes in:
|
||||||
|
|
||||||
|
1. In the project root, create a folder called `answers/`
|
||||||
|
2. Put your solved version of the exercise file in there
|
||||||
|
3. Back in the root, run:
|
||||||
|
`./patches/gollum <exercise-number>`<br>
|
||||||
|
For example: `./patches/gollum 106`
|
||||||
|
This will generate a shiny new patch.
|
||||||
|
|
||||||
|
Double-check everything by asking the magical Eowyn:
|
||||||
|
`./patches/eowyn`<br>
|
||||||
|
If all tests pass: You are done!
|
||||||
|
|
||||||
|
Don’t forget to commit the patch file.
|
||||||
|
|
10
README.md
10
README.md
|
@ -25,10 +25,9 @@ language such as C.
|
||||||
|
|
||||||
Each exercise is self-contained and self-explained. However,
|
Each exercise is self-contained and self-explained. However,
|
||||||
you're encouraged to also check out these Zig language resources
|
you're encouraged to also check out these Zig language resources
|
||||||
for more detail:
|
for more details:
|
||||||
|
|
||||||
* https://ziglang.org/learn/
|
* https://ziglang.org/learn/
|
||||||
* https://ziglearn.org/
|
|
||||||
* https://ziglang.org/documentation/master/
|
* https://ziglang.org/documentation/master/
|
||||||
* [Zig in Depth! (video series)](https://www.youtube.com/watch?v=MMtvGA1YhW4&list=PLtB7CL7EG7pCw7Xy1SQC53Gl8pI7aDg9t&pp=iAQB)
|
* [Zig in Depth! (video series)](https://www.youtube.com/watch?v=MMtvGA1YhW4&list=PLtB7CL7EG7pCw7Xy1SQC53Gl8pI7aDg9t&pp=iAQB)
|
||||||
|
|
||||||
|
@ -45,7 +44,7 @@ Verify the installation and build number of `zig` like so:
|
||||||
|
|
||||||
```
|
```
|
||||||
$ zig version
|
$ zig version
|
||||||
0.15.0-dev.xxxx+xxxxxxxxx
|
0.16.0-dev.xxxx+xxxxxxxxx
|
||||||
```
|
```
|
||||||
|
|
||||||
Clone this repository with Git:
|
Clone this repository with Git:
|
||||||
|
@ -73,8 +72,8 @@ the appropriate tag.
|
||||||
The Zig language is under very active development. In order to be
|
The Zig language is under very active development. In order to be
|
||||||
current, Ziglings tracks **development** builds of the Zig
|
current, Ziglings tracks **development** builds of the Zig
|
||||||
compiler rather than versioned **release** builds. The last
|
compiler rather than versioned **release** builds. The last
|
||||||
stable release was `0.14.1`, but Ziglings needs a dev build with
|
stable release was `0.15.1`, but Ziglings needs a dev build with
|
||||||
pre-release version "0.15.0" and a build number at least as high
|
pre-release version "0.16.0" and a build number at least as high
|
||||||
as that shown in the example version check above.
|
as that shown in the example version check above.
|
||||||
|
|
||||||
It is likely that you'll download a build which is _greater_ than
|
It is likely that you'll download a build which is _greater_ than
|
||||||
|
@ -87,6 +86,7 @@ that if you update one, you may need to also update the other.
|
||||||
|
|
||||||
### Version Changes
|
### Version Changes
|
||||||
|
|
||||||
|
* *2025-09-03* zig 0.16.0-dev.164 - changes in reader, see [#25077](https://github.com/ziglang/zig/pull/25077)
|
||||||
* *2025-08-15* zig 0.15.0-dev.1519 - changes in array list, see [#24801](https://github.com/ziglang/zig/pull/24801)
|
* *2025-08-15* zig 0.15.0-dev.1519 - changes in array list, see [#24801](https://github.com/ziglang/zig/pull/24801)
|
||||||
* *2025-08-08* zig 0.15.0-dev.1380 - changes in build system, see [#24588](https://github.com/ziglang/zig/pull/24588)
|
* *2025-08-08* zig 0.15.0-dev.1380 - changes in build system, see [#24588](https://github.com/ziglang/zig/pull/24588)
|
||||||
* *2025-07-22* zig 0.15.0-dev.1092 - various changes due to new I/O API, see [#24488](https://github.com/ziglang/zig/pull/24488)
|
* *2025-07-22* zig 0.15.0-dev.1092 - various changes due to new I/O API, see [#24488](https://github.com/ziglang/zig/pull/24488)
|
||||||
|
|
14
build.zig
14
build.zig
|
@ -15,7 +15,7 @@ const print = std.debug.print;
|
||||||
// 1) Getting Started
|
// 1) Getting Started
|
||||||
// 2) Version Changes
|
// 2) Version Changes
|
||||||
comptime {
|
comptime {
|
||||||
const required_zig = "0.15.0-dev.1519";
|
const required_zig = "0.16.0-dev.164";
|
||||||
const current_zig = builtin.zig_version;
|
const current_zig = builtin.zig_version;
|
||||||
const min_zig = std.SemanticVersion.parse(required_zig) catch unreachable;
|
const min_zig = std.SemanticVersion.parse(required_zig) catch unreachable;
|
||||||
if (current_zig.order(min_zig) == .lt) {
|
if (current_zig.order(min_zig) == .lt) {
|
||||||
|
@ -276,8 +276,12 @@ pub fn build(b: *Build) !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
defer _ = gpa.deinit();
|
defer _ = gpa.deinit();
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
const contents = try progress_file.readToEndAlloc(allocator, progress_file_size);
|
const contents = try allocator.alloc(u8, progress_file_size);
|
||||||
defer allocator.free(contents);
|
defer allocator.free(contents);
|
||||||
|
const bytes_read = try progress_file.read(contents);
|
||||||
|
if (bytes_read != progress_file_size) {
|
||||||
|
return error.UnexpectedEOF;
|
||||||
|
}
|
||||||
|
|
||||||
starting_exercise = try std.fmt.parseInt(u32, contents, 10);
|
starting_exercise = try std.fmt.parseInt(u32, contents, 10);
|
||||||
} else |err| {
|
} else |err| {
|
||||||
|
@ -508,6 +512,10 @@ const ZiglingStep = struct {
|
||||||
zig_args.append("-lc") catch @panic("OOM");
|
zig_args.append("-lc") catch @panic("OOM");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (b.reference_trace) |rt| {
|
||||||
|
zig_args.append(b.fmt("-freference-trace={}", .{rt})) catch @panic("OOM");
|
||||||
|
}
|
||||||
|
|
||||||
zig_args.append(b.pathFromRoot(path)) catch @panic("OOM");
|
zig_args.append(b.pathFromRoot(path)) catch @panic("OOM");
|
||||||
|
|
||||||
zig_args.append("--cache-dir") catch @panic("OOM");
|
zig_args.append("--cache-dir") catch @panic("OOM");
|
||||||
|
@ -562,7 +570,7 @@ const ZiglingStep = struct {
|
||||||
|
|
||||||
// Render compile errors at the bottom of the terminal.
|
// Render compile errors at the bottom of the terminal.
|
||||||
// TODO: use the same ttyconf from the builder.
|
// TODO: use the same ttyconf from the builder.
|
||||||
const ttyconf: std.io.tty.Config = if (use_color_escapes)
|
const ttyconf: std.Io.tty.Config = if (use_color_escapes)
|
||||||
.escape_codes
|
.escape_codes
|
||||||
else
|
else
|
||||||
.no_color;
|
.no_color;
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
// example that takes two parameters. As you can see, parameters
|
// example that takes two parameters. As you can see, parameters
|
||||||
// are declared just like any other types ("name": "type"):
|
// are declared just like any other types ("name": "type"):
|
||||||
//
|
//
|
||||||
// fn myFunction(number: u8, is_lucky: bool) {
|
// fn myFunction(number: u8, is_lucky: bool) void {
|
||||||
// ...
|
// ...
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
|
|
|
@ -58,7 +58,7 @@ test "add" {
|
||||||
|
|
||||||
// Another way to perform this test
|
// Another way to perform this test
|
||||||
// is as follows:
|
// is as follows:
|
||||||
try testing.expectEqual(add(41, 1), 42);
|
try testing.expectEqual(42, add(41, 1));
|
||||||
|
|
||||||
// This time a test with the addition
|
// This time a test with the addition
|
||||||
// of a negative number:
|
// of a negative number:
|
||||||
|
|
|
@ -63,8 +63,9 @@ pub fn main() !void {
|
||||||
}
|
}
|
||||||
// to check if you actually write to the file, you can either,
|
// to check if you actually write to the file, you can either,
|
||||||
// 1. open the file in your text editor, or
|
// 1. open the file in your text editor, or
|
||||||
// 2. print the content of the file in the console with the following command
|
// 2. print the content of the file in the console with one of these commands
|
||||||
// >> cat ./output/zigling.txt
|
// Linux/macOS: >> cat ./output/zigling.txt
|
||||||
|
// Windows (CMD): >> type .\output\zigling.txt
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// More on Creating files
|
// More on Creating files
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
// Prerequisite :
|
// Prerequisite :
|
||||||
// - exercise/106_files.zig, or
|
// - exercise/106_files.zig, or
|
||||||
// - create a file {project_root}/output/zigling.txt
|
// - create a file {project_root}/output/zigling.txt
|
||||||
// with content `It's zigling time!`(18 byte total)
|
// with content `It's zigling time!`(18 bytes total)
|
||||||
//
|
//
|
||||||
// Now there's no point in writing to a file if we don't read from it, am I right?
|
// Now there's no point in writing to a file if we don't read from it, am I right?
|
||||||
// Let's write a program to read the content of the file that we just created.
|
// Let's write a program to read the content of the file that we just created.
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
--- exercises/106_files.zig 2025-03-13 15:26:59.532367792 +0200
|
--- exercises/106_files.zig 2025-08-24 19:23:55.168565003 +0200
|
||||||
+++ answers/106_files.zig 2025-03-14 22:04:52.243435159 +0200
|
+++ answers/106_files.zig 2025-08-24 19:25:37.745597511 +0200
|
||||||
@@ -35,7 +35,7 @@
|
@@ -35,7 +35,7 @@
|
||||||
// by doing nothing
|
// by doing nothing
|
||||||
//
|
//
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
--- exercises/107_files2.zig 2025-03-13 15:26:59.532367792 +0200
|
--- exercises/107_files2.zig 2025-08-24 19:15:17.789371332 +0200
|
||||||
+++ answers/107_files2.zig 2025-03-14 22:08:35.167953736 +0200
|
+++ answers/107_files2.zig 2025-08-24 19:17:58.897538288 +0200
|
||||||
@@ -33,7 +33,7 @@
|
@@ -33,7 +33,7 @@
|
||||||
// initialize an array of u8 with all letter 'A'
|
// initialize an array of u8 with all letter 'A'
|
||||||
// we need to pick the size of the array, 64 seems like a good number
|
// we need to pick the size of the array, 64 seems like a good number
|
||||||
|
|
Loading…
Reference in New Issue
Block a user