2021-02-12 05:04:36 +01:00
|
|
|
const std = @import("std");
|
2021-10-09 16:19:34 +02:00
|
|
|
const builtin = @import("builtin");
|
2023-04-03 11:06:39 +02:00
|
|
|
const compat = @import("src/compat.zig");
|
2023-04-18 12:22:30 +02:00
|
|
|
const ipc = @import("src/ipc.zig");
|
2023-04-09 18:33:59 +02:00
|
|
|
const tests = @import("test/tests.zig");
|
2023-04-03 11:06:39 +02:00
|
|
|
|
|
|
|
const Build = compat.Build;
|
2023-04-30 17:47:16 +02:00
|
|
|
const CompileStep = compat.build.CompileStep;
|
2023-04-03 11:06:39 +02:00
|
|
|
const Step = compat.build.Step;
|
2023-04-13 18:03:00 +02:00
|
|
|
const Child = std.process.Child;
|
2023-04-03 11:06:39 +02:00
|
|
|
|
2021-02-12 05:04:36 +01:00
|
|
|
const assert = std.debug.assert;
|
2023-04-30 16:07:30 +02:00
|
|
|
const join = std.fs.path.join;
|
2021-02-12 05:04:36 +01:00
|
|
|
const print = std.debug.print;
|
|
|
|
|
2023-04-09 18:33:59 +02:00
|
|
|
pub const Exercise = struct {
|
2021-02-12 05:04:36 +01:00
|
|
|
/// main_file must have the format key_name.zig.
|
2023-05-06 12:02:21 +02:00
|
|
|
/// The key will be used as a shorthand to build just one example.
|
2021-02-12 05:04:36 +01:00
|
|
|
main_file: []const u8,
|
|
|
|
|
|
|
|
/// This is the desired output of the program.
|
2023-04-24 11:20:07 +02:00
|
|
|
/// A program passes if its output, excluding trailing whitespace, is equal
|
|
|
|
/// to this string.
|
2021-02-12 05:04:36 +01:00
|
|
|
output: []const u8,
|
|
|
|
|
|
|
|
/// This is an optional hint to give if the program does not succeed.
|
2023-05-06 11:50:42 +02:00
|
|
|
hint: ?[]const u8 = null,
|
2021-02-12 05:04:36 +01:00
|
|
|
|
|
|
|
/// By default, we verify output against stderr.
|
|
|
|
/// Set this to true to check stdout instead.
|
|
|
|
check_stdout: bool = false,
|
|
|
|
|
2023-05-06 12:02:21 +02:00
|
|
|
/// This exercise makes use of C functions.
|
|
|
|
/// We need to keep track of this, so we compile with libc.
|
2023-04-30 16:25:15 +02:00
|
|
|
link_libc: bool = false,
|
2023-02-15 22:55:44 +01:00
|
|
|
|
2023-04-08 12:56:51 +02:00
|
|
|
/// This exercise is not supported by the current Zig compiler.
|
|
|
|
skip: bool = false,
|
|
|
|
|
2021-02-12 05:04:36 +01:00
|
|
|
/// Returns the name of the main file with .zig stripped.
|
2023-04-30 18:48:52 +02:00
|
|
|
pub fn name(self: Exercise) []const u8 {
|
|
|
|
return std.fs.path.stem(self.main_file);
|
2021-02-12 05:04:36 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 00:59:46 +01:00
|
|
|
/// Returns the key of the main file, the string before the '_' with
|
|
|
|
/// "zero padding" removed.
|
|
|
|
/// For example, "001_hello.zig" has the key "1".
|
2021-02-12 05:04:36 +01:00
|
|
|
pub fn key(self: Exercise) []const u8 {
|
2023-05-06 12:02:21 +02:00
|
|
|
// Main file must be key_description.zig.
|
|
|
|
const end_index = std.mem.indexOfScalar(u8, self.main_file, '_') orelse
|
|
|
|
unreachable;
|
2021-03-13 00:59:46 +01:00
|
|
|
|
2023-05-06 12:02:21 +02:00
|
|
|
// Remove zero padding by advancing index past '0's.
|
2021-03-13 00:59:46 +01:00
|
|
|
var start_index: usize = 0;
|
|
|
|
while (self.main_file[start_index] == '0') start_index += 1;
|
2023-05-06 12:02:21 +02:00
|
|
|
return self.main_file[start_index..end_index];
|
2021-02-12 05:04:36 +01:00
|
|
|
}
|
build: restore the exercise chain
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.
1. Add the Dexno option, in order to isolate the chain starting from a
named exercise from the normal chain, thus simplify the code.
The current code have an additional issue: it added 4 x n steps,
making reading the help message or the list of steps very hard.
Add only the `install`, `uninstall`, `zigling`, `test` and `start`
steps. The last three steps match the old steps `n`, `n_test` and
`n_start`.
The default step is zigling (note the singular form).
The `install` step override the builtin install step, showing a
custom description and matches the old `n_install` step.
The uninstall step was added for consistency, so that the
description is consistent.
Setup a new chain starting at `zig build -Dexno=n start` so that it
is stricly serialized.
The behavior should be the same as the old one.
2. Handle the code for all the exercises separately.
Add only the `ziglings step`, making it the default step, in
addition to the install and uninstall steps.
Setup a new chain starting at the first exercise, to that it is
strictly serialized.
The behavior should be the same as the old one.
The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.
Update the README.md file.
Closes #202
2023-04-06 12:47:08 +02:00
|
|
|
|
|
|
|
/// Returns the exercise key as an integer.
|
|
|
|
pub fn number(self: Exercise) usize {
|
|
|
|
return std.fmt.parseInt(usize, self.key(), 10) catch unreachable;
|
|
|
|
}
|
2023-04-30 17:47:16 +02:00
|
|
|
|
|
|
|
/// Returns the CompileStep for this exercise.
|
|
|
|
pub fn addExecutable(self: Exercise, b: *Build, work_path: []const u8) *CompileStep {
|
2023-05-06 18:18:08 +02:00
|
|
|
const path = join(b.allocator, &.{ work_path, self.main_file }) catch
|
2023-04-30 17:47:16 +02:00
|
|
|
@panic("OOM");
|
|
|
|
|
|
|
|
return b.addExecutable(.{
|
2023-04-30 18:48:52 +02:00
|
|
|
.name = self.name(),
|
2023-05-06 18:18:08 +02:00
|
|
|
.root_source_file = .{ .path = path },
|
2023-04-30 17:47:16 +02:00
|
|
|
.link_libc = self.link_libc,
|
|
|
|
});
|
|
|
|
}
|
2021-02-12 05:04:36 +01:00
|
|
|
};
|
|
|
|
|
2023-04-03 11:06:39 +02:00
|
|
|
pub fn build(b: *Build) !void {
|
|
|
|
if (!compat.is_compatible) compat.die();
|
2023-04-09 18:33:59 +02:00
|
|
|
if (!validate_exercises()) std.os.exit(1);
|
2021-02-12 05:04:36 +01:00
|
|
|
|
|
|
|
use_color_escapes = false;
|
2023-03-19 00:59:42 +01:00
|
|
|
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;
|
|
|
|
}
|
2021-02-12 05:04:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (use_color_escapes) {
|
|
|
|
red_text = "\x1b[31m";
|
|
|
|
green_text = "\x1b[32m";
|
|
|
|
bold_text = "\x1b[1m";
|
|
|
|
reset_text = "\x1b[0m";
|
|
|
|
}
|
|
|
|
|
2023-03-19 00:59:42 +01:00
|
|
|
const logo =
|
2021-02-12 05:04:36 +01:00
|
|
|
\\ _ _ _
|
|
|
|
\\ ___(_) __ _| (_)_ __ __ _ ___
|
|
|
|
\\ |_ | |/ _' | | | '_ \ / _' / __|
|
2021-03-03 05:06:09 +01:00
|
|
|
\\ / /| | (_| | | | | | | (_| \__ \
|
2021-02-12 05:04:36 +01:00
|
|
|
\\ /___|_|\__, |_|_|_| |_|\__, |___/
|
|
|
|
\\ |___/ |___/
|
|
|
|
\\
|
2023-05-08 15:20:27 +02:00
|
|
|
\\ "Look out! Broken programs below!"
|
|
|
|
\\
|
2021-02-12 05:04:36 +01:00
|
|
|
\\
|
2023-03-19 00:59:42 +01:00
|
|
|
;
|
2023-04-02 14:59:22 +02:00
|
|
|
|
2023-05-05 16:13:57 +02:00
|
|
|
const healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse false;
|
|
|
|
const override_healed_path = b.option([]const u8, "healed-path", "Override healed path");
|
build: restore the exercise chain
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.
1. Add the Dexno option, in order to isolate the chain starting from a
named exercise from the normal chain, thus simplify the code.
The current code have an additional issue: it added 4 x n steps,
making reading the help message or the list of steps very hard.
Add only the `install`, `uninstall`, `zigling`, `test` and `start`
steps. The last three steps match the old steps `n`, `n_test` and
`n_start`.
The default step is zigling (note the singular form).
The `install` step override the builtin install step, showing a
custom description and matches the old `n_install` step.
The uninstall step was added for consistency, so that the
description is consistent.
Setup a new chain starting at `zig build -Dexno=n start` so that it
is stricly serialized.
The behavior should be the same as the old one.
2. Handle the code for all the exercises separately.
Add only the `ziglings step`, making it the default step, in
addition to the install and uninstall steps.
Setup a new chain starting at the first exercise, to that it is
strictly serialized.
The behavior should be the same as the old one.
The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.
Update the README.md file.
Closes #202
2023-04-06 12:47:08 +02:00
|
|
|
const exno: ?usize = b.option(usize, "n", "Select exercise");
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-05-05 16:13:57 +02:00
|
|
|
const healed_path = if (override_healed_path) |path| path else "patches/healed";
|
|
|
|
const work_path = if (healed) healed_path else "exercises";
|
2023-04-30 16:07:30 +02:00
|
|
|
|
2023-04-10 21:24:49 +02:00
|
|
|
const header_step = PrintStep.create(b, logo);
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-04-10 18:14:58 +02:00
|
|
|
if (exno) |n| {
|
|
|
|
if (n == 0 or n > exercises.len - 1) {
|
|
|
|
print("unknown exercise number: {}\n", .{n});
|
build: restore the exercise chain
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.
1. Add the Dexno option, in order to isolate the chain starting from a
named exercise from the normal chain, thus simplify the code.
The current code have an additional issue: it added 4 x n steps,
making reading the help message or the list of steps very hard.
Add only the `install`, `uninstall`, `zigling`, `test` and `start`
steps. The last three steps match the old steps `n`, `n_test` and
`n_start`.
The default step is zigling (note the singular form).
The `install` step override the builtin install step, showing a
custom description and matches the old `n_install` step.
The uninstall step was added for consistency, so that the
description is consistent.
Setup a new chain starting at `zig build -Dexno=n start` so that it
is stricly serialized.
The behavior should be the same as the old one.
2. Handle the code for all the exercises separately.
Add only the `ziglings step`, making it the default step, in
addition to the install and uninstall steps.
Setup a new chain starting at the first exercise, to that it is
strictly serialized.
The behavior should be the same as the old one.
The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.
Update the README.md file.
Closes #202
2023-04-06 12:47:08 +02:00
|
|
|
std.os.exit(1);
|
2023-04-09 18:33:59 +02:00
|
|
|
}
|
2023-04-10 18:14:58 +02:00
|
|
|
const ex = exercises[n - 1];
|
2023-02-02 04:51:47 +01:00
|
|
|
|
2023-04-30 17:47:16 +02:00
|
|
|
const build_step = ex.addExecutable(b, work_path);
|
2023-05-07 20:01:02 +02:00
|
|
|
|
|
|
|
const skip_step = SkipStep.create(b, ex);
|
|
|
|
if (!ex.skip)
|
|
|
|
b.installArtifact(build_step)
|
|
|
|
else
|
|
|
|
b.getInstallStep().dependOn(&skip_step.step);
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-04-12 18:41:40 +02:00
|
|
|
const run_step = b.addRunArtifact(build_step);
|
build: restore the exercise chain
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.
1. Add the Dexno option, in order to isolate the chain starting from a
named exercise from the normal chain, thus simplify the code.
The current code have an additional issue: it added 4 x n steps,
making reading the help message or the list of steps very hard.
Add only the `install`, `uninstall`, `zigling`, `test` and `start`
steps. The last three steps match the old steps `n`, `n_test` and
`n_start`.
The default step is zigling (note the singular form).
The `install` step override the builtin install step, showing a
custom description and matches the old `n_install` step.
The uninstall step was added for consistency, so that the
description is consistent.
Setup a new chain starting at `zig build -Dexno=n start` so that it
is stricly serialized.
The behavior should be the same as the old one.
2. Handle the code for all the exercises separately.
Add only the `ziglings step`, making it the default step, in
addition to the install and uninstall steps.
Setup a new chain starting at the first exercise, to that it is
strictly serialized.
The behavior should be the same as the old one.
The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.
Update the README.md file.
Closes #202
2023-04-06 12:47:08 +02:00
|
|
|
|
|
|
|
const test_step = b.step("test", b.fmt("Run {s} without checking output", .{ex.main_file}));
|
2023-04-08 12:56:51 +02:00
|
|
|
if (ex.skip) {
|
|
|
|
test_step.dependOn(&skip_step.step);
|
|
|
|
} else {
|
|
|
|
test_step.dependOn(&run_step.step);
|
|
|
|
}
|
build: restore the exercise chain
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.
1. Add the Dexno option, in order to isolate the chain starting from a
named exercise from the normal chain, thus simplify the code.
The current code have an additional issue: it added 4 x n steps,
making reading the help message or the list of steps very hard.
Add only the `install`, `uninstall`, `zigling`, `test` and `start`
steps. The last three steps match the old steps `n`, `n_test` and
`n_start`.
The default step is zigling (note the singular form).
The `install` step override the builtin install step, showing a
custom description and matches the old `n_install` step.
The uninstall step was added for consistency, so that the
description is consistent.
Setup a new chain starting at `zig build -Dexno=n start` so that it
is stricly serialized.
The behavior should be the same as the old one.
2. Handle the code for all the exercises separately.
Add only the `ziglings step`, making it the default step, in
addition to the install and uninstall steps.
Setup a new chain starting at the first exercise, to that it is
strictly serialized.
The behavior should be the same as the old one.
The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.
Update the README.md file.
Closes #202
2023-04-06 12:47:08 +02:00
|
|
|
|
2023-04-30 16:07:30 +02:00
|
|
|
const verify_step = ZiglingStep.create(b, ex, work_path);
|
2021-02-12 05:04:36 +01:00
|
|
|
|
build: restore the exercise chain
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.
1. Add the Dexno option, in order to isolate the chain starting from a
named exercise from the normal chain, thus simplify the code.
The current code have an additional issue: it added 4 x n steps,
making reading the help message or the list of steps very hard.
Add only the `install`, `uninstall`, `zigling`, `test` and `start`
steps. The last three steps match the old steps `n`, `n_test` and
`n_start`.
The default step is zigling (note the singular form).
The `install` step override the builtin install step, showing a
custom description and matches the old `n_install` step.
The uninstall step was added for consistency, so that the
description is consistent.
Setup a new chain starting at `zig build -Dexno=n start` so that it
is stricly serialized.
The behavior should be the same as the old one.
2. Handle the code for all the exercises separately.
Add only the `ziglings step`, making it the default step, in
addition to the install and uninstall steps.
Setup a new chain starting at the first exercise, to that it is
strictly serialized.
The behavior should be the same as the old one.
The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.
Update the README.md file.
Closes #202
2023-04-06 12:47:08 +02:00
|
|
|
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;
|
2021-02-12 05:04:36 +01:00
|
|
|
|
build: restore the exercise chain
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.
1. Add the Dexno option, in order to isolate the chain starting from a
named exercise from the normal chain, thus simplify the code.
The current code have an additional issue: it added 4 x n steps,
making reading the help message or the list of steps very hard.
Add only the `install`, `uninstall`, `zigling`, `test` and `start`
steps. The last three steps match the old steps `n`, `n_test` and
`n_start`.
The default step is zigling (note the singular form).
The `install` step override the builtin install step, showing a
custom description and matches the old `n_install` step.
The uninstall step was added for consistency, so that the
description is consistent.
Setup a new chain starting at `zig build -Dexno=n start` so that it
is stricly serialized.
The behavior should be the same as the old one.
2. Handle the code for all the exercises separately.
Add only the `ziglings step`, making it the default step, in
addition to the install and uninstall steps.
Setup a new chain starting at the first exercise, to that it is
strictly serialized.
The behavior should be the same as the old one.
The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.
Update the README.md file.
Closes #202
2023-04-06 12:47:08 +02:00
|
|
|
const start_step = b.step("start", b.fmt("Check all solutions starting at {s}", .{ex.main_file}));
|
2021-02-12 05:04:36 +01:00
|
|
|
|
build: restore the exercise chain
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.
1. Add the Dexno option, in order to isolate the chain starting from a
named exercise from the normal chain, thus simplify the code.
The current code have an additional issue: it added 4 x n steps,
making reading the help message or the list of steps very hard.
Add only the `install`, `uninstall`, `zigling`, `test` and `start`
steps. The last three steps match the old steps `n`, `n_test` and
`n_start`.
The default step is zigling (note the singular form).
The `install` step override the builtin install step, showing a
custom description and matches the old `n_install` step.
The uninstall step was added for consistency, so that the
description is consistent.
Setup a new chain starting at `zig build -Dexno=n start` so that it
is stricly serialized.
The behavior should be the same as the old one.
2. Handle the code for all the exercises separately.
Add only the `ziglings step`, making it the default step, in
addition to the install and uninstall steps.
Setup a new chain starting at the first exercise, to that it is
strictly serialized.
The behavior should be the same as the old one.
The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.
Update the README.md file.
Closes #202
2023-04-06 12:47:08 +02:00
|
|
|
var prev_step = verify_step;
|
|
|
|
for (exercises) |exn| {
|
2023-04-10 18:14:58 +02:00
|
|
|
const nth = exn.number();
|
|
|
|
if (nth > n) {
|
2023-04-30 16:07:30 +02:00
|
|
|
const verify_stepn = ZiglingStep.create(b, exn, work_path);
|
build: restore the exercise chain
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.
1. Add the Dexno option, in order to isolate the chain starting from a
named exercise from the normal chain, thus simplify the code.
The current code have an additional issue: it added 4 x n steps,
making reading the help message or the list of steps very hard.
Add only the `install`, `uninstall`, `zigling`, `test` and `start`
steps. The last three steps match the old steps `n`, `n_test` and
`n_start`.
The default step is zigling (note the singular form).
The `install` step override the builtin install step, showing a
custom description and matches the old `n_install` step.
The uninstall step was added for consistency, so that the
description is consistent.
Setup a new chain starting at `zig build -Dexno=n start` so that it
is stricly serialized.
The behavior should be the same as the old one.
2. Handle the code for all the exercises separately.
Add only the `ziglings step`, making it the default step, in
addition to the install and uninstall steps.
Setup a new chain starting at the first exercise, to that it is
strictly serialized.
The behavior should be the same as the old one.
The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.
Update the README.md file.
Closes #202
2023-04-06 12:47:08 +02:00
|
|
|
verify_stepn.step.dependOn(&prev_step.step);
|
2021-02-12 05:04:36 +01:00
|
|
|
|
build: restore the exercise chain
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.
1. Add the Dexno option, in order to isolate the chain starting from a
named exercise from the normal chain, thus simplify the code.
The current code have an additional issue: it added 4 x n steps,
making reading the help message or the list of steps very hard.
Add only the `install`, `uninstall`, `zigling`, `test` and `start`
steps. The last three steps match the old steps `n`, `n_test` and
`n_start`.
The default step is zigling (note the singular form).
The `install` step override the builtin install step, showing a
custom description and matches the old `n_install` step.
The uninstall step was added for consistency, so that the
description is consistent.
Setup a new chain starting at `zig build -Dexno=n start` so that it
is stricly serialized.
The behavior should be the same as the old one.
2. Handle the code for all the exercises separately.
Add only the `ziglings step`, making it the default step, in
addition to the install and uninstall steps.
Setup a new chain starting at the first exercise, to that it is
strictly serialized.
The behavior should be the same as the old one.
The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.
Update the README.md file.
Closes #202
2023-04-06 12:47:08 +02:00
|
|
|
prev_step = verify_stepn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
start_step.dependOn(&prev_step.step);
|
|
|
|
|
2023-04-08 09:02:38 +02:00
|
|
|
return;
|
2023-05-05 16:13:57 +02:00
|
|
|
} else if (healed and false) {
|
2023-04-30 16:07:30 +02:00
|
|
|
// 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.
|
2023-04-08 09:02:38 +02:00
|
|
|
const test_step = b.step("test", "Test the healed exercises");
|
|
|
|
b.default_step = test_step;
|
|
|
|
|
|
|
|
for (exercises) |ex| {
|
2023-04-30 17:47:16 +02:00
|
|
|
const build_step = ex.addExecutable(b, healed_path);
|
2023-04-12 18:41:40 +02:00
|
|
|
b.installArtifact(build_step);
|
2023-04-08 09:02:38 +02:00
|
|
|
|
2023-04-12 18:41:40 +02:00
|
|
|
const run_step = b.addRunArtifact(build_step);
|
2023-04-08 12:56:51 +02:00
|
|
|
if (ex.skip) {
|
|
|
|
const skip_step = SkipStep.create(b, ex);
|
|
|
|
test_step.dependOn(&skip_step.step);
|
|
|
|
} else {
|
|
|
|
test_step.dependOn(&run_step.step);
|
|
|
|
}
|
2023-04-08 09:02:38 +02:00
|
|
|
}
|
|
|
|
|
build: restore the exercise chain
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.
1. Add the Dexno option, in order to isolate the chain starting from a
named exercise from the normal chain, thus simplify the code.
The current code have an additional issue: it added 4 x n steps,
making reading the help message or the list of steps very hard.
Add only the `install`, `uninstall`, `zigling`, `test` and `start`
steps. The last three steps match the old steps `n`, `n_test` and
`n_start`.
The default step is zigling (note the singular form).
The `install` step override the builtin install step, showing a
custom description and matches the old `n_install` step.
The uninstall step was added for consistency, so that the
description is consistent.
Setup a new chain starting at `zig build -Dexno=n start` so that it
is stricly serialized.
The behavior should be the same as the old one.
2. Handle the code for all the exercises separately.
Add only the `ziglings step`, making it the default step, in
addition to the install and uninstall steps.
Setup a new chain starting at the first exercise, to that it is
strictly serialized.
The behavior should be the same as the old one.
The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.
Update the README.md file.
Closes #202
2023-04-06 12:47:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ziglings_step = b.step("ziglings", "Check all ziglings");
|
|
|
|
b.default_step = ziglings_step;
|
|
|
|
|
2023-04-10 21:02:54 +02:00
|
|
|
var prev_step = &header_step.step;
|
2023-04-10 18:14:58 +02:00
|
|
|
for (exercises) |ex| {
|
2023-05-07 16:25:35 +02:00
|
|
|
const build_step = ex.addExecutable(b, work_path);
|
2023-05-07 20:01:02 +02:00
|
|
|
|
|
|
|
const skip_step = SkipStep.create(b, ex);
|
|
|
|
if (!ex.skip)
|
|
|
|
b.installArtifact(build_step)
|
|
|
|
else
|
|
|
|
b.getInstallStep().dependOn(&skip_step.step);
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-04-30 16:07:30 +02:00
|
|
|
const verify_stepn = ZiglingStep.create(b, ex, work_path);
|
2023-04-10 21:02:54 +02:00
|
|
|
verify_stepn.step.dependOn(prev_step);
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-04-10 21:02:54 +02:00
|
|
|
prev_step = &verify_stepn.step;
|
2023-03-19 18:23:35 +01:00
|
|
|
}
|
build: restore the exercise chain
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.
1. Add the Dexno option, in order to isolate the chain starting from a
named exercise from the normal chain, thus simplify the code.
The current code have an additional issue: it added 4 x n steps,
making reading the help message or the list of steps very hard.
Add only the `install`, `uninstall`, `zigling`, `test` and `start`
steps. The last three steps match the old steps `n`, `n_test` and
`n_start`.
The default step is zigling (note the singular form).
The `install` step override the builtin install step, showing a
custom description and matches the old `n_install` step.
The uninstall step was added for consistency, so that the
description is consistent.
Setup a new chain starting at `zig build -Dexno=n start` so that it
is stricly serialized.
The behavior should be the same as the old one.
2. Handle the code for all the exercises separately.
Add only the `ziglings step`, making it the default step, in
addition to the install and uninstall steps.
Setup a new chain starting at the first exercise, to that it is
strictly serialized.
The behavior should be the same as the old one.
The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.
Update the README.md file.
Closes #202
2023-04-06 12:47:08 +02:00
|
|
|
ziglings_step.dependOn(prev_step);
|
2023-04-09 18:33:59 +02:00
|
|
|
|
2023-05-02 11:06:58 +02:00
|
|
|
const test_step = b.step("test", "Run all the tests");
|
|
|
|
test_step.dependOn(tests.addCliTests(b, &exercises));
|
2021-02-12 05:04:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-04-30 16:07:30 +02:00
|
|
|
work_path: []const u8,
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-04-17 15:52:57 +02:00
|
|
|
result_messages: []const u8 = "",
|
|
|
|
result_error_bundle: std.zig.ErrorBundle = std.zig.ErrorBundle.empty,
|
|
|
|
|
2023-05-03 17:44:47 +02:00
|
|
|
pub fn create(b: *Build, exercise: Exercise, work_path: []const u8) *ZiglingStep {
|
|
|
|
const self = b.allocator.create(ZiglingStep) catch @panic("OOM");
|
2021-02-12 05:04:36 +01:00
|
|
|
self.* = .{
|
2023-05-03 17:44:47 +02:00
|
|
|
.step = Step.init(.{
|
|
|
|
.id = .custom,
|
|
|
|
.name = exercise.main_file,
|
|
|
|
.owner = b,
|
|
|
|
.makeFn = make,
|
|
|
|
}),
|
2021-02-12 05:04:36 +01:00
|
|
|
.exercise = exercise,
|
2023-04-30 16:07:30 +02:00
|
|
|
.work_path = work_path,
|
2021-02-12 05:04:36 +01:00
|
|
|
};
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2023-05-01 17:12:05 +02:00
|
|
|
fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
2023-05-03 12:34:33 +02:00
|
|
|
const self = @fieldParentPtr(ZiglingStep, "step", step);
|
2023-04-08 12:56:51 +02:00
|
|
|
|
|
|
|
if (self.exercise.skip) {
|
|
|
|
print("Skipping {s}\n\n", .{self.exercise.main_file});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-05-01 17:12:05 +02:00
|
|
|
|
|
|
|
const exe_path = self.compile(prog_node) catch {
|
2023-05-06 11:50:42 +02:00
|
|
|
if (self.exercise.hint) |hint|
|
2023-05-08 15:20:27 +02:00
|
|
|
print("\n{s}Ziglings hint: {s}{s}", .{ bold_text, hint, reset_text });
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-05-01 17:12:05 +02:00
|
|
|
self.help();
|
2023-05-08 15:20:27 +02:00
|
|
|
|
|
|
|
// NOTE: Returning 0 'success' status because the *exercise* failed,
|
|
|
|
// but Ziglings did not. Otherwise the learner will see this message:
|
|
|
|
// "error: the following build command failed with exit code 1:..."
|
|
|
|
std.os.exit(0);
|
2021-02-12 05:04:36 +01:00
|
|
|
};
|
|
|
|
|
2023-05-01 17:12:05 +02:00
|
|
|
self.run(exe_path, prog_node) catch {
|
2023-05-06 11:50:42 +02:00
|
|
|
if (self.exercise.hint) |hint|
|
2023-05-08 15:20:27 +02:00
|
|
|
print("\n{s}Ziglings hint: {s}{s}", .{ bold_text, hint, reset_text });
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-05-01 17:12:05 +02:00
|
|
|
self.help();
|
2023-05-08 15:20:27 +02:00
|
|
|
|
|
|
|
// NOTE: See note above!
|
|
|
|
std.os.exit(0);
|
2023-05-01 17:12:05 +02:00
|
|
|
};
|
|
|
|
}
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-05-03 12:34:33 +02:00
|
|
|
fn run(self: *ZiglingStep, exe_path: []const u8, _: *std.Progress.Node) !void {
|
2023-04-17 19:09:30 +02:00
|
|
|
resetLine();
|
2021-02-14 22:42:42 +01:00
|
|
|
print("Checking {s}...\n", .{self.exercise.main_file});
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-05-01 19:27:50 +02:00
|
|
|
const b = self.step.owner;
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-05-01 19:27:50 +02:00
|
|
|
// 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,
|
|
|
|
});
|
2021-02-12 05:04:36 +01:00
|
|
|
return err;
|
|
|
|
};
|
|
|
|
|
2023-05-03 17:44:47 +02:00
|
|
|
const raw_output = if (self.exercise.check_stdout)
|
2023-05-01 19:27:50 +02:00
|
|
|
result.stdout
|
2021-02-12 05:04:36 +01:00
|
|
|
else
|
2023-05-01 19:27:50 +02:00
|
|
|
result.stderr;
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-04-24 11:20:07 +02:00
|
|
|
// Make sure it exited cleanly.
|
2023-05-01 19:27:50 +02:00
|
|
|
switch (result.term) {
|
2021-02-12 05:04:36 +01:00
|
|
|
.Exited => |code| {
|
|
|
|
if (code != 0) {
|
2023-05-03 17:44:47 +02:00
|
|
|
print("{s}{s} exited with error code {d} (expected {d}){s}\n", .{
|
|
|
|
red_text, self.exercise.main_file, code, 0, reset_text,
|
|
|
|
});
|
2021-02-12 05:04:36 +01:00
|
|
|
return error.BadExitCode;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
2023-05-03 17:44:47 +02:00
|
|
|
print("{s}{s} terminated unexpectedly{s}\n", .{
|
|
|
|
red_text, self.exercise.main_file, reset_text,
|
|
|
|
});
|
2021-02-12 05:04:36 +01:00
|
|
|
return error.UnexpectedTermination;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-04-24 11:20:07 +02:00
|
|
|
// Validate the output.
|
2023-05-06 18:02:18 +02:00
|
|
|
// NOTE: exercise.output can never contain a CR character.
|
|
|
|
// See https://ziglang.org/documentation/master/#Source-Encoding.
|
|
|
|
const output = try trimLines(b.allocator, raw_output);
|
|
|
|
const exercise_output = self.exercise.output;
|
|
|
|
if (!std.mem.eql(u8, output, self.exercise.output)) {
|
2023-05-03 17:44:47 +02:00
|
|
|
const red = red_text;
|
|
|
|
const reset = reset_text;
|
|
|
|
|
2021-02-12 05:04:36 +01:00
|
|
|
print(
|
|
|
|
\\
|
2023-05-03 14:39:42 +02:00
|
|
|
\\{s}========= expected this output: =========={s}
|
|
|
|
\\{s}
|
|
|
|
\\{s}========= but found: ====================={s}
|
|
|
|
\\{s}
|
|
|
|
\\{s}=========================================={s}
|
2021-02-12 05:04:36 +01:00
|
|
|
\\
|
2023-05-03 17:44:47 +02:00
|
|
|
, .{ red, reset, exercise_output, red, reset, output, red, reset });
|
2021-02-12 05:04:36 +01:00
|
|
|
return error.InvalidOutput;
|
|
|
|
}
|
|
|
|
|
2023-05-03 17:44:47 +02:00
|
|
|
print("{s}PASSED:\n{s}{s}\n\n", .{ green_text, output, reset_text });
|
2021-02-12 05:04:36 +01:00
|
|
|
}
|
|
|
|
|
2023-05-03 12:34:33 +02:00
|
|
|
fn compile(self: *ZiglingStep, prog_node: *std.Progress.Node) ![]const u8 {
|
2023-05-01 17:12:05 +02:00
|
|
|
print("Compiling {s}...\n", .{self.exercise.main_file});
|
|
|
|
|
2023-05-03 09:56:29 +02:00
|
|
|
const b = self.step.owner;
|
2023-05-03 17:44:47 +02:00
|
|
|
const exercise_path = self.exercise.main_file;
|
|
|
|
const path = join(b.allocator, &.{ self.work_path, exercise_path }) catch
|
|
|
|
@panic("OOM");
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-05-03 09:56:29 +02:00
|
|
|
var zig_args = std.ArrayList([]const u8).init(b.allocator);
|
2021-02-12 05:04:36 +01:00
|
|
|
defer zig_args.deinit();
|
|
|
|
|
2023-05-03 15:25:55 +02:00
|
|
|
zig_args.append(b.zig_exe) catch @panic("OOM");
|
|
|
|
zig_args.append("build-exe") catch @panic("OOM");
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-05-03 17:44:47 +02:00
|
|
|
// Enable C support for exercises that use C functions.
|
2023-04-30 16:25:15 +02:00
|
|
|
if (self.exercise.link_libc) {
|
2023-05-03 15:25:55 +02:00
|
|
|
zig_args.append("-lc") catch @panic("OOM");
|
2023-02-15 22:55:44 +01:00
|
|
|
}
|
|
|
|
|
2023-05-03 17:44:47 +02:00
|
|
|
zig_args.append(b.pathFromRoot(path)) catch @panic("OOM");
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-05-03 15:25:55 +02:00
|
|
|
zig_args.append("--cache-dir") catch @panic("OOM");
|
|
|
|
zig_args.append(b.pathFromRoot(b.cache_root.path.?)) catch @panic("OOM");
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-05-03 15:25:55 +02:00
|
|
|
zig_args.append("--listen=-") catch @panic("OOM");
|
2021-02-12 05:04:36 +01:00
|
|
|
|
|
|
|
const argv = zig_args.items;
|
|
|
|
var code: u8 = undefined;
|
2023-05-01 17:12:05 +02:00
|
|
|
const exe_path = self.eval(argv, &code, prog_node) catch |err| {
|
2023-04-17 15:52:57 +02:00
|
|
|
self.printErrors();
|
|
|
|
|
2021-02-12 05:04:36 +01:00
|
|
|
switch (err) {
|
|
|
|
error.FileNotFound => {
|
2023-05-03 17:44:47 +02:00
|
|
|
print("{s}{s}: Unable to spawn the following command: file not found{s}\n", .{
|
|
|
|
red_text, self.exercise.main_file, reset_text,
|
|
|
|
});
|
2021-02-12 05:04:36 +01:00
|
|
|
for (argv) |v| print("{s} ", .{v});
|
|
|
|
print("\n", .{});
|
|
|
|
},
|
|
|
|
error.ExitCodeFailure => {
|
2023-05-03 17:44:47 +02:00
|
|
|
print("{s}{s}: The following command exited with error code {}:{s}\n", .{
|
|
|
|
red_text, self.exercise.main_file, code, reset_text,
|
|
|
|
});
|
2021-02-12 05:04:36 +01:00
|
|
|
for (argv) |v| print("{s} ", .{v});
|
|
|
|
print("\n", .{});
|
|
|
|
},
|
|
|
|
error.ProcessTerminated => {
|
2023-05-03 17:44:47 +02:00
|
|
|
print("{s}{s}: The following command terminated unexpectedly:{s}\n", .{
|
|
|
|
red_text, self.exercise.main_file, reset_text,
|
|
|
|
});
|
2021-02-12 05:04:36 +01:00
|
|
|
for (argv) |v| print("{s} ", .{v});
|
|
|
|
print("\n", .{});
|
|
|
|
},
|
2023-04-17 15:52:57 +02:00
|
|
|
error.ZigIPCError => {
|
2023-05-08 15:20:27 +02:00
|
|
|
// Commenting this out for now. It always shows up when compilation fails.
|
|
|
|
//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", .{});
|
2023-04-17 15:52:57 +02:00
|
|
|
},
|
2023-05-01 17:32:07 +02:00
|
|
|
else => {
|
|
|
|
print("{s}{s}: Unexpected error: {s}{s}\n", .{
|
2023-05-03 17:44:47 +02:00
|
|
|
red_text, self.exercise.main_file, @errorName(err), reset_text,
|
2023-05-01 17:32:07 +02:00
|
|
|
});
|
|
|
|
for (argv) |v| print("{s} ", .{v});
|
|
|
|
print("\n", .{});
|
|
|
|
},
|
2021-02-12 05:04:36 +01:00
|
|
|
}
|
2023-04-13 18:03:00 +02:00
|
|
|
|
2021-02-12 05:04:36 +01:00
|
|
|
return err;
|
|
|
|
};
|
2023-04-17 15:52:57 +02:00
|
|
|
self.printErrors();
|
2021-02-12 05:04:36 +01:00
|
|
|
|
2023-05-01 17:12:05 +02:00
|
|
|
return exe_path;
|
2023-04-13 18:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.?,
|
2021-02-12 05:04:36 +01:00
|
|
|
});
|
2023-04-13 18:03:00 +02:00
|
|
|
defer poller.deinit();
|
|
|
|
|
2023-04-18 12:22:30 +02:00
|
|
|
try ipc.sendMessage(child.stdin.?, .update);
|
|
|
|
try ipc.sendMessage(child.stdin.?, .exit);
|
2023-04-13 18:03:00 +02:00
|
|
|
|
|
|
|
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 => {
|
2023-04-17 15:52:57 +02:00
|
|
|
self.result_error_bundle = try ipc.parseErrorBundle(allocator, body);
|
2023-04-13 18:03:00 +02:00
|
|
|
},
|
|
|
|
.progress => {
|
|
|
|
node_name.clearRetainingCapacity();
|
|
|
|
try node_name.appendSlice(allocator, body);
|
|
|
|
sub_prog_node.setName(node_name.items);
|
|
|
|
},
|
|
|
|
.emit_bin_path => {
|
2023-04-18 12:22:30 +02:00
|
|
|
const emit_bin = try ipc.parseEmitBinPath(allocator, body);
|
|
|
|
result = emit_bin.path;
|
2023-04-13 18:03:00 +02:00
|
|
|
},
|
|
|
|
else => {}, // ignore other messages
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout.discard(body.len);
|
|
|
|
}
|
|
|
|
|
|
|
|
const stderr = poller.fifo(.stderr);
|
|
|
|
if (stderr.readableLength() > 0) {
|
2023-04-17 15:52:57 +02:00
|
|
|
self.result_messages = try stderr.toOwnedSlice();
|
2023-04-13 18:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2021-02-12 05:04:36 +01:00
|
|
|
}
|
2023-04-17 15:52:57 +02:00
|
|
|
|
2023-05-01 17:12:05 +02:00
|
|
|
fn help(self: *ZiglingStep) void {
|
|
|
|
const path = self.exercise.main_file;
|
|
|
|
|
2023-05-06 20:14:30 +02:00
|
|
|
print("\n{s}Edit exercises/{s} and run 'zig build' again.{s}\n", .{
|
2023-05-01 17:12:05 +02:00
|
|
|
red_text, path, reset_text,
|
|
|
|
});
|
|
|
|
|
2023-05-08 15:20:27 +02:00
|
|
|
// NOTE: The README explains this "advanced feature" if anyone wishes to use
|
|
|
|
// it. Otherwise, beginners are thinking they *have* to do this.
|
|
|
|
//const key = self.exercise.key();
|
|
|
|
//const format =
|
|
|
|
// \\
|
|
|
|
// \\{s}To compile only this exercise, you can also use this command:{s}
|
|
|
|
// \\{s}zig build -Dn={s}{s}
|
|
|
|
// \\
|
|
|
|
// \\
|
|
|
|
//;
|
|
|
|
//print(format, .{ red_text, reset_text, bold_text, key, reset_text });
|
2023-05-01 17:12:05 +02:00
|
|
|
}
|
|
|
|
|
2023-04-17 15:52:57 +02:00
|
|
|
fn printErrors(self: *ZiglingStep) void {
|
2023-04-17 19:09:30 +02:00
|
|
|
resetLine();
|
|
|
|
|
2023-04-17 15:52:57 +02:00
|
|
|
// 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 });
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 05:04:36 +01:00
|
|
|
};
|
2023-04-02 14:59:22 +02:00
|
|
|
|
2023-04-17 19:09:30 +02:00
|
|
|
// 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"});
|
|
|
|
}
|
|
|
|
|
2023-05-06 18:02:18 +02:00
|
|
|
/// Remove trailing whitespace for each line in buf, also ensuring that there
|
|
|
|
/// are no trailing LF characters at the end.
|
|
|
|
pub fn trimLines(allocator: std.mem.Allocator, buf: []const u8) ![]const u8 {
|
|
|
|
var list = try std.ArrayList(u8).initCapacity(allocator, buf.len);
|
|
|
|
|
|
|
|
var iter = std.mem.split(u8, buf, " \n");
|
|
|
|
while (iter.next()) |line| {
|
|
|
|
// TODO: trimming CR characters is probably not necessary.
|
|
|
|
const data = std.mem.trimRight(u8, line, " \r");
|
|
|
|
try list.appendSlice(data);
|
|
|
|
try list.append('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = try list.toOwnedSlice(); // TODO: probably not necessary
|
|
|
|
|
|
|
|
// Remove the trailing LF character, that is always present in the exercise
|
|
|
|
// output.
|
|
|
|
return std.mem.trimRight(u8, result, "\n");
|
|
|
|
}
|
|
|
|
|
2023-04-10 21:24:49 +02:00
|
|
|
// Print a message to stderr.
|
2023-04-02 14:59:22 +02:00
|
|
|
const PrintStep = struct {
|
|
|
|
step: Step,
|
|
|
|
message: []const u8,
|
|
|
|
|
2023-04-10 21:24:49 +02:00
|
|
|
pub fn create(owner: *Build, message: []const u8) *PrintStep {
|
2023-04-02 14:59:22 +02:00
|
|
|
const self = owner.allocator.create(PrintStep) catch @panic("OOM");
|
|
|
|
self.* = .{
|
|
|
|
.step = Step.init(.{
|
|
|
|
.id = .custom,
|
2023-04-09 18:48:19 +02:00
|
|
|
.name = "print",
|
2023-04-02 14:59:22 +02:00
|
|
|
.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);
|
|
|
|
|
2023-04-10 21:24:49 +02:00
|
|
|
print("{s}", .{p.message});
|
2023-04-02 14:59:22 +02:00
|
|
|
}
|
|
|
|
};
|
2023-04-08 12:56:51 +02:00
|
|
|
|
|
|
|
// 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,
|
2023-04-09 18:48:19 +02:00
|
|
|
.name = owner.fmt("skip {s}", .{exercise.main_file}),
|
2023-04-08 12:56:51 +02:00
|
|
|
.owner = owner,
|
|
|
|
.makeFn = make,
|
|
|
|
}),
|
|
|
|
.exercise = exercise,
|
|
|
|
};
|
|
|
|
|
2023-04-30 15:27:51 +02:00
|
|
|
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)`.
|
|
|
|
//
|
2023-05-06 18:02:18 +02:00
|
|
|
// Additionally check that the output field lines doesn't have trailing whitespace.
|
2023-04-30 15:27:51 +02:00
|
|
|
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;
|
2023-04-30 18:48:52 +02:00
|
|
|
for (exercises[0..]) |ex| {
|
|
|
|
const exno = ex.number();
|
|
|
|
const last = 999;
|
2023-04-30 15:27:51 +02:00
|
|
|
i += 1;
|
2023-04-30 18:48:52 +02:00
|
|
|
|
|
|
|
if (exno != i and exno != last) {
|
2023-04-30 15:27:51 +02:00
|
|
|
print("exercise {s} has an incorrect number: expected {}, got {s}\n", .{
|
|
|
|
ex.main_file,
|
|
|
|
i,
|
|
|
|
ex.key(),
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-06 18:02:18 +02:00
|
|
|
var iter = std.mem.split(u8, ex.output, "\n");
|
|
|
|
while (iter.next()) |line| {
|
|
|
|
const output = std.mem.trimRight(u8, line, " \r");
|
|
|
|
if (output.len != line.len) {
|
|
|
|
print("exercise {s} output field lines have trailing whitespace\n", .{
|
|
|
|
ex.main_file,
|
|
|
|
});
|
2023-04-30 15:27:51 +02:00
|
|
|
|
2023-05-06 18:02:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-04-30 15:27:51 +02:00
|
|
|
}
|
2023-04-30 18:48:52 +02:00
|
|
|
|
|
|
|
if (!std.mem.endsWith(u8, ex.main_file, ".zig")) {
|
|
|
|
print("exercise {s} is not a zig source file\n", .{ex.main_file});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-08 12:56:51 +02:00
|
|
|
}
|
|
|
|
|
2023-04-30 15:27:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const exercises = [_]Exercise{
|
|
|
|
.{
|
|
|
|
.main_file = "001_hello.zig",
|
|
|
|
.output = "Hello world!",
|
2023-05-06 18:02:18 +02:00
|
|
|
.hint =
|
|
|
|
\\DON'T PANIC!
|
2023-05-08 15:20:27 +02:00
|
|
|
\\Read the compiler messages above. (Something about 'main'?)
|
|
|
|
\\Open up the source file as noted below and read the comments.
|
|
|
|
\\
|
|
|
|
\\(Hints like these will occasionally show up, but for the
|
|
|
|
\\most part, you'll be taking directions from the Zig
|
|
|
|
\\compiler itself.)
|
2023-05-06 20:22:20 +02:00
|
|
|
\\
|
2023-05-06 18:02:18 +02:00
|
|
|
,
|
2023-04-30 15:27:51 +02:00
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "002_std.zig",
|
|
|
|
.output = "Standard Library.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "003_assignment.zig",
|
|
|
|
.output = "55 314159 -11",
|
|
|
|
.hint = "There are three mistakes in this one!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "004_arrays.zig",
|
|
|
|
.output = "First: 2, Fourth: 7, Length: 8",
|
|
|
|
.hint = "There are two things to complete here.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "005_arrays2.zig",
|
|
|
|
.output = "LEET: 1337, Bits: 100110011001",
|
|
|
|
.hint = "Fill in the two arrays.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "006_strings.zig",
|
|
|
|
.output = "d=d ha ha ha Major Tom",
|
|
|
|
.hint = "Each '???' needs something filled in.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "007_strings2.zig",
|
2023-05-06 18:02:18 +02:00
|
|
|
.output =
|
|
|
|
\\Ziggy played guitar
|
|
|
|
\\Jamming good with Andrew Kelley
|
|
|
|
\\And the Spiders from Mars
|
|
|
|
,
|
2023-04-30 15:27:51 +02:00
|
|
|
.hint = "Please fix the lyrics!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "008_quiz.zig",
|
|
|
|
.output = "Program in Zig!",
|
|
|
|
.hint = "See if you can fix the program!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "009_if.zig",
|
|
|
|
.output = "Foo is 1!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "010_if2.zig",
|
|
|
|
.output = "With the discount, the price is $17.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "011_while.zig",
|
|
|
|
.output = "2 4 8 16 32 64 128 256 512 n=1024",
|
|
|
|
.hint = "You probably want a 'less than' condition.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "012_while2.zig",
|
|
|
|
.output = "2 4 8 16 32 64 128 256 512 n=1024",
|
|
|
|
.hint = "It might help to look back at the previous exercise.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "013_while3.zig",
|
|
|
|
.output = "1 2 4 7 8 11 13 14 16 17 19",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "014_while4.zig",
|
|
|
|
.output = "n=4",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "015_for.zig",
|
|
|
|
.output = "A Dramatic Story: :-) :-) :-( :-| :-) The End.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "016_for2.zig",
|
|
|
|
.output = "The value of bits '1101': 13.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "017_quiz2.zig",
|
|
|
|
.output = "1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16,",
|
|
|
|
.hint = "This is a famous game!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "018_functions.zig",
|
|
|
|
.output = "Answer to the Ultimate Question: 42",
|
|
|
|
.hint = "Can you help write the function?",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "019_functions2.zig",
|
|
|
|
.output = "Powers of two: 2 4 8 16",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "020_quiz3.zig",
|
|
|
|
.output = "32 64 128 256",
|
|
|
|
.hint = "Unexpected pop quiz! Help!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "021_errors.zig",
|
|
|
|
.output = "2<4. 3<4. 4=4. 5>4. 6>4.",
|
|
|
|
.hint = "What's the deal with fours?",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "022_errors2.zig",
|
|
|
|
.output = "I compiled!",
|
|
|
|
.hint = "Get the error union type right to allow this to compile.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "023_errors3.zig",
|
|
|
|
.output = "a=64, b=22",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "024_errors4.zig",
|
|
|
|
.output = "a=20, b=14, c=10",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "025_errors5.zig",
|
|
|
|
.output = "a=0, b=19, c=0",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "026_hello2.zig",
|
|
|
|
.output = "Hello world!",
|
|
|
|
.hint = "Try using a try!",
|
|
|
|
.check_stdout = true,
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "027_defer.zig",
|
|
|
|
.output = "One Two",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "028_defer2.zig",
|
|
|
|
.output = "(Goat) (Cat) (Dog) (Dog) (Goat) (Unknown) done.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "029_errdefer.zig",
|
|
|
|
.output = "Getting number...got 5. Getting number...failed!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "030_switch.zig",
|
|
|
|
.output = "ZIG?",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "031_switch2.zig",
|
|
|
|
.output = "ZIG!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "032_unreachable.zig",
|
|
|
|
.output = "1 2 3 9 8 7",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "033_iferror.zig",
|
|
|
|
.output = "2<4. 3<4. 4=4. 5>4. 6>4.",
|
|
|
|
.hint = "Seriously, what's the deal with fours?",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "034_quiz4.zig",
|
|
|
|
.output = "my_num=42",
|
|
|
|
.hint = "Can you make this work?",
|
|
|
|
.check_stdout = true,
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "035_enums.zig",
|
|
|
|
.output = "1 2 3 9 8 7",
|
|
|
|
.hint = "This problem seems familiar...",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "036_enums2.zig",
|
2023-05-06 18:02:18 +02:00
|
|
|
.output =
|
|
|
|
\\<p>
|
|
|
|
\\ <span style="color: #ff0000">Red</span>
|
|
|
|
\\ <span style="color: #00ff00">Green</span>
|
|
|
|
\\ <span style="color: #0000ff">Blue</span>
|
|
|
|
\\</p>
|
|
|
|
,
|
2023-04-30 15:27:51 +02:00
|
|
|
.hint = "I'm feeling blue about this.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "037_structs.zig",
|
|
|
|
.output = "Your wizard has 90 health and 25 gold.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "038_structs2.zig",
|
2023-05-06 18:02:18 +02:00
|
|
|
.output =
|
|
|
|
\\Character 1 - G:20 H:100 XP:10
|
|
|
|
\\Character 2 - G:10 H:100 XP:20
|
|
|
|
,
|
2023-04-30 15:27:51 +02:00
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "039_pointers.zig",
|
|
|
|
.output = "num1: 5, num2: 5",
|
|
|
|
.hint = "Pointers aren't so bad.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "040_pointers2.zig",
|
|
|
|
.output = "a: 12, b: 12",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "041_pointers3.zig",
|
|
|
|
.output = "foo=6, bar=11",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "042_pointers4.zig",
|
|
|
|
.output = "num: 5, more_nums: 1 1 5 1",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "043_pointers5.zig",
|
2023-05-06 18:02:18 +02:00
|
|
|
.output =
|
|
|
|
\\Wizard (G:10 H:100 XP:20)
|
|
|
|
\\ Mentor: Wizard (G:10000 H:100 XP:2340)
|
|
|
|
,
|
2023-04-30 15:27:51 +02:00
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "044_quiz5.zig",
|
|
|
|
.output = "Elephant A. Elephant B. Elephant C.",
|
|
|
|
.hint = "Oh no! We forgot Elephant B!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "045_optionals.zig",
|
|
|
|
.output = "The Ultimate Answer: 42.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "046_optionals2.zig",
|
|
|
|
.output = "Elephant A. Elephant B. Elephant C.",
|
|
|
|
.hint = "Elephants again!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "047_methods.zig",
|
|
|
|
.output = "5 aliens. 4 aliens. 1 aliens. 0 aliens. Earth is saved!",
|
|
|
|
.hint = "Use the heat ray. And the method!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "048_methods2.zig",
|
|
|
|
.output = "A B C",
|
|
|
|
.hint = "This just needs one little fix.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "049_quiz6.zig",
|
|
|
|
.output = "A B C Cv Bv Av",
|
|
|
|
.hint = "Now you're writing Zig!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "050_no_value.zig",
|
|
|
|
.output = "That is not dead which can eternal lie / And with strange aeons even death may die.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "051_values.zig",
|
|
|
|
.output = "1:false!. 2:true!. 3:true!. XP before:0, after:200.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "052_slices.zig",
|
2023-05-06 18:02:18 +02:00
|
|
|
.output =
|
|
|
|
\\Hand1: A 4 K 8
|
|
|
|
\\Hand2: 5 2 Q J
|
|
|
|
,
|
2023-04-30 15:27:51 +02:00
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "053_slices2.zig",
|
|
|
|
.output = "'all your base are belong to us.' 'for great justice.'",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "054_manypointers.zig",
|
|
|
|
.output = "Memory is a resource.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "055_unions.zig",
|
|
|
|
.output = "Insect report! Ant alive is: true. Bee visited 15 flowers.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "056_unions2.zig",
|
|
|
|
.output = "Insect report! Ant alive is: true. Bee visited 16 flowers.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "057_unions3.zig",
|
|
|
|
.output = "Insect report! Ant alive is: true. Bee visited 17 flowers.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "058_quiz7.zig",
|
|
|
|
.output = "Archer's Point--2->Bridge--1->Dogwood Grove--3->Cottage--2->East Pond--1->Fox Pond",
|
|
|
|
.hint = "This is the biggest program we've seen yet. But you can do it!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "059_integers.zig",
|
|
|
|
.output = "Zig is cool.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "060_floats.zig",
|
|
|
|
.output = "Shuttle liftoff weight: 1995796kg",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "061_coercions.zig",
|
|
|
|
.output = "Letter: A",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "062_loop_expressions.zig",
|
|
|
|
.output = "Current language: Zig",
|
|
|
|
.hint = "Surely the current language is 'Zig'!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "063_labels.zig",
|
|
|
|
.output = "Enjoy your Cheesy Chili!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "064_builtins.zig",
|
|
|
|
.output = "1101 + 0101 = 0010 (true). Without overflow: 00010010. Furthermore, 11110000 backwards is 00001111.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "065_builtins2.zig",
|
|
|
|
.output = "A Narcissus loves all Narcissuses. He has room in his heart for: me myself.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "066_comptime.zig",
|
|
|
|
.output = "Immutable: 12345, 987.654; Mutable: 54321, 456.789; Types: comptime_int, comptime_float, u32, f32",
|
|
|
|
.hint = "It may help to read this one out loud to your favorite stuffed animal until it sinks in completely.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "067_comptime2.zig",
|
|
|
|
.output = "A BB CCC DDDD",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "068_comptime3.zig",
|
2023-05-06 18:02:18 +02:00
|
|
|
.output =
|
|
|
|
\\Minnow (1:32, 4 x 2)
|
|
|
|
\\Shark (1:16, 8 x 5)
|
|
|
|
\\Whale (1:1, 143 x 95)
|
|
|
|
,
|
2023-04-30 15:27:51 +02:00
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "069_comptime4.zig",
|
|
|
|
.output = "s1={ 1, 2, 3 }, s2={ 1, 2, 3, 4, 5 }, s3={ 1, 2, 3, 4, 5, 6, 7 }",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "070_comptime5.zig",
|
2023-05-06 18:02:18 +02:00
|
|
|
.output =
|
|
|
|
\\"Quack." ducky1: true, "Squeek!" ducky2: true, ducky3: false
|
|
|
|
,
|
2023-04-30 15:27:51 +02:00
|
|
|
.hint = "Have you kept the wizard hat on?",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "071_comptime6.zig",
|
|
|
|
.output = "Narcissus has room in his heart for: me myself.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "072_comptime7.zig",
|
|
|
|
.output = "26",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "073_comptime8.zig",
|
|
|
|
.output = "My llama value is 25.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "074_comptime9.zig",
|
|
|
|
.output = "My llama value is 2.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "075_quiz8.zig",
|
|
|
|
.output = "Archer's Point--2->Bridge--1->Dogwood Grove--3->Cottage--2->East Pond--1->Fox Pond",
|
|
|
|
.hint = "Roll up those sleeves. You get to WRITE some code for this one.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "076_sentinels.zig",
|
|
|
|
.output = "Array:123056. Many-item pointer:123.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "077_sentinels2.zig",
|
|
|
|
.output = "Weird Data!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "078_sentinels3.zig",
|
|
|
|
.output = "Weird Data!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "079_quoted_identifiers.zig",
|
|
|
|
.output = "Sweet freedom: 55, false.",
|
|
|
|
.hint = "Help us, Zig Programmer, you're our only hope!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "080_anonymous_structs.zig",
|
|
|
|
.output = "[Circle(i32): 25,70,15] [Circle(f32): 25.2,71.0,15.7]",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "081_anonymous_structs2.zig",
|
|
|
|
.output = "x:205 y:187 radius:12",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "082_anonymous_structs3.zig",
|
2023-05-06 18:02:18 +02:00
|
|
|
.output =
|
|
|
|
\\"0"(bool):true "1"(bool):false "2"(i32):42 "3"(f32):3.14159202e+00
|
|
|
|
,
|
2023-04-30 15:27:51 +02:00
|
|
|
.hint = "This one is a challenge! But you have everything you need.",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "083_anonymous_lists.zig",
|
|
|
|
.output = "I say hello!",
|
|
|
|
},
|
|
|
|
|
|
|
|
// Skipped because of https://github.com/ratfactor/ziglings/issues/163
|
|
|
|
// direct link: https://github.com/ziglang/zig/issues/6025
|
|
|
|
.{
|
|
|
|
.main_file = "084_async.zig",
|
|
|
|
.output = "foo() A",
|
|
|
|
.hint = "Read the facts. Use the facts.",
|
|
|
|
.skip = true,
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "085_async2.zig",
|
|
|
|
.output = "Hello async!",
|
|
|
|
.skip = true,
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "086_async3.zig",
|
|
|
|
.output = "5 4 3 2 1",
|
|
|
|
.skip = true,
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "087_async4.zig",
|
|
|
|
.output = "1 2 3 4 5",
|
|
|
|
.skip = true,
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "088_async5.zig",
|
|
|
|
.output = "Example Title.",
|
|
|
|
.skip = true,
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "089_async6.zig",
|
|
|
|
.output = ".com: Example Title, .org: Example Title.",
|
|
|
|
.skip = true,
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "090_async7.zig",
|
|
|
|
.output = "beef? BEEF!",
|
|
|
|
.skip = true,
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "091_async8.zig",
|
|
|
|
.output = "ABCDEF",
|
|
|
|
.skip = true,
|
|
|
|
},
|
2023-04-08 12:56:51 +02:00
|
|
|
|
2023-04-30 15:27:51 +02:00
|
|
|
.{
|
|
|
|
.main_file = "092_interfaces.zig",
|
2023-05-06 18:02:18 +02:00
|
|
|
.output =
|
|
|
|
\\Daily Insect Report:
|
|
|
|
\\Ant is alive.
|
|
|
|
\\Bee visited 17 flowers.
|
|
|
|
\\Grasshopper hopped 32 meters.
|
|
|
|
,
|
2023-04-30 15:27:51 +02:00
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "093_hello_c.zig",
|
|
|
|
.output = "Hello C from Zig! - C result is 17 chars written.",
|
2023-04-30 16:25:15 +02:00
|
|
|
.link_libc = true,
|
2023-04-30 15:27:51 +02:00
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "094_c_math.zig",
|
|
|
|
.output = "The normalized angle of 765.2 degrees is 45.2 degrees.",
|
2023-04-30 16:25:15 +02:00
|
|
|
.link_libc = true,
|
2023-04-30 15:27:51 +02:00
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "095_for3.zig",
|
|
|
|
.output = "1 2 4 7 8 11 13 14 16 17 19",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "096_memory_allocation.zig",
|
|
|
|
.output = "Running Average: 0.30 0.25 0.20 0.18 0.22",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "097_bit_manipulation.zig",
|
|
|
|
.output = "x = 0; y = 1",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "098_bit_manipulation2.zig",
|
|
|
|
.output = "Is this a pangram? true!",
|
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "099_formatting.zig",
|
2023-05-06 18:02:18 +02:00
|
|
|
.output =
|
|
|
|
\\
|
|
|
|
\\ X | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
|
|
|
\\---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|
|
|
|
\\ 1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
|
|
|
\\
|
|
|
|
\\ 2 | 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
|
|
|
|
\\
|
|
|
|
\\ 3 | 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45
|
|
|
|
\\
|
|
|
|
\\ 4 | 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
|
|
|
|
\\
|
|
|
|
\\ 5 | 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
|
|
|
|
\\
|
|
|
|
\\ 6 | 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90
|
|
|
|
\\
|
|
|
|
\\ 7 | 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105
|
|
|
|
\\
|
|
|
|
\\ 8 | 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120
|
|
|
|
\\
|
|
|
|
\\ 9 | 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135
|
|
|
|
\\
|
|
|
|
\\10 | 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
|
|
|
|
\\
|
|
|
|
\\11 | 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165
|
|
|
|
\\
|
|
|
|
\\12 | 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180
|
|
|
|
\\
|
|
|
|
\\13 | 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195
|
|
|
|
\\
|
|
|
|
\\14 | 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210
|
|
|
|
\\
|
|
|
|
\\15 | 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225
|
|
|
|
,
|
2023-04-30 15:27:51 +02:00
|
|
|
},
|
|
|
|
.{
|
|
|
|
.main_file = "100_for4.zig",
|
|
|
|
.output = "Arrays match!",
|
|
|
|
},
|
2023-05-02 14:26:32 +02:00
|
|
|
.{
|
|
|
|
.main_file = "101_for5.zig",
|
2023-05-06 18:02:18 +02:00
|
|
|
.output =
|
|
|
|
\\1. Wizard (Gold: 25, XP: 40)
|
|
|
|
\\2. Bard (Gold: 11, XP: 17)
|
|
|
|
\\3. Bard (Gold: 5, XP: 55)
|
|
|
|
\\4. Warrior (Gold: 7392, XP: 21)
|
|
|
|
,
|
2023-05-02 14:26:32 +02:00
|
|
|
},
|
2023-04-30 15:27:51 +02:00
|
|
|
.{
|
|
|
|
.main_file = "999_the_end.zig",
|
2023-05-06 18:02:18 +02:00
|
|
|
.output =
|
|
|
|
\\
|
|
|
|
\\This is the end for now!
|
|
|
|
\\We hope you had fun and were able to learn a lot, so visit us again when the next exercises are available.
|
|
|
|
,
|
2023-04-30 15:27:51 +02:00
|
|
|
},
|
2023-04-08 12:56:51 +02:00
|
|
|
};
|