summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/ci.yml7
-rw-r--r--.github/workflows/eowyn.yml8
-rw-r--r--README.md10
-rw-r--r--build.zig1199
-rw-r--r--exercises/015_for.zig6
-rw-r--r--exercises/016_for2.zig6
-rw-r--r--exercises/095_for3.zig73
-rw-r--r--exercises/095_for_loops.zig64
-rw-r--r--exercises/100_for4.zig62
-rw-r--r--exercises/101_for5.zig120
-rwxr-xr-xpatches/eowyn.sh7
-rw-r--r--patches/patches/095_for3.patch (renamed from patches/patches/095_for_loops.patch)2
-rw-r--r--patches/patches/100_for4.patch4
-rw-r--r--patches/patches/101_for5.patch4
-rw-r--r--src/compat.zig2
-rw-r--r--test/tests.zig64
16 files changed, 953 insertions, 685 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f40f279..d071d8f 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1,5 +1,4 @@
-name: 'CI'
-# Makes some checks
+name: CI
on:
pull_request:
@@ -27,9 +26,9 @@ jobs:
- name: Check compatibility with old Zig compilers
run: ci/compat.sh
-
+
test:
- name: Unit Test
+ name: Unit Tests
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
diff --git a/.github/workflows/eowyn.yml b/.github/workflows/eowyn.yml
index 1b94a48..3ac48f7 100644
--- a/.github/workflows/eowyn.yml
+++ b/.github/workflows/eowyn.yml
@@ -1,5 +1,5 @@
-name: 'Eowyn'
-# Tests all exercises
+# Test that exercises work with the latest Zig compiler.
+name: Eowyn
on:
pull_request:
@@ -22,11 +22,11 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
-
+
- name: Setup Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: master
-
+
- name: Run Eowyn
run: patches/eowyn.sh
diff --git a/README.md b/README.md
index 554cbbe..dff3f2d 100644
--- a/README.md
+++ b/README.md
@@ -40,7 +40,7 @@ Verify the installation and build number of `zig` like so:
```
$ zig version
-0.11.0-dev.2560+xxxxxxxxx
+0.11.0-dev.2704+xxxxxxxxx
```
Clone this repository with Git:
@@ -74,13 +74,12 @@ Once you have a build of the Zig compiler that works with Ziglings, they'll
continue to work together. But keep in mind that if you update one, you may
need to also update the other.
-Also note that the current "stage 1" Zig compiler is very strict
-about input:
-[no tab characters or Windows CR/LF newlines are allowed](https://github.com/ziglang/zig/issues/544).
### Version Changes
-Version-0.11.0-dev.2560+602029bb2
+Version-0.11.0-dev.2704+83970b6d9
+* *2023-04-30* zig 0.11.0-dev.2704 - use of the new `std.Build.ExecutableOptions.link_libc` field
+* *2023-04-12* zig 0.11.0-dev.2560 - changes in `std.Build` - remove run() and install()
* *2023-04-07* zig 0.11.0-dev.2401 - fixes of the new build system - see [#212](https://github.com/ratfactor/ziglings/pull/212)
* *2023-02-21* zig 0.11.0-dev.2157 - changes in `build system` - new: parallel processing of the build steps
* *2023-02-21* zig 0.11.0-dev.1711 - changes in `for loops` - new: Multi-Object For-Loops + Struct-of-Arrays
@@ -196,6 +195,7 @@ Zig Core Language
Zig Standard Library
* [X] String formatting
+* [ ] Testing
## Contributing
diff --git a/build.zig b/build.zig
index 398f02e..64968b9 100644
--- a/build.zig
+++ b/build.zig
@@ -5,10 +5,12 @@ const ipc = @import("src/ipc.zig");
const tests = @import("test/tests.zig");
const Build = compat.Build;
+const CompileStep = compat.build.CompileStep;
const Step = compat.build.Step;
const Child = std.process.Child;
const assert = std.debug.assert;
+const join = std.fs.path.join;
const print = std.debug.print;
pub const Exercise = struct {
@@ -29,21 +31,16 @@ pub const Exercise = struct {
/// Set this to true to check stdout instead.
check_stdout: bool = false,
- /// This exercise makes use of the async feature.
- /// We need to keep track of this, so we compile without the self hosted compiler
- @"async": bool = false,
-
/// This exercise makes use of C functions
/// We need to keep track of this, so we compile with libc
- C: bool = false,
+ link_libc: bool = false,
/// This exercise is not supported by the current Zig compiler.
skip: bool = false,
/// Returns the name of the main file with .zig stripped.
- pub fn baseName(self: Exercise) []const u8 {
- assert(std.mem.endsWith(u8, self.main_file, ".zig"));
- return self.main_file[0 .. self.main_file.len - 4];
+ pub fn name(self: Exercise) []const u8 {
+ return std.fs.path.stem(self.main_file);
}
/// Returns the key of the main file, the string before the '_' with
@@ -63,8 +60,606 @@ pub const Exercise = struct {
pub fn number(self: Exercise) usize {
return std.fmt.parseInt(usize, self.key(), 10) catch unreachable;
}
+
+ /// Returns the CompileStep for this exercise.
+ pub fn addExecutable(self: Exercise, b: *Build, work_path: []const u8) *CompileStep {
+ const file_path = join(b.allocator, &.{ work_path, self.main_file }) catch
+ @panic("OOM");
+
+ return b.addExecutable(.{
+ .name = self.name(),
+ .root_source_file = .{ .path = file_path },
+ .link_libc = self.link_libc,
+ });
+ }
+};
+
+pub fn build(b: *Build) !void {
+ if (!compat.is_compatible) compat.die();
+ if (!validate_exercises()) std.os.exit(1);
+
+ use_color_escapes = false;
+ if (std.io.getStdErr().supportsAnsiEscapeCodes()) {
+ use_color_escapes = true;
+ } else if (builtin.os.tag == .windows) {
+ const w32 = struct {
+ const WINAPI = std.os.windows.WINAPI;
+ const DWORD = std.os.windows.DWORD;
+ const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
+ const STD_ERROR_HANDLE = @bitCast(DWORD, @as(i32, -12));
+ extern "kernel32" fn GetStdHandle(id: DWORD) callconv(WINAPI) ?*anyopaque;
+ extern "kernel32" fn GetConsoleMode(console: ?*anyopaque, out_mode: *DWORD) callconv(WINAPI) u32;
+ extern "kernel32" fn SetConsoleMode(console: ?*anyopaque, mode: DWORD) callconv(WINAPI) u32;
+ };
+ const handle = w32.GetStdHandle(w32.STD_ERROR_HANDLE);
+ var mode: w32.DWORD = 0;
+ if (w32.GetConsoleMode(handle, &mode) != 0) {
+ mode |= w32.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
+ use_color_escapes = w32.SetConsoleMode(handle, mode) != 0;
+ }
+ }
+
+ if (use_color_escapes) {
+ red_text = "\x1b[31m";
+ green_text = "\x1b[32m";
+ bold_text = "\x1b[1m";
+ reset_text = "\x1b[0m";
+ }
+
+ const logo =
+ \\
+ \\ _ _ _
+ \\ ___(_) __ _| (_)_ __ __ _ ___
+ \\ |_ | |/ _' | | | '_ \ / _' / __|
+ \\ / /| | (_| | | | | | | (_| \__ \
+ \\ /___|_|\__, |_|_|_| |_|\__, |___/
+ \\ |___/ |___/
+ \\
+ \\
+ ;
+
+ const use_healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse false;
+ const exno: ?usize = b.option(usize, "n", "Select exercise");
+
+ const healed_path = "patches/healed";
+ const work_path = if (use_healed) healed_path else "exercises";
+
+ const header_step = PrintStep.create(b, logo);
+
+ if (exno) |n| {
+ if (n == 0 or n > exercises.len - 1) {
+ print("unknown exercise number: {}\n", .{n});
+ std.os.exit(1);
+ }
+
+ const ex = exercises[n - 1];
+
+ const build_step = ex.addExecutable(b, work_path);
+ b.installArtifact(build_step);
+
+ const run_step = b.addRunArtifact(build_step);
+
+ const test_step = b.step("test", b.fmt("Run {s} without checking output", .{ex.main_file}));
+ if (ex.skip) {
+ const skip_step = SkipStep.create(b, ex);
+ test_step.dependOn(&skip_step.step);
+ } else {
+ test_step.dependOn(&run_step.step);
+ }
+
+ const verify_step = ZiglingStep.create(b, ex, work_path);
+
+ const zigling_step = b.step("zigling", b.fmt("Check the solution of {s}", .{ex.main_file}));
+ zigling_step.dependOn(&verify_step.step);
+ b.default_step = zigling_step;
+
+ const start_step = b.step("start", b.fmt("Check all solutions starting at {s}", .{ex.main_file}));
+
+ var prev_step = verify_step;
+ for (exercises) |exn| {
+ const nth = exn.number();
+ if (nth > n) {
+ const verify_stepn = ZiglingStep.create(b, exn, work_path);
+ verify_stepn.step.dependOn(&prev_step.step);
+
+ prev_step = verify_stepn;
+ }
+ }
+ start_step.dependOn(&prev_step.step);
+
+ return;
+ } else if (use_healed and false) {
+ // Special case when healed by the eowyn script, where we can make the
+ // code more efficient.
+ //
+ // TODO: this branch is disabled because it prevents the normal case to
+ // be executed.
+ const test_step = b.step("test", "Test the healed exercises");
+ b.default_step = test_step;
+
+ for (exercises) |ex| {
+ const build_step = ex.addExecutable(b, healed_path);
+ b.installArtifact(build_step);
+
+ const run_step = b.addRunArtifact(build_step);
+ if (ex.skip) {
+ const skip_step = SkipStep.create(b, ex);
+ test_step.dependOn(&skip_step.step);
+ } else {
+ test_step.dependOn(&run_step.step);
+ }
+ }
+
+ return;
+ }
+
+ const ziglings_step = b.step("ziglings", "Check all ziglings");
+ b.default_step = ziglings_step;
+
+ // Don't use the "multi-object for loop" syntax, in order to avoid a syntax
+ // error with old Zig compilers.
+ var prev_step = &header_step.step;
+ for (exercises) |ex| {
+ const build_step = ex.addExecutable(b, "exercises");
+ b.installArtifact(build_step);
+
+ const verify_stepn = ZiglingStep.create(b, ex, work_path);
+ verify_stepn.step.dependOn(prev_step);
+
+ prev_step = &verify_stepn.step;
+ }
+ ziglings_step.dependOn(prev_step);
+
+ const test_step = b.step("test", "Run all the tests");
+ test_step.dependOn(tests.addCliTests(b, &exercises));
+}
+
+var use_color_escapes = false;
+var red_text: []const u8 = "";
+var green_text: []const u8 = "";
+var bold_text: []const u8 = "";
+var reset_text: []const u8 = "";
+
+const ZiglingStep = struct {
+ step: Step,
+ exercise: Exercise,
+ work_path: []const u8,
+
+ result_messages: []const u8 = "",
+ result_error_bundle: std.zig.ErrorBundle = std.zig.ErrorBundle.empty,
+
+ pub fn create(b: *Build, exercise: Exercise, work_path: []const u8) *ZiglingStep {
+ const self = b.allocator.create(ZiglingStep) catch @panic("OOM");
+ self.* = .{
+ .step = Step.init(.{
+ .id = .custom,
+ .name = exercise.main_file,
+ .owner = b,
+ .makeFn = make,
+ }),
+ .exercise = exercise,
+ .work_path = work_path,
+ };
+ return self;
+ }
+
+ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
+ const self = @fieldParentPtr(ZiglingStep, "step", step);
+
+ if (self.exercise.skip) {
+ print("Skipping {s}\n\n", .{self.exercise.main_file});
+
+ return;
+ }
+
+ const exe_path = self.compile(prog_node) catch {
+ if (self.exercise.hint.len > 0) {
+ print("\n{s}HINT: {s}{s}", .{
+ bold_text, self.exercise.hint, reset_text,
+ });
+ }
+
+ self.help();
+ std.os.exit(1);
+ };
+
+ self.run(exe_path, prog_node) catch {
+ if (self.exercise.hint.len > 0) {
+ print("\n{s}HINT: {s}{s}", .{
+ bold_text, self.exercise.hint, reset_text,
+ });
+ }
+
+ self.help();
+ std.os.exit(1);
+ };
+ }
+
+ fn run(self: *ZiglingStep, exe_path: []const u8, _: *std.Progress.Node) !void {
+ resetLine();
+ print("Checking {s}...\n", .{self.exercise.main_file});
+
+ const b = self.step.owner;
+
+ // Allow up to 1 MB of stdout capture.
+ const max_output_bytes = 1 * 1024 * 1024;
+
+ var result = Child.exec(.{
+ .allocator = b.allocator,
+ .argv = &.{exe_path},
+ .cwd = b.build_root.path.?,
+ .cwd_dir = b.build_root.handle,
+ .max_output_bytes = max_output_bytes,
+ }) catch |err| {
+ print("{s}Unable to spawn {s}: {s}{s}\n", .{
+ red_text, exe_path, @errorName(err), reset_text,
+ });
+ return err;
+ };
+
+ const raw_output = if (self.exercise.check_stdout)
+ result.stdout
+ else
+ result.stderr;
+
+ // Make sure it exited cleanly.
+ switch (result.term) {
+ .Exited => |code| {
+ if (code != 0) {
+ print("{s}{s} exited with error code {d} (expected {d}){s}\n", .{
+ red_text, self.exercise.main_file, code, 0, reset_text,
+ });
+ return error.BadExitCode;
+ }
+ },
+ else => {
+ print("{s}{s} terminated unexpectedly{s}\n", .{
+ red_text, self.exercise.main_file, reset_text,
+ });
+ return error.UnexpectedTermination;
+ },
+ }
+
+ // Validate the output.
+ const output = std.mem.trimRight(u8, raw_output, " \r\n");
+ const exercise_output = std.mem.trimRight(u8, self.exercise.output, " \r\n");
+ if (!std.mem.eql(u8, output, exercise_output)) {
+ const red = red_text;
+ const reset = reset_text;
+
+ print(
+ \\
+ \\{s}========= expected this output: =========={s}
+ \\{s}
+ \\{s}========= but found: ====================={s}
+ \\{s}
+ \\{s}=========================================={s}
+ \\
+ , .{ red, reset, exercise_output, red, reset, output, red, reset });
+ return error.InvalidOutput;
+ }
+
+ print("{s}PASSED:\n{s}{s}\n\n", .{ green_text, output, reset_text });
+ }
+
+ fn compile(self: *ZiglingStep, prog_node: *std.Progress.Node) ![]const u8 {
+ print("Compiling {s}...\n", .{self.exercise.main_file});
+
+ const b = self.step.owner;
+ const exercise_path = self.exercise.main_file;
+ const path = join(b.allocator, &.{ self.work_path, exercise_path }) catch
+ @panic("OOM");
+
+ var zig_args = std.ArrayList([]const u8).init(b.allocator);
+ defer zig_args.deinit();
+
+ zig_args.append(b.zig_exe) catch @panic("OOM");
+ zig_args.append("build-exe") catch @panic("OOM");
+
+ // Enable C support for exercises that use C functions.
+ if (self.exercise.link_libc) {
+ zig_args.append("-lc") catch @panic("OOM");
+ }
+
+ zig_args.append(b.pathFromRoot(path)) catch @panic("OOM");
+
+ zig_args.append("--cache-dir") catch @panic("OOM");
+ zig_args.append(b.pathFromRoot(b.cache_root.path.?)) catch @panic("OOM");
+
+ zig_args.append("--listen=-") catch @panic("OOM");
+
+ const argv = zig_args.items;
+ var code: u8 = undefined;
+ const exe_path = self.eval(argv, &code, prog_node) catch |err| {
+ self.printErrors();
+
+ switch (err) {
+ error.FileNotFound => {
+ print("{s}{s}: Unable to spawn the following command: file not found{s}\n", .{
+ red_text, self.exercise.main_file, reset_text,
+ });
+ for (argv) |v| print("{s} ", .{v});
+ print("\n", .{});
+ },
+ error.ExitCodeFailure => {
+ print("{s}{s}: The following command exited with error code {}:{s}\n", .{
+ red_text, self.exercise.main_file, code, reset_text,
+ });
+ for (argv) |v| print("{s} ", .{v});
+ print("\n", .{});
+ },
+ error.ProcessTerminated => {
+ print("{s}{s}: The following command terminated unexpectedly:{s}\n", .{
+ red_text, self.exercise.main_file, reset_text,
+ });
+ for (argv) |v| print("{s} ", .{v});
+ print("\n", .{});
+ },
+ error.ZigIPCError => {
+ print("{s}{s}: The following command failed to communicate the compilation result:{s}\n", .{
+ red_text, self.exercise.main_file, reset_text,
+ });
+ for (argv) |v| print("{s} ", .{v});
+ print("\n", .{});
+ },
+ else => {
+ print("{s}{s}: Unexpected error: {s}{s}\n", .{
+ red_text, self.exercise.main_file, @errorName(err), reset_text,
+ });
+ for (argv) |v| print("{s} ", .{v});
+ print("\n", .{});
+ },
+ }
+
+ return err;
+ };
+ self.printErrors();
+
+ return exe_path;
+ }
+
+ // Code adapted from `std.Build.execAllowFail and `std.Build.Step.evalZigProcess`.
+ pub fn eval(
+ self: *ZiglingStep,
+ argv: []const []const u8,
+ out_code: *u8,
+ prog_node: *std.Progress.Node,
+ ) ![]const u8 {
+ assert(argv.len != 0);
+ const b = self.step.owner;
+ const allocator = b.allocator;
+
+ var child = Child.init(argv, allocator);
+ child.env_map = b.env_map;
+ child.stdin_behavior = .Pipe;
+ child.stdout_behavior = .Pipe;
+ child.stderr_behavior = .Pipe;
+
+ try child.spawn();
+
+ var poller = std.io.poll(allocator, enum { stdout, stderr }, .{
+ .stdout = child.stdout.?,
+ .stderr = child.stderr.?,
+ });
+ defer poller.deinit();
+
+ try ipc.sendMessage(child.stdin.?, .update);
+ try ipc.sendMessage(child.stdin.?, .exit);
+
+ const Header = std.zig.Server.Message.Header;
+ var result: ?[]const u8 = null;
+
+ var node_name: std.ArrayListUnmanaged(u8) = .{};
+ defer node_name.deinit(allocator);
+ var sub_prog_node = prog_node.start("", 0);
+ defer sub_prog_node.end();
+
+ const stdout = poller.fifo(.stdout);
+
+ poll: while (true) {
+ while (stdout.readableLength() < @sizeOf(Header)) {
+ if (!(try poller.poll())) break :poll;
+ }
+ const header = stdout.reader().readStruct(Header) catch unreachable;
+ while (stdout.readableLength() < header.bytes_len) {
+ if (!(try poller.poll())) break :poll;
+ }
+ const body = stdout.readableSliceOfLen(header.bytes_len);
+
+ switch (header.tag) {
+ .zig_version => {
+ if (!std.mem.eql(u8, builtin.zig_version_string, body))
+ return error.ZigVersionMismatch;
+ },
+ .error_bundle => {
+ self.result_error_bundle = try ipc.parseErrorBundle(allocator, body);
+ },
+ .progress => {
+ node_name.clearRetainingCapacity();
+ try node_name.appendSlice(allocator, body);
+ sub_prog_node.setName(node_name.items);
+ },
+ .emit_bin_path => {
+ const emit_bin = try ipc.parseEmitBinPath(allocator, body);
+ result = emit_bin.path;
+ },
+ else => {}, // ignore other messages
+ }
+
+ stdout.discard(body.len);
+ }
+
+ const stderr = poller.fifo(.stderr);
+ if (stderr.readableLength() > 0) {
+ self.result_messages = try stderr.toOwnedSlice();
+ }
+
+ // Send EOF to stdin.
+ child.stdin.?.close();
+ child.stdin = null;
+
+ // Keep the errors compatible with std.Build.execAllowFail.
+ const term = try child.wait();
+ switch (term) {
+ .Exited => |code| {
+ if (code != 0) {
+ out_code.* = @truncate(u8, code);
+
+ return error.ExitCodeFailure;
+ }
+ },
+ .Signal, .Stopped, .Unknown => |code| {
+ out_code.* = @truncate(u8, code);
+
+ return error.ProcessTerminated;
+ },
+ }
+
+ return result orelse return error.ZigIPCError;
+ }
+
+ fn help(self: *ZiglingStep) void {
+ const path = self.exercise.main_file;
+ const key = self.exercise.key();
+
+ print("\n{s}Edit exercises/{s} and run this again.{s}", .{
+ red_text, path, reset_text,
+ });
+
+ const format =
+ \\
+ \\{s}To continue from this zigling, use this command:{s}
+ \\ {s}zig build -Dn={s}{s}
+ \\
+ ;
+ print(format, .{ red_text, reset_text, bold_text, key, reset_text });
+ }
+
+ fn printErrors(self: *ZiglingStep) void {
+ resetLine();
+
+ // Print the additional log and verbose messages.
+ // TODO: use colors?
+ if (self.result_messages.len > 0) print("{s}", .{self.result_messages});
+
+ // Print the compiler errors.
+ // TODO: use the same ttyconf from the builder.
+ const ttyconf: std.debug.TTY.Config = if (use_color_escapes)
+ .escape_codes
+ else
+ .no_color;
+ if (self.result_error_bundle.errorMessageCount() > 0) {
+ self.result_error_bundle.renderToStdErr(.{ .ttyconf = ttyconf });
+ }
+ }
};
+// Clear the entire line and move the cursor to column zero.
+// Used for clearing the compiler and build_runner progress messages.
+fn resetLine() void {
+ if (use_color_escapes) print("{s}", .{"\x1b[2K\r"});
+}
+
+// Print a message to stderr.
+const PrintStep = struct {
+ step: Step,
+ message: []const u8,
+
+ pub fn create(owner: *Build, message: []const u8) *PrintStep {
+ const self = owner.allocator.create(PrintStep) catch @panic("OOM");
+ self.* = .{
+ .step = Step.init(.{
+ .id = .custom,
+ .name = "print",
+ .owner = owner,
+ .makeFn = make,
+ }),
+ .message = message,
+ };
+
+ return self;
+ }
+
+ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
+ _ = prog_node;
+ const p = @fieldParentPtr(PrintStep, "step", step);
+
+ print("{s}", .{p.message});
+ }
+};
+
+// Skip an exercise.
+const SkipStep = struct {
+ step: Step,
+ exercise: Exercise,
+
+ pub fn create(owner: *Build, exercise: Exercise) *SkipStep {
+ const self = owner.allocator.create(SkipStep) catch @panic("OOM");
+ self.* = .{
+ .step = Step.init(.{
+ .id = .custom,
+ .name = owner.fmt("skip {s}", .{exercise.main_file}),
+ .owner = owner,
+ .makeFn = make,
+ }),
+ .exercise = exercise,
+ };
+
+ return self;
+ }
+
+ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
+ _ = prog_node;
+ const p = @fieldParentPtr(SkipStep, "step", step);
+
+ if (p.exercise.skip) {
+ print("{s} skipped\n", .{p.exercise.main_file});
+ }
+ }
+};
+
+// Check that each exercise number, excluding the last, forms the sequence
+// `[1, exercise.len)`.
+//
+// Additionally check that the output field does not contain trailing whitespace.
+fn validate_exercises() bool {
+ // Don't use the "multi-object for loop" syntax, in order to avoid a syntax
+ // error with old Zig compilers.
+ var i: usize = 0;
+ for (exercises[0..]) |ex| {
+ const exno = ex.number();
+ const last = 999;
+ i += 1;
+
+ if (exno != i and exno != last) {
+ print("exercise {s} has an incorrect number: expected {}, got {s}\n", .{
+ ex.main_file,
+ i,
+ ex.key(),
+ });
+
+ return false;
+ }
+
+ const output = std.mem.trimRight(u8, ex.output, " \r\n");
+ if (output.len != ex.output.len) {
+ print("exercise {s} output field has extra trailing whitespace\n", .{
+ ex.main_file,
+ });
+
+ return false;
+ }
+
+ if (!std.mem.endsWith(u8, ex.main_file, ".zig")) {
+ print("exercise {s} is not a zig source file\n", .{ex.main_file});
+
+ return false;
+ }
+ }
+
+ return true;
+}
+
const exercises = [_]Exercise{
.{
.main_file = "001_hello.zig",
@@ -439,49 +1034,41 @@ const exercises = [_]Exercise{
.main_file = "084_async.zig",
.output = "foo() A",
.hint = "Read the facts. Use the facts.",
- .@"async" = true,
.skip = true,
},
.{
.main_file = "085_async2.zig",
.output = "Hello async!",
- .@"async" = true,
.skip = true,
},
.{
.main_file = "086_async3.zig",
.output = "5 4 3 2 1",
- .@"async" = true,
.skip = true,
},
.{
.main_file = "087_async4.zig",
.output = "1 2 3 4 5",
- .@"async" = true,
.skip = true,
},
.{
.main_file = "088_async5.zig",
.output = "Example Title.",
- .@"async" = true,
.skip = true,
},
.{
.main_file = "089_async6.zig",
.output = ".com: Example Title, .org: Example Title.",
- .@"async" = true,
.skip = true,
},
.{
.main_file = "090_async7.zig",
.output = "beef? BEEF!",
- .@"async" = true,
.skip = true,
},
.{
.main_file = "091_async8.zig",
.output = "ABCDEF",
- .@"async" = true,
.skip = true,
},
@@ -492,15 +1079,15 @@ const exercises = [_]Exercise{
.{
.main_file = "093_hello_c.zig",
.output = "Hello C from Zig! - C result is 17 chars written.",
- .C = true,
+ .link_libc = true,
},
.{
.main_file = "094_c_math.zig",
.output = "The normalized angle of 765.2 degrees is 45.2 degrees.",
- .C = true,
+ .link_libc = true,
},
.{
- .main_file = "095_for_loops.zig",
+ .main_file = "095_for3.zig",
.output = "1 2 4 7 8 11 13 14 16 17 19",
},
.{
@@ -520,573 +1107,15 @@ const exercises = [_]Exercise{
.output = "\n X | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 \n---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n 1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 \n\n 2 | 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 \n\n 3 | 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 \n\n 4 | 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 \n\n 5 | 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 \n\n 6 | 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 \n\n 7 | 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 \n\n 8 | 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 \n\n 9 | 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 \n\n10 | 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 \n\n11 | 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 \n\n12 | 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180 \n\n13 | 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195 \n\n14 | 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210 \n\n15 | 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225",
},
.{
+ .main_file = "100_for4.zig",
+ .output = "Arrays match!",
+ },
+ .{
+ .main_file = "101_for5.zig",
+ .output = "1. Wizard (Gold: 25, XP: 40)\n2. Bard (Gold: 11, XP: 17)\n3. Bard (Gold: 5, XP: 55)\n4. Warrior (Gold: 7392, XP: 21)",
+ },
+ .{
.main_file = "999_the_end.zig",
.output = "\nThis is the end for now!\nWe hope you had fun and were able to learn a lot, so visit us again when the next exercises are available.",
},
};
-
-pub fn build(b: *Build) !void {
- if (!compat.is_compatible) compat.die();
- if (!validate_exercises()) std.os.exit(1);
-
- use_color_escapes = false;
- if (std.io.getStdErr().supportsAnsiEscapeCodes()) {
- use_color_escapes = true;
- } else if (builtin.os.tag == .windows) {
- const w32 = struct {
- const WINAPI = std.os.windows.WINAPI;
- const DWORD = std.os.windows.DWORD;
- const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
- const STD_ERROR_HANDLE = @bitCast(DWORD, @as(i32, -12));
- extern "kernel32" fn GetStdHandle(id: DWORD) callconv(WINAPI) ?*anyopaque;
- extern "kernel32" fn GetConsoleMode(console: ?*anyopaque, out_mode: *DWORD) callconv(WINAPI) u32;
- extern "kernel32" fn SetConsoleMode(console: ?*anyopaque, mode: DWORD) callconv(WINAPI) u32;
- };
- const handle = w32.GetStdHandle(w32.STD_ERROR_HANDLE);
- var mode: w32.DWORD = 0;
- if (w32.GetConsoleMode(handle, &mode) != 0) {
- mode |= w32.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
- use_color_escapes = w32.SetConsoleMode(handle, mode) != 0;
- }
- }
-
- if (use_color_escapes) {
- red_text = "\x1b[31m";
- green_text = "\x1b[32m";
- bold_text = "\x1b[1m";
- reset_text = "\x1b[0m";
- }
-
- const logo =
- \\
- \\ _ _ _
- \\ ___(_) __ _| (_)_ __ __ _ ___
- \\ |_ | |/ _' | | | '_ \ / _' / __|
- \\ / /| | (_| | | | | | | (_| \__ \
- \\ /___|_|\__, |_|_|_| |_|\__, |___/
- \\ |___/ |___/
- \\
- \\
- ;
-
- const use_healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse false;
- const exno: ?usize = b.option(usize, "n", "Select exercise");
-
- const header_step = PrintStep.create(b, logo);
-
- if (exno) |n| {
- if (n == 0 or n > exercises.len - 1) {
- print("unknown exercise number: {}\n", .{n});
- std.os.exit(1);
- }
-
- const ex = exercises[n - 1];
- const base_name = ex.baseName();
- const file_path = std.fs.path.join(b.allocator, &[_][]const u8{
- if (use_healed) "patches/healed" else "exercises", ex.main_file,
- }) catch unreachable;
-
- const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } });
- if (ex.C) {
- build_step.linkLibC();
- }
- b.installArtifact(build_step);
-
- const run_step = b.addRunArtifact(build_step);
-
- const test_step = b.step("test", b.fmt("Run {s} without checking output", .{ex.main_file}));
- if (ex.skip) {
- const skip_step = SkipStep.create(b, ex);
- test_step.dependOn(&skip_step.step);
- } else {
- test_step.dependOn(&run_step.step);
- }
-
- const install_step = b.step("install", b.fmt("Install {s} to prefix path", .{ex.main_file}));
- install_step.dependOn(b.getInstallStep());
-
- const uninstall_step = b.step("uninstall", b.fmt("Uninstall {s} from prefix path", .{ex.main_file}));
- uninstall_step.dependOn(b.getUninstallStep());
-
- const verify_step = ZiglingStep.create(b, ex, use_healed);
-
- const zigling_step = b.step("zigling", b.fmt("Check the solution of {s}", .{ex.main_file}));
- zigling_step.dependOn(&verify_step.step);
- b.default_step = zigling_step;
-
- const start_step = b.step("start", b.fmt("Check all solutions starting at {s}", .{ex.main_file}));
-
- var prev_step = verify_step;
- for (exercises) |exn| {
- const nth = exn.number();
- if (nth > n) {
- const verify_stepn = ZiglingStep.create(b, exn, use_healed);
- verify_stepn.step.dependOn(&prev_step.step);
-
- prev_step = verify_stepn;
- }
- }
- start_step.dependOn(&prev_step.step);
-
- return;
- } else if (use_healed and false) {
- const test_step = b.step("test", "Test the healed exercises");
- b.default_step = test_step;
-
- for (exercises) |ex| {
- const base_name = ex.baseName();
- const file_path = std.fs.path.join(b.allocator, &[_][]const u8{
- "patches/healed", ex.main_file,
- }) catch unreachable;
-
- const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } });
- if (ex.C) {
- build_step.linkLibC();
- }
- b.installArtifact(build_step);
-
- const run_step = b.addRunArtifact(build_step);
- if (ex.skip) {
- const skip_step = SkipStep.create(b, ex);
- test_step.dependOn(&skip_step.step);
- } else {
- test_step.dependOn(&run_step.step);
- }
- }
-
- return;
- }
-
- const ziglings_step = b.step("ziglings", "Check all ziglings");
- b.default_step = ziglings_step;
-
- // Don't use the "multi-object for loop" syntax, in order to avoid a syntax
- // error with old Zig compilers.
- var prev_step = &header_step.step;
- for (exercises) |ex| {
- const base_name = ex.baseName();
- const file_path = std.fs.path.join(b.allocator, &[_][]const u8{
- "exercises", ex.main_file,
- }) catch unreachable;
-
- const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } });
- b.installArtifact(build_step);
-
- const verify_stepn = ZiglingStep.create(b, ex, use_healed);
- verify_stepn.step.dependOn(prev_step);
-
- prev_step = &verify_stepn.step;
- }
- ziglings_step.dependOn(prev_step);
-
- const test_step = b.step("test", "Run all the tests");
- test_step.dependOn(tests.addCliTests(b, &exercises));
-}
-
-var use_color_escapes = false;
-var red_text: []const u8 = "";
-var green_text: []const u8 = "";
-var bold_text: []const u8 = "";
-var reset_text: []const u8 = "";
-
-const ZiglingStep = struct {
- step: Step,
- exercise: Exercise,
- builder: *Build,
- use_healed: bool,
-
- result_messages: []const u8 = "",
- result_error_bundle: std.zig.ErrorBundle = std.zig.ErrorBundle.empty,
-
- pub fn create(builder: *Build, exercise: Exercise, use_healed: bool) *@This() {
- const self = builder.allocator.create(@This()) catch unreachable;
- self.* = .{
- .step = Step.init(Step.Options{ .id = .custom, .name = exercise.main_file, .owner = builder, .makeFn = make }),
- .exercise = exercise,
- .builder = builder,
- .use_healed = use_healed,
- };
- return self;
- }
-
- fn make(step: *Step, prog_node: *std.Progress.Node) anyerror!void {
- const self = @fieldParentPtr(@This(), "step", step);
-
- if (self.exercise.skip) {
- print("Skipping {s}\n\n", .{self.exercise.main_file});
-
- return;
- }
- self.makeInternal(prog_node) catch {
- if (self.exercise.hint.len > 0) {
- print("\n{s}HINT: {s}{s}", .{ bold_text, self.exercise.hint, reset_text });
- }
-
- print("\n{s}Edit exercises/{s} and run this again.{s}", .{ red_text, self.exercise.main_file, reset_text });
- print("\n{s}To continue from this zigling, use this command:{s}\n {s}zig build -Dn={s}{s}\n", .{ red_text, reset_text, bold_text, self.exercise.key(), reset_text });
- std.os.exit(1);
- };
- }
-
- fn makeInternal(self: *@This(), prog_node: *std.Progress.Node) !void {
- print("Compiling {s}...\n", .{self.exercise.main_file});
-
- const exe_file = try self.doCompile(prog_node);
-
- resetLine();
- print("Checking {s}...\n", .{self.exercise.main_file});
-
- const cwd = self.builder.build_root.path.?;
-
- const argv = [_][]const u8{exe_file};
-
- var child = std.ChildProcess.init(&argv, self.builder.allocator);
-
- child.cwd = cwd;
- child.env_map = self.builder.env_map;
-
- child.stdin_behavior = .Inherit;
- if (self.exercise.check_stdout) {
- child.stdout_behavior = .Pipe;
- child.stderr_behavior = .Inherit;
- } else {
- child.stdout_behavior = .Inherit;
- child.stderr_behavior = .Pipe;
- }
-
- child.spawn() catch |err| {
- print("{s}Unable to spawn {s}: {s}{s}\n", .{ red_text, argv[0], @errorName(err), reset_text });
- return err;
- };
-
- // Allow up to 1 MB of stdout capture.
- const max_output_len = 1 * 1024 * 1024;
- const output = if (self.exercise.check_stdout)
- try child.stdout.?.reader().readAllAlloc(self.builder.allocator, max_output_len)
- else
- try child.stderr.?.reader().readAllAlloc(self.builder.allocator, max_output_len);
-
- // At this point stdout is closed, wait for the process to terminate.
- const term = child.wait() catch |err| {
- print("{s}Unable to spawn {s}: {s}{s}\n", .{ red_text, argv[0], @errorName(err), reset_text });
- return err;
- };
-
- // Make sure it exited cleanly.
- switch (term) {
- .Exited => |code| {
- if (code != 0) {
- print("{s}{s} exited with error code {d} (expected {d}){s}\n", .{ red_text, self.exercise.main_file, code, 0, reset_text });
- return error.BadExitCode;
- }
- },
- else => {
- print("{s}{s} terminated unexpectedly{s}\n", .{ red_text, self.exercise.main_file, reset_text });
- return error.UnexpectedTermination;
- },
- }
-
- // Validate the output.
- const trimOutput = std.mem.trimRight(u8, output, " \r\n");
- const trimExerciseOutput = std.mem.trimRight(u8, self.exercise.output, " \r\n");
- if (!std.mem.eql(u8, trimOutput, trimExerciseOutput)) {
- print(
- \\
- \\{s}----------- Expected this output -----------{s}
- \\"{s}"
- \\{s}----------- but found -----------{s}
- \\"{s}"
- \\{s}-----------{s}
- \\
- , .{ red_text, reset_text, trimExerciseOutput, red_text, reset_text, trimOutput, red_text, reset_text });
- return error.InvalidOutput;
- }
-
- print("{s}PASSED:\n{s}{s}\n\n", .{ green_text, trimOutput, reset_text });
- }
-
- // The normal compile step calls os.exit, so we can't use it as a library :(
- // This is a stripped down copy of std.build.LibExeObjStep.make.
- fn doCompile(self: *@This(), prog_node: *std.Progress.Node) ![]const u8 {
- const builder = self.builder;
-
- var zig_args = std.ArrayList([]const u8).init(builder.allocator);
- defer zig_args.deinit();
-
- zig_args.append(builder.zig_exe) catch unreachable;
- zig_args.append("build-exe") catch unreachable;
-
- // Enable the stage 1 compiler if using the async feature
- // disabled because of https://github.com/ratfactor/ziglings/issues/163
- // if (self.exercise.@"async") {
- // zig_args.append("-fstage1") catch unreachable;
- // }
-
- // Enable C support for exercises that use C functions
- if (self.exercise.C) {
- zig_args.append("-lc") catch unreachable;
- }
-
- const zig_file = std.fs.path.join(builder.allocator, &[_][]const u8{ if (self.use_healed) "patches/healed" else "exercises", self.exercise.main_file }) catch unreachable;
- zig_args.append(builder.pathFromRoot(zig_file)) catch unreachable;
-
- zig_args.append("--cache-dir") catch unreachable;
- zig_args.append(builder.pathFromRoot(builder.cache_root.path.?)) catch unreachable;
-
- zig_args.append("--listen=-") catch unreachable;
-
- const argv = zig_args.items;
- var code: u8 = undefined;
- const file_name = self.eval(argv, &code, prog_node) catch |err| {
- self.printErrors();
-
- switch (err) {
- error.FileNotFound => {
- print("{s}{s}: Unable to spawn the following command: file not found{s}\n", .{ red_text, self.exercise.main_file, reset_text });
- for (argv) |v| print("{s} ", .{v});
- print("\n", .{});
- },
- error.ExitCodeFailure => {
- print("{s}{s}: The following command exited with error code {}:{s}\n", .{ red_text, self.exercise.main_file, code, reset_text });
- for (argv) |v| print("{s} ", .{v});
- print("\n", .{});
- },
- error.ProcessTerminated => {
- print("{s}{s}: The following command terminated unexpectedly:{s}\n", .{ red_text, self.exercise.main_file, reset_text });
- for (argv) |v| print("{s} ", .{v});
- print("\n", .{});
- },
- error.ZigIPCError => {
- print("{s}{s}: The following command failed to communicate the compilation result:{s}\n", .{
- red_text,
- self.exercise.main_file,
- reset_text,
- });
- for (argv) |v| print("{s} ", .{v});
- print("\n", .{});
- },
- else => {},
- }
-
- return err;
- };
- self.printErrors();
-
- return file_name;
- }
-
- // Code adapted from `std.Build.execAllowFail and `std.Build.Step.evalZigProcess`.
- pub fn eval(
- self: *ZiglingStep,
- argv: []const []const u8,
- out_code: *u8,
- prog_node: *std.Progress.Node,
- ) ![]const u8 {
- assert(argv.len != 0);
- const b = self.step.owner;
- const allocator = b.allocator;
-
- var child = Child.init(argv, allocator);
- child.env_map = b.env_map;
- child.stdin_behavior = .Pipe;
- child.stdout_behavior = .Pipe;
- child.stderr_behavior = .Pipe;
-
- try child.spawn();
-
- var poller = std.io.poll(allocator, enum { stdout, stderr }, .{
- .stdout = child.stdout.?,
- .stderr = child.stderr.?,
- });
- defer poller.deinit();
-
- try ipc.sendMessage(child.stdin.?, .update);
- try ipc.sendMessage(child.stdin.?, .exit);
-
- const Header = std.zig.Server.Message.Header;
- var result: ?[]const u8 = null;
-
- var node_name: std.ArrayListUnmanaged(u8) = .{};
- defer node_name.deinit(allocator);
- var sub_prog_node = prog_node.start("", 0);
- defer sub_prog_node.end();
-
- const stdout = poller.fifo(.stdout);
-
- poll: while (true) {
- while (stdout.readableLength() < @sizeOf(Header)) {
- if (!(try poller.poll())) break :poll;
- }
- const header = stdout.reader().readStruct(Header) catch unreachable;
- while (stdout.readableLength() < header.bytes_len) {
- if (!(try poller.poll())) break :poll;
- }
- const body = stdout.readableSliceOfLen(header.bytes_len);
-
- switch (header.tag) {
- .zig_version => {
- if (!std.mem.eql(u8, builtin.zig_version_string, body))
- return error.ZigVersionMismatch;
- },
- .error_bundle => {
- self.result_error_bundle = try ipc.parseErrorBundle(allocator, body);
- },
- .progress => {
- node_name.clearRetainingCapacity();
- try node_name.appendSlice(allocator, body);
- sub_prog_node.setName(node_name.items);
- },
- .emit_bin_path => {
- const emit_bin = try ipc.parseEmitBinPath(allocator, body);
- result = emit_bin.path;
- },
- else => {}, // ignore other messages
- }
-
- stdout.discard(body.len);
- }
-
- const stderr = poller.fifo(.stderr);
- if (stderr.readableLength() > 0) {
- self.result_messages = try stderr.toOwnedSlice();
- }
-
- // Send EOF to stdin.
- child.stdin.?.close();
- child.stdin = null;
-
- // Keep the errors compatible with std.Build.execAllowFail.
- const term = try child.wait();
- switch (term) {
- .Exited => |code| {
- if (code != 0) {
- out_code.* = @truncate(u8, code);
-
- return error.ExitCodeFailure;
- }
- },
- .Signal, .Stopped, .Unknown => |code| {
- out_code.* = @truncate(u8, code);
-
- return error.ProcessTerminated;
- },
- }
-
- return result orelse return error.ZigIPCError;
- }
-
- fn printErrors(self: *ZiglingStep) void {
- resetLine();
-
- // Print the additional log and verbose messages.
- // TODO: use colors?
- if (self.result_messages.len > 0) print("{s}", .{self.result_messages});
-
- // Print the compiler errors.
- // TODO: use the same ttyconf from the builder.
- const ttyconf: std.debug.TTY.Config = if (use_color_escapes)
- .escape_codes
- else
- .no_color;
- if (self.result_error_bundle.errorMessageCount() > 0) {
- self.result_error_bundle.renderToStdErr(.{ .ttyconf = ttyconf });
- }
- }
-};
-
-// Clear the entire line and move the cursor to column zero.
-// Used for clearing the compiler and build_runner progress messages.
-fn resetLine() void {
- if (use_color_escapes) print("{s}", .{"\x1b[2K\r"});
-}
-
-// Print a message to stderr.
-const PrintStep = struct {
- step: Step,
- message: []const u8,
-
- pub fn create(owner: *Build, message: []const u8) *PrintStep {
- const self = owner.allocator.create(PrintStep) catch @panic("OOM");
- self.* = .{
- .step = Step.init(.{
- .id = .custom,
- .name = "print",
- .owner = owner,
- .makeFn = make,
- }),
- .message = message,
- };
-
- return self;
- }
-
- fn make(step: *Step, prog_node: *std.Progress.Node) !void {
- _ = prog_node;
- const p = @fieldParentPtr(PrintStep, "step", step);
-
- print("{s}", .{p.message});
- }
-};
-
-// Skip an exercise.
-const SkipStep = struct {
- step: Step,
- exercise: Exercise,
-
- pub fn create(owner: *Build, exercise: Exercise) *SkipStep {
- const self = owner.allocator.create(SkipStep) catch @panic("OOM");
- self.* = .{
- .step = Step.init(.{
- .id = .custom,
- .name = owner.fmt("skip {s}", .{exercise.main_file}),
- .owner = owner,
- .makeFn = make,
- }),
- .exercise = exercise,
- };
-
- return self;
- }
-
- fn make(step: *Step, prog_node: *std.Progress.Node) !void {
- _ = prog_node;
- const p = @fieldParentPtr(SkipStep, "step", step);
-
- if (p.exercise.skip) {
- print("{s} skipped\n", .{p.exercise.main_file});
- }
- }
-};
-
-// Check that each exercise number, excluding the last, forms the sequence
-// `[1, exercise.len)`.
-//
-// Additionally check that the output field does not contain trailing whitespace.
-fn validate_exercises() bool {
- // Don't use the "multi-object for loop" syntax, in order to avoid a syntax
- // error with old Zig compilers.
- var i: usize = 0;
- for (exercises[0 .. exercises.len - 1]) |ex| {
- i += 1;
- if (ex.number() != i) {
- print("exercise {s} has an incorrect number: expected {}, got {s}\n", .{
- ex.main_file,
- i,
- ex.key(),
- });
-
- return false;
- }
-
- const output = std.mem.trimRight(u8, ex.output, " \r\n");
- if (output.len != ex.output.len) {
- print("exercise {s} output field has extra trailing whitespace\n", .{
- ex.main_file,
- });
-
- return false;
- }
- }
-
- return true;
-}
diff --git a/exercises/015_for.zig b/exercises/015_for.zig
index 4c87a05..0ee8e7d 100644
--- a/exercises/015_for.zig
+++ b/exercises/015_for.zig
@@ -23,5 +23,9 @@ pub fn main() void {
std.debug.print("The End.\n", .{});
}
-// Note that "for" loops also work on things called "slices"
+// Note that 'for' loops also work on things called "slices"
// which we'll see later.
+//
+// Also note that 'for' loops have recently become more flexible
+// and powerful (two years after this exercise was written).
+// More about that in a moment.
diff --git a/exercises/016_for2.zig b/exercises/016_for2.zig
index 4a8d09c..ea1e6ca 100644
--- a/exercises/016_for2.zig
+++ b/exercises/016_for2.zig
@@ -35,3 +35,9 @@ pub fn main() void {
std.debug.print("The value of bits '1101': {}.\n", .{value});
}
+//
+// As mentioned in the previous exercise, 'for' loops have gained
+// additional flexibility since these early exercises were
+// written. As we'll see in later exercises, the above syntax for
+// capturing the index is part of a more general ability. Hang in
+// there!
diff --git a/exercises/095_for3.zig b/exercises/095_for3.zig
new file mode 100644
index 0000000..e4c4662
--- /dev/null
+++ b/exercises/095_for3.zig
@@ -0,0 +1,73 @@
+//
+// The Zig language is in rapid development and continuously
+// improves the language constructs. Ziglings evolves with it.
+//
+// Until version 0.11, Zig's 'for' loops did not directly
+// replicate the functionality of the C-style: "for(a;b;c)"
+// which are so well suited for iterating over a numeric
+// sequence.
+//
+// Instead, 'while' loops with counters clumsily stood in their
+// place:
+//
+// var i: usize = 0;
+// while (i < 10) : (i += 1) {
+// // Here variable 'i' will have each value 0 to 9.
+// }
+//
+// But here we are in the glorious future and Zig's 'for' loops
+// can now take this form:
+//
+// for (0..10) |i| {
+// // Here variable 'i' will have each value 0 to 9.
+// }
+//
+// The key to understanding this example is to know that '0..9'
+// uses the new range syntax:
+//
+// 0..10 is a range from 0 to 9
+// 1..4 is a range from 1 to 3
+//
+// At the moment, ranges are only supported in 'for' loops.
+//
+// Perhaps you recall Exercise 13? We were printing a numeric
+// sequence like so:
+//
+// var n: u32 = 1;
+//
+// // I want to print every number between 1 and 20 that is NOT
+// // divisible by 3 or 5.
+// while (n <= 20) : (n += 1) {
+// // The '%' symbol is the "modulo" operator and it
+// // returns the remainder after division.
+// if (n % 3 == 0) continue;
+// if (n % 5 == 0) continue;
+// std.debug.print("{} ", .{n});
+// }
+//
+// Let's try out the new form of 'for' to re-implement that
+// exercise:
+//
+const std = @import("std");
+
+pub fn main() void {
+
+ // I want to print every number between 1 and 20 that is NOT
+ // divisible by 3 or 5.
+ for (???) |n| {
+
+ // The '%' symbol is the "modulo" operator and it
+ // returns the remainder after division.
+ if (n % 3 == 0) continue;
+ if (n % 5 == 0) continue;
+ std.debug.print("{} ", .{n});
+ }
+
+ std.debug.print("\n", .{});
+}
+//
+// That's a bit nicer, right?
+//
+// Of course, both 'while' and 'for' have different advantages.
+// Exercises 11, 12, and 14 would NOT be simplified by switching
+// a 'while' for a 'for'.
diff --git a/exercises/095_for_loops.zig b/exercises/095_for_loops.zig
deleted file mode 100644
index b437946..0000000
--- a/exercises/095_for_loops.zig
+++ /dev/null
@@ -1,64 +0,0 @@
-//
-// The Zig language is in rapid development and continuously improves
-// the language constructs steadily.
-//
-// Since version 0.11, the "for-loops" widely used in other languages
-// such as C, e.g. "for (int i = 0; i < 10..." can now also be formed
-// similarly in Zig, which previously required a "while" construct.
-// Similar in this case actually means better, just as Zig generally
-// tries to make everything simple and "better".
-//
-// These new "for-loops" look like the following in Zig:
-//
-// for (0..10) |idx| {
-// // In this case 'idx' takes all values from 0 to 9.
-// }
-//
-// This is really simple and can replace the previous, somewhat bulky:
-//
-// var idx: usize = 0;
-// while (idx < 10) : (idx += 1) {
-// // Again, idx takes all values from 0 to 9.
-// }
-//
-// This would also simplify exercise 13, for example.
-// The best way to try this out is to use this exercise, which in the
-// original looks like this:
-//
-// ...
-// var n: u32 = 1;
-//
-// // I want to print every number between 1 and 20 that is NOT
-// // divisible by 3 or 5.
-// while (n <= 20) : (n += 1) {
-// // The '%' symbol is the "modulo" operator and it
-// // returns the remainder after division.
-// if (n % 3 == 0) continue;
-// if (n % 5 == 0) continue;
-// std.debug.print("{} ", .{n});
-// }
-// ...
-//
-const std = @import("std");
-
-// And now with the new "for-loop".
-pub fn main() void {
-
- // I want to print every number between 1 and 20 that is NOT
- // divisible by 3 or 5.
- for (???) |n| {
-
- // The '%' symbol is the "modulo" operator and it
- // returns the remainder after division.
- if (n % 3 == 0) continue;
- if (n % 5 == 0) continue;
- std.debug.print("{} ", .{n});
- }
-
- std.debug.print("\n", .{});
-}
-
-// Is actually a little easier. The interesting thing here is that the other
-// previous 'while' exercises (11,12, 14) cannot be simplified by this
-// new "for-loop". Therefore it is good to be able to use both variations
-// accordingly.
diff --git a/exercises/100_for4.zig b/exercises/100_for4.zig
new file mode 100644
index 0000000..e0fa602
--- /dev/null
+++ b/exercises/100_for4.zig
@@ -0,0 +1,62 @@
+//
+// We've seen that the 'for' loop can let us perform some action
+// for every item in an array or slice.
+//
+// More recently, we discovered that it supports ranges to
+// iterate over number sequences.
+//
+// This is part of a more general capability of the `for` loop:
+// looping over one or more "objects" where an object is an
+// array, slice, or range.
+//
+// In fact, we *did* use multiple objects way back in Exercise
+// 016 where we iterated over an array and also a numeric index.
+// It didn't always work exactly this way, so the exercise had to
+// be retroactively modified a little bit.
+//
+// for (bits, 0..) |bit, i| { ... }
+//
+// The general form of a 'for' loop with two lists is:
+//
+// for (list_a, list_b) |a, b| {
+// // Here we have the first item from list_a and list_b,
+// // then the second item from each, then the third and
+// // so forth...
+// }
+//
+// What's really beautiful about this is that we don't have to
+// keep track of an index or advancing a memory pointer for
+// *either* of these lists. That error-prone stuff is all taken
+// care of for us by the compiler.
+//
+// Below, we have a program that is supposed to compare two
+// arrays. Please make it work!
+//
+const std = @import("std");
+const print = std.debug.print;
+
+pub fn main() void {
+ const hex_nums = [_]u8{ 0xb, 0x2a, 0x77 };
+ const dec_nums = [_]u8{ 11, 42, 119 };
+
+ for (hex_nums, ???) |hn, ???| {
+ if (hn != dn) {
+ std.debug.print("Uh oh! Found a mismatch: {d} vs {d}\n", .{ hn, dn });
+ return;
+ }
+ }
+
+ std.debug.print("Arrays match!\n", .{});
+}
+//
+// You are perhaps wondering what happens if one of the two lists
+// is longer than the other? Try it!
+//
+// By the way, congratulations for making it to Exercise 100!
+//
+// +-------------+
+// | Celebration |
+// | Area * * * |
+// +-------------+
+//
+// Please keep your celebrating within the area provided.
diff --git a/exercises/101_for5.zig b/exercises/101_for5.zig
new file mode 100644
index 0000000..3861417
--- /dev/null
+++ b/exercises/101_for5.zig
@@ -0,0 +1,120 @@
+//
+// The 'for' loop is not just limited to looping over one or two
+// items. Let's try an example with a whole bunch!
+//
+// But first, there's one last thing we've avoided mentioning
+// until now: The special range that leaves off the last value:
+//
+// for ( things, 0.. ) |t, i| { ... }
+//
+// That's how we tell Zig that we want to get a numeric value for
+// every item in "things", starting with 0.
+//
+// A nice feature of these index ranges is that you can have them
+// start with any number you choose. The first value of "i" in
+// this example will be 500, then 501, 502, etc.:
+//
+// for ( things, 500.. ) |t, i| { ... }
+//
+// Remember our RPG characters? They had the following
+// properties, which we stored in a struct type:
+//
+// class
+// gold
+// experience
+//
+// What we're going to do now is store the same RPG character
+// data, but in a separate array for each property.
+//
+// It might look a little awkward, but let's bear with it.
+//
+// We've started writing a program to print a numbered list of
+// characters with each of their properties, but it needs a
+// little help:
+//
+const std = @import("std");
+const print = std.debug.print;
+
+// This is the same character class enum we've seen before.
+const Class = enum {
+ wizard,
+ thief,
+ bard,
+ warrior,
+};
+
+pub fn main() void {
+ // Here are the three "property" arrays:
+ const classes = [4]Class{ .wizard, .bard, .bard, .warrior };
+ const gold = [4]u16{ 25, 11, 5, 7392 };
+ const experience = [4]u8{ 40, 17, 55, 21 };
+
+ // We would like to number our list starting with 1, not 0.
+ // How do we do that?
+ for (classes, gold, experience, ???) |c, g, e, i| {
+ const class_name = switch (c) {
+ .wizard => "Wizard",
+ .thief => "Thief",
+ .bard => "Bard",
+ .warrior => "Warrior",
+ };
+
+ std.debug.print("{d}. {s} (Gold: {d}, XP: {d})\n", .{
+ i,
+ class_name,
+ g,
+ e,
+ });
+ }
+}
+//
+// By the way, storing our character data in arrays like this
+// isn't *just* a silly way to demonstrate multi-object 'for'
+// loops.
+//
+// It's *also* a silly way to introduce a concept called
+// "data-oriented design".
+//
+// Let's use a metaphor to build up an intuition for what this is
+// all about:
+//
+// Let's say you've been tasked with grabbing three glass
+// marbles, three spoons, and three feathers from a bucket. But
+// you can't use your hands to grab them. Instead, you have a
+// special marble scoop, spoon magnet, and feather tongs to grab
+// each type of object.
+//
+// Now, would you rather have:
+//
+// A. The items layered so you have to pick up one marble, then
+// one spoon, then one feather?
+//
+// OR
+//
+// B. The items separated by type so you can pick up all of the
+// marbles at once, then all the spoons, then all of the
+// feathers?
+//
+// If this metaphor is working, hopefully it's clear that the 'B'
+// option would be much more efficient.
+//
+// Well, it probably comes as little surprise that storing and
+// using data in a sequential and uniform fashion is also more
+// efficient for modern CPUs.
+//
+// Decades of OOP practices have steered people towards grouping
+// different data types together into "objects" with the hope
+// that it would be friendlier to the human mind. But
+// data-oriented design groups data in a way that is more
+// efficient for the computer.
+//
+// In Zig terminology, the difference in groupings is sometimes
+// known as "Array of Structs" (AoS) versus "Struct of Arrays"
+// (SoA).
+//
+// To envision these two designs in action, imagine an array of
+// RPG character structs, each containing three different data
+// types (AoS) versus a single RPG character struct containing
+// three arrays of one data type each, like those in the exercise
+// above (SoA).
+//
diff --git a/patches/eowyn.sh b/patches/eowyn.sh
index bc2fb7c..95b2a1c 100755
--- a/patches/eowyn.sh
+++ b/patches/eowyn.sh
@@ -45,13 +45,8 @@ do
fi
done
-# Test the correct formatting of the healed exercises.
echo "Looking for non-conforming code formatting..."
-for healed in patches/healed/*.zig
-do
- echo Check $(basename "$healed")
- zig fmt --check "$healed"
-done
+zig fmt --check patches/healed
# Test the healed exercises. May the compiler have mercy upon us.
zig build -Dhealed
diff --git a/patches/patches/095_for_loops.patch b/patches/patches/095_for3.patch
index 5eaecef..c258b7b 100644
--- a/patches/patches/095_for_loops.patch
+++ b/patches/patches/095_for3.patch
@@ -1,4 +1,4 @@
-49c49
+57c57
< for (???) |n| {
---
> for (1..21) |n| {
diff --git a/patches/patches/100_for4.patch b/patches/patches/100_for4.patch
new file mode 100644
index 0000000..6465325
--- /dev/null
+++ b/patches/patches/100_for4.patch
@@ -0,0 +1,4 @@
+42c42
+< for (hex_nums, ???) |hn, ???| {
+---
+> for (hex_nums, dec_nums) |hn, dn| {
diff --git a/patches/patches/101_for5.patch b/patches/patches/101_for5.patch
new file mode 100644
index 0000000..0466cc8
--- /dev/null
+++ b/patches/patches/101_for5.patch
@@ -0,0 +1,4 @@
+54c54
+< for (classes, gold, experience, ???) |c, g, e, i| {
+---
+> for (classes, gold, experience, 1..) |c, g, e, i| {
diff --git a/src/compat.zig b/src/compat.zig
index cd7f3e5..42ecb6b 100644
--- a/src/compat.zig
+++ b/src/compat.zig
@@ -15,7 +15,7 @@ const print = if (@hasDecl(debug, "print")) debug.print else debug.warn;
// When changing this version, be sure to also update README.md in two places:
// 1) Getting Started
// 2) Version Changes
-const needed_version_str = "0.11.0-dev.2560";
+const needed_version_str = "0.11.0-dev.2704";
fn isCompatible() bool {
if (!@hasDecl(builtin, "zig_version") or !@hasDecl(std, "SemanticVersion")) {
diff --git a/test/tests.zig b/test/tests.zig
index f91c4fd..752ca50 100644
--- a/test/tests.zig
+++ b/test/tests.zig
@@ -20,15 +20,14 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
// We should use a temporary path, but it will make the implementation of
// `build.zig` more complex.
- const outdir = "patches/healed";
+ const work_path = "patches/healed";
- fs.cwd().makePath(outdir) catch |err| {
- return fail(step, "unable to make '{s}': {s}\n", .{ outdir, @errorName(err) });
- };
- heal(b.allocator, exercises, outdir) catch |err| {
- return fail(step, "unable to heal exercises: {s}\n", .{@errorName(err)});
+ fs.cwd().makePath(work_path) catch |err| {
+ return fail(step, "unable to make '{s}': {s}\n", .{ work_path, @errorName(err) });
};
+ const heal_step = HealStep.create(b, exercises, work_path);
+
{
// Test that `zig build -Dhealed -Dn=n test` selects the nth exercise.
const case_step = createCase(b, "case-1");
@@ -49,6 +48,8 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
else
expectStdErrMatch(cmd, ex.output);
+ cmd.step.dependOn(&heal_step.step);
+
case_step.dependOn(&cmd.step);
}
@@ -72,6 +73,8 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
cmd.expectStdOutEqual("");
expectStdErrMatch(cmd, b.fmt("{s} skipped", .{ex.main_file}));
+ cmd.step.dependOn(&heal_step.step);
+
case_step.dependOn(&cmd.step);
}
@@ -86,6 +89,7 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
const cmd = b.addSystemCommand(&.{ b.zig_exe, "build", "-Dhealed" });
cmd.setName("zig build -Dhealed");
cmd.expectExitCode(0);
+ cmd.step.dependOn(&heal_step.step);
const stderr = cmd.captureStdErr();
const verify = CheckStep.create(b, exercises, stderr, true);
@@ -107,6 +111,7 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
);
cmd.setName("zig build -Dhealed -Dn=1 start");
cmd.expectExitCode(0);
+ cmd.step.dependOn(&heal_step.step);
const stderr = cmd.captureStdErr();
const verify = CheckStep.create(b, exercises, stderr, false);
@@ -126,14 +131,16 @@ pub fn addCliTests(b: *std.Build, exercises: []const Exercise) *Step {
cmd.expectExitCode(1);
expectStdErrMatch(cmd, exercises[0].hint);
+ cmd.step.dependOn(&heal_step.step);
+
case_step.dependOn(&cmd.step);
step.dependOn(case_step);
}
- // Don't add the cleanup step, since it may delete outdir while a test case
- // is running.
- //const cleanup = b.addRemoveDirTree(outdir);
+ // Don't add the cleanup step, since it may delete work_path while a test
+ // case is running.
+ //const cleanup = b.addRemoveDirTree(work_path);
//step.dependOn(&cleanup.step);
return step;
@@ -315,15 +322,45 @@ fn fail(step: *Step, comptime format: []const u8, args: anytype) *Step {
return step;
}
+// A step that heals exercises.
+const HealStep = struct {
+ step: Step,
+ exercises: []const Exercise,
+ work_path: []const u8,
+
+ pub fn create(owner: *Build, exercises: []const Exercise, work_path: []const u8) *HealStep {
+ const self = owner.allocator.create(HealStep) catch @panic("OOM");
+ self.* = .{
+ .step = Step.init(.{
+ .id = .custom,
+ .name = "heal",
+ .owner = owner,
+ .makeFn = make,
+ }),
+ .exercises = exercises,
+ .work_path = work_path,
+ };
+
+ return self;
+ }
+
+ fn make(step: *Step, _: *std.Progress.Node) !void {
+ const b = step.owner;
+ const self = @fieldParentPtr(HealStep, "step", step);
+
+ return heal(b.allocator, self.exercises, self.work_path);
+ }
+};
+
// Heals all the exercises.
-fn heal(allocator: Allocator, exercises: []const Exercise, outdir: []const u8) !void {
+fn heal(allocator: Allocator, exercises: []const Exercise, work_path: []const u8) !void {
const join = fs.path.join;
const exercises_path = "exercises";
const patches_path = "patches/patches";
for (exercises) |ex| {
- const name = ex.baseName();
+ const name = ex.name();
// Use the POSIX patch variant.
const file = try join(allocator, &.{ exercises_path, ex.main_file });
@@ -331,12 +368,11 @@ fn heal(allocator: Allocator, exercises: []const Exercise, outdir: []const u8) !
const patch_name = try fmt.allocPrint(allocator, "{s}.patch", .{name});
break :b try join(allocator, &.{ patches_path, patch_name });
};
- const output = try join(allocator, &.{ outdir, ex.main_file });
+ const output = try join(allocator, &.{ work_path, ex.main_file });
- const argv = &.{ "patch", "-i", patch, "-o", output, file };
+ const argv = &.{ "patch", "-i", patch, "-o", output, "-s", file };
var child = std.process.Child.init(argv, allocator);
- child.stdout_behavior = .Ignore; // the POSIX standard says that stdout is not used
_ = try child.spawnAndWait();
}
}