build.zig 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. const std = @import("std");
  2. const builtin = @import("builtin");
  3. const compat = @import("src/compat.zig");
  4. const tests = @import("test/tests.zig");
  5. const Build = compat.Build;
  6. const CompileStep = compat.build.CompileStep;
  7. const Step = compat.build.Step;
  8. const Child = std.process.Child;
  9. const assert = std.debug.assert;
  10. const join = std.fs.path.join;
  11. const print = std.debug.print;
  12. const Kind = enum {
  13. /// Run the artifact as a normal executable.
  14. exe,
  15. /// Run the artifact as a test.
  16. @"test",
  17. };
  18. pub const Exercise = struct {
  19. /// main_file must have the format key_name.zig.
  20. /// The key will be used as a shorthand to build just one example.
  21. main_file: []const u8,
  22. /// This is the desired output of the program.
  23. /// A program passes if its output, excluding trailing whitespace, is equal
  24. /// to this string.
  25. output: []const u8,
  26. /// This is an optional hint to give if the program does not succeed.
  27. hint: ?[]const u8 = null,
  28. /// By default, we verify output against stderr.
  29. /// Set this to true to check stdout instead.
  30. check_stdout: bool = false,
  31. /// This exercise makes use of C functions.
  32. /// We need to keep track of this, so we compile with libc.
  33. link_libc: bool = false,
  34. /// This exercise kind.
  35. kind: Kind = .exe,
  36. /// This exercise is not supported by the current Zig compiler.
  37. skip: bool = false,
  38. /// Returns the name of the main file with .zig stripped.
  39. pub fn name(self: Exercise) []const u8 {
  40. return std.fs.path.stem(self.main_file);
  41. }
  42. /// Returns the key of the main file, the string before the '_' with
  43. /// "zero padding" removed.
  44. /// For example, "001_hello.zig" has the key "1".
  45. pub fn key(self: Exercise) []const u8 {
  46. // Main file must be key_description.zig.
  47. const end_index = std.mem.indexOfScalar(u8, self.main_file, '_') orelse
  48. unreachable;
  49. // Remove zero padding by advancing index past '0's.
  50. var start_index: usize = 0;
  51. while (self.main_file[start_index] == '0') start_index += 1;
  52. return self.main_file[start_index..end_index];
  53. }
  54. /// Returns the exercise key as an integer.
  55. pub fn number(self: Exercise) usize {
  56. return std.fmt.parseInt(usize, self.key(), 10) catch unreachable;
  57. }
  58. };
  59. /// Build mode.
  60. const Mode = enum {
  61. /// Normal build mode: `zig build`
  62. normal,
  63. /// Named build mode: `zig build -Dn=n`
  64. named,
  65. };
  66. pub const logo =
  67. \\ _ _ _
  68. \\ ___(_) __ _| (_)_ __ __ _ ___
  69. \\ |_ | |/ _' | | | '_ \ / _' / __|
  70. \\ / /| | (_| | | | | | | (_| \__ \
  71. \\ /___|_|\__, |_|_|_| |_|\__, |___/
  72. \\ |___/ |___/
  73. \\
  74. \\ "Look out! Broken programs below!"
  75. \\
  76. \\
  77. ;
  78. pub fn build(b: *Build) !void {
  79. if (!compat.is_compatible) compat.die();
  80. if (!validate_exercises()) std.os.exit(2);
  81. use_color_escapes = false;
  82. if (std.io.getStdErr().supportsAnsiEscapeCodes()) {
  83. use_color_escapes = true;
  84. } else if (builtin.os.tag == .windows) {
  85. const w32 = struct {
  86. const WINAPI = std.os.windows.WINAPI;
  87. const DWORD = std.os.windows.DWORD;
  88. const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
  89. const STD_ERROR_HANDLE = @bitCast(DWORD, @as(i32, -12));
  90. extern "kernel32" fn GetStdHandle(id: DWORD) callconv(WINAPI) ?*anyopaque;
  91. extern "kernel32" fn GetConsoleMode(console: ?*anyopaque, out_mode: *DWORD) callconv(WINAPI) u32;
  92. extern "kernel32" fn SetConsoleMode(console: ?*anyopaque, mode: DWORD) callconv(WINAPI) u32;
  93. };
  94. const handle = w32.GetStdHandle(w32.STD_ERROR_HANDLE);
  95. var mode: w32.DWORD = 0;
  96. if (w32.GetConsoleMode(handle, &mode) != 0) {
  97. mode |= w32.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
  98. use_color_escapes = w32.SetConsoleMode(handle, mode) != 0;
  99. }
  100. }
  101. if (use_color_escapes) {
  102. red_text = "\x1b[31m";
  103. red_bold_text = "\x1b[31;1m";
  104. red_dim_text = "\x1b[31;2m";
  105. green_text = "\x1b[32m";
  106. bold_text = "\x1b[1m";
  107. reset_text = "\x1b[0m";
  108. }
  109. // Remove the standard install and uninstall steps.
  110. b.top_level_steps = .{};
  111. const healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse
  112. false;
  113. const override_healed_path = b.option([]const u8, "healed-path", "Override healed path");
  114. const exno: ?usize = b.option(usize, "n", "Select exercise");
  115. const sep = std.fs.path.sep_str;
  116. const healed_path = if (override_healed_path) |path|
  117. path
  118. else
  119. "patches" ++ sep ++ "healed";
  120. const work_path = if (healed) healed_path else "exercises";
  121. const header_step = PrintStep.create(b, logo);
  122. if (exno) |n| {
  123. // Named build mode: verifies a single exercise.
  124. if (n == 0 or n > exercises.len - 1) {
  125. print("unknown exercise number: {}\n", .{n});
  126. std.os.exit(2);
  127. }
  128. const ex = exercises[n - 1];
  129. const zigling_step = b.step(
  130. "zigling",
  131. b.fmt("Check the solution of {s}", .{ex.main_file}),
  132. );
  133. b.default_step = zigling_step;
  134. zigling_step.dependOn(&header_step.step);
  135. const verify_step = ZiglingStep.create(b, ex, work_path, .named);
  136. verify_step.step.dependOn(&header_step.step);
  137. zigling_step.dependOn(&verify_step.step);
  138. return;
  139. }
  140. // Normal build mode: verifies all exercises according to the recommended
  141. // order.
  142. const ziglings_step = b.step("ziglings", "Check all ziglings");
  143. b.default_step = ziglings_step;
  144. var prev_step = &header_step.step;
  145. for (exercises) |ex| {
  146. const verify_stepn = ZiglingStep.create(b, ex, work_path, .normal);
  147. verify_stepn.step.dependOn(prev_step);
  148. prev_step = &verify_stepn.step;
  149. }
  150. ziglings_step.dependOn(prev_step);
  151. const test_step = b.step("test", "Run all the tests");
  152. test_step.dependOn(tests.addCliTests(b, &exercises));
  153. }
  154. var use_color_escapes = false;
  155. var red_text: []const u8 = "";
  156. var red_bold_text: []const u8 = "";
  157. var red_dim_text: []const u8 = "";
  158. var green_text: []const u8 = "";
  159. var bold_text: []const u8 = "";
  160. var reset_text: []const u8 = "";
  161. const ZiglingStep = struct {
  162. step: Step,
  163. exercise: Exercise,
  164. work_path: []const u8,
  165. mode: Mode,
  166. pub fn create(
  167. b: *Build,
  168. exercise: Exercise,
  169. work_path: []const u8,
  170. mode: Mode,
  171. ) *ZiglingStep {
  172. const self = b.allocator.create(ZiglingStep) catch @panic("OOM");
  173. self.* = .{
  174. .step = Step.init(.{
  175. .id = .custom,
  176. .name = exercise.main_file,
  177. .owner = b,
  178. .makeFn = make,
  179. }),
  180. .exercise = exercise,
  181. .work_path = work_path,
  182. .mode = mode,
  183. };
  184. return self;
  185. }
  186. fn make(step: *Step, prog_node: *std.Progress.Node) !void {
  187. // NOTE: Using exit code 2 will prevent the Zig compiler to print the message:
  188. // "error: the following build command failed with exit code 1:..."
  189. const self = @fieldParentPtr(ZiglingStep, "step", step);
  190. if (self.exercise.skip) {
  191. print("Skipping {s}\n\n", .{self.exercise.main_file});
  192. return;
  193. }
  194. const exe_path = self.compile(prog_node) catch {
  195. self.printErrors();
  196. if (self.exercise.hint) |hint|
  197. print("\n{s}Ziglings hint: {s}{s}", .{ bold_text, hint, reset_text });
  198. self.help();
  199. std.os.exit(2);
  200. };
  201. self.run(exe_path, prog_node) catch {
  202. self.printErrors();
  203. if (self.exercise.hint) |hint|
  204. print("\n{s}Ziglings hint: {s}{s}", .{ bold_text, hint, reset_text });
  205. self.help();
  206. std.os.exit(2);
  207. };
  208. // Print possible warning/debug messages.
  209. self.printErrors();
  210. }
  211. fn run(self: *ZiglingStep, exe_path: []const u8, _: *std.Progress.Node) !void {
  212. resetLine();
  213. print("Checking {s}...\n", .{self.exercise.main_file});
  214. const b = self.step.owner;
  215. // Allow up to 1 MB of stdout capture.
  216. const max_output_bytes = 1 * 1024 * 1024;
  217. var result = Child.exec(.{
  218. .allocator = b.allocator,
  219. .argv = &.{exe_path},
  220. .cwd = b.build_root.path.?,
  221. .cwd_dir = b.build_root.handle,
  222. .max_output_bytes = max_output_bytes,
  223. }) catch |err| {
  224. return self.step.fail("unable to spawn {s}: {s}", .{
  225. exe_path, @errorName(err),
  226. });
  227. };
  228. switch (self.exercise.kind) {
  229. .exe => return self.check_output(result),
  230. .@"test" => return self.check_test(result),
  231. }
  232. }
  233. fn check_output(self: *ZiglingStep, result: Child.ExecResult) !void {
  234. const b = self.step.owner;
  235. // Make sure it exited cleanly.
  236. switch (result.term) {
  237. .Exited => |code| {
  238. if (code != 0) {
  239. return self.step.fail("{s} exited with error code {d} (expected {})", .{
  240. self.exercise.main_file, code, 0,
  241. });
  242. }
  243. },
  244. else => {
  245. return self.step.fail("{s} terminated unexpectedly", .{
  246. self.exercise.main_file,
  247. });
  248. },
  249. }
  250. const raw_output = if (self.exercise.check_stdout)
  251. result.stdout
  252. else
  253. result.stderr;
  254. // Validate the output.
  255. // NOTE: exercise.output can never contain a CR character.
  256. // See https://ziglang.org/documentation/master/#Source-Encoding.
  257. const output = trimLines(b.allocator, raw_output) catch @panic("OOM");
  258. const exercise_output = self.exercise.output;
  259. if (!std.mem.eql(u8, output, self.exercise.output)) {
  260. const red = red_bold_text;
  261. const reset = reset_text;
  262. // Override the coloring applied by the printError method.
  263. // NOTE: the first red and the last reset are not necessary, they
  264. // are here only for alignment.
  265. return self.step.fail(
  266. \\
  267. \\{s}========= expected this output: =========={s}
  268. \\{s}
  269. \\{s}========= but found: ====================={s}
  270. \\{s}
  271. \\{s}=========================================={s}
  272. , .{ red, reset, exercise_output, red, reset, output, red, reset });
  273. }
  274. print("{s}PASSED:\n{s}{s}\n\n", .{ green_text, output, reset_text });
  275. }
  276. fn check_test(self: *ZiglingStep, result: Child.ExecResult) !void {
  277. switch (result.term) {
  278. .Exited => |code| {
  279. if (code != 0) {
  280. // The test failed.
  281. const stderr = std.mem.trimRight(u8, result.stderr, " \r\n");
  282. return self.step.fail("\n{s}", .{stderr});
  283. }
  284. },
  285. else => {
  286. return self.step.fail("{s} terminated unexpectedly", .{
  287. self.exercise.main_file,
  288. });
  289. },
  290. }
  291. print("{s}PASSED{s}\n\n", .{ green_text, reset_text });
  292. }
  293. fn compile(self: *ZiglingStep, prog_node: *std.Progress.Node) ![]const u8 {
  294. print("Compiling {s}...\n", .{self.exercise.main_file});
  295. const b = self.step.owner;
  296. const exercise_path = self.exercise.main_file;
  297. const path = join(b.allocator, &.{ self.work_path, exercise_path }) catch
  298. @panic("OOM");
  299. var zig_args = std.ArrayList([]const u8).init(b.allocator);
  300. defer zig_args.deinit();
  301. zig_args.append(b.zig_exe) catch @panic("OOM");
  302. const cmd = switch (self.exercise.kind) {
  303. .exe => "build-exe",
  304. .@"test" => "test",
  305. };
  306. zig_args.append(cmd) catch @panic("OOM");
  307. // Enable C support for exercises that use C functions.
  308. if (self.exercise.link_libc) {
  309. zig_args.append("-lc") catch @panic("OOM");
  310. }
  311. zig_args.append(b.pathFromRoot(path)) catch @panic("OOM");
  312. zig_args.append("--cache-dir") catch @panic("OOM");
  313. zig_args.append(b.pathFromRoot(b.cache_root.path.?)) catch @panic("OOM");
  314. zig_args.append("--listen=-") catch @panic("OOM");
  315. return try self.step.evalZigProcess(zig_args.items, prog_node);
  316. }
  317. fn help(self: *ZiglingStep) void {
  318. const b = self.step.owner;
  319. const key = self.exercise.key();
  320. const path = self.exercise.main_file;
  321. const cmd = switch (self.mode) {
  322. .normal => "zig build",
  323. .named => b.fmt("zig build -Dn={s}", .{key}),
  324. };
  325. print("\n{s}Edit exercises/{s} and run '{s}' again.{s}\n", .{
  326. red_bold_text, path, cmd, reset_text,
  327. });
  328. }
  329. fn printErrors(self: *ZiglingStep) void {
  330. resetLine();
  331. // Display error/warning messages.
  332. if (self.step.result_error_msgs.items.len > 0) {
  333. for (self.step.result_error_msgs.items) |msg| {
  334. print("{s}error: {s}{s}{s}{s}\n", .{
  335. red_bold_text, reset_text, red_dim_text, msg, reset_text,
  336. });
  337. }
  338. }
  339. // Render compile errors at the bottom of the terminal.
  340. // TODO: use the same ttyconf from the builder.
  341. const ttyconf: std.io.tty.Config = if (use_color_escapes)
  342. .escape_codes
  343. else
  344. .no_color;
  345. if (self.step.result_error_bundle.errorMessageCount() > 0) {
  346. self.step.result_error_bundle.renderToStdErr(.{ .ttyconf = ttyconf });
  347. }
  348. }
  349. };
  350. /// Clears the entire line and move the cursor to column zero.
  351. /// Used for clearing the compiler and build_runner progress messages.
  352. fn resetLine() void {
  353. if (use_color_escapes) print("{s}", .{"\x1b[2K\r"});
  354. }
  355. /// Removes trailing whitespace for each line in buf, also ensuring that there
  356. /// are no trailing LF characters at the end.
  357. pub fn trimLines(allocator: std.mem.Allocator, buf: []const u8) ![]const u8 {
  358. var list = try std.ArrayList(u8).initCapacity(allocator, buf.len);
  359. var iter = std.mem.split(u8, buf, " \n");
  360. while (iter.next()) |line| {
  361. // TODO: trimming CR characters is probably not necessary.
  362. const data = std.mem.trimRight(u8, line, " \r");
  363. try list.appendSlice(data);
  364. try list.append('\n');
  365. }
  366. const result = try list.toOwnedSlice(); // TODO: probably not necessary
  367. // Remove the trailing LF character, that is always present in the exercise
  368. // output.
  369. return std.mem.trimRight(u8, result, "\n");
  370. }
  371. /// Prints a message to stderr.
  372. const PrintStep = struct {
  373. step: Step,
  374. message: []const u8,
  375. pub fn create(owner: *Build, message: []const u8) *PrintStep {
  376. const self = owner.allocator.create(PrintStep) catch @panic("OOM");
  377. self.* = .{
  378. .step = Step.init(.{
  379. .id = .custom,
  380. .name = "print",
  381. .owner = owner,
  382. .makeFn = make,
  383. }),
  384. .message = message,
  385. };
  386. return self;
  387. }
  388. fn make(step: *Step, _: *std.Progress.Node) !void {
  389. const self = @fieldParentPtr(PrintStep, "step", step);
  390. print("{s}", .{self.message});
  391. }
  392. };
  393. /// Checks that each exercise number, excluding the last, forms the sequence
  394. /// `[1, exercise.len)`.
  395. ///
  396. /// Additionally check that the output field lines doesn't have trailing whitespace.
  397. fn validate_exercises() bool {
  398. // Don't use the "multi-object for loop" syntax, in order to avoid a syntax
  399. // error with old Zig compilers.
  400. var i: usize = 0;
  401. for (exercises[0..]) |ex| {
  402. const exno = ex.number();
  403. const last = 999;
  404. i += 1;
  405. if (exno != i and exno != last) {
  406. print("exercise {s} has an incorrect number: expected {}, got {s}\n", .{
  407. ex.main_file, i, ex.key(),
  408. });
  409. return false;
  410. }
  411. var iter = std.mem.split(u8, ex.output, "\n");
  412. while (iter.next()) |line| {
  413. const output = std.mem.trimRight(u8, line, " \r");
  414. if (output.len != line.len) {
  415. print("exercise {s} output field lines have trailing whitespace\n", .{
  416. ex.main_file,
  417. });
  418. return false;
  419. }
  420. }
  421. if (!std.mem.endsWith(u8, ex.main_file, ".zig")) {
  422. print("exercise {s} is not a zig source file\n", .{ex.main_file});
  423. return false;
  424. }
  425. }
  426. return true;
  427. }
  428. const exercises = [_]Exercise{
  429. .{
  430. .main_file = "001_hello.zig",
  431. .output = "Hello world!",
  432. .hint =
  433. \\DON'T PANIC!
  434. \\Read the compiler messages above. (Something about 'main'?)
  435. \\Open up the source file as noted below and read the comments.
  436. \\
  437. \\(Hints like these will occasionally show up, but for the
  438. \\most part, you'll be taking directions from the Zig
  439. \\compiler itself.)
  440. \\
  441. ,
  442. },
  443. .{
  444. .main_file = "002_std.zig",
  445. .output = "Standard Library.",
  446. },
  447. .{
  448. .main_file = "003_assignment.zig",
  449. .output = "55 314159 -11",
  450. .hint = "There are three mistakes in this one!",
  451. },
  452. .{
  453. .main_file = "004_arrays.zig",
  454. .output = "First: 2, Fourth: 7, Length: 8",
  455. .hint = "There are two things to complete here.",
  456. },
  457. .{
  458. .main_file = "005_arrays2.zig",
  459. .output = "LEET: 1337, Bits: 100110011001",
  460. .hint = "Fill in the two arrays.",
  461. },
  462. .{
  463. .main_file = "006_strings.zig",
  464. .output = "d=d ha ha ha Major Tom",
  465. .hint = "Each '???' needs something filled in.",
  466. },
  467. .{
  468. .main_file = "007_strings2.zig",
  469. .output =
  470. \\Ziggy played guitar
  471. \\Jamming good with Andrew Kelley
  472. \\And the Spiders from Mars
  473. ,
  474. .hint = "Please fix the lyrics!",
  475. },
  476. .{
  477. .main_file = "008_quiz.zig",
  478. .output = "Program in Zig!",
  479. .hint = "See if you can fix the program!",
  480. },
  481. .{
  482. .main_file = "009_if.zig",
  483. .output = "Foo is 1!",
  484. },
  485. .{
  486. .main_file = "010_if2.zig",
  487. .output = "With the discount, the price is $17.",
  488. },
  489. .{
  490. .main_file = "011_while.zig",
  491. .output = "2 4 8 16 32 64 128 256 512 n=1024",
  492. .hint = "You probably want a 'less than' condition.",
  493. },
  494. .{
  495. .main_file = "012_while2.zig",
  496. .output = "2 4 8 16 32 64 128 256 512 n=1024",
  497. .hint = "It might help to look back at the previous exercise.",
  498. },
  499. .{
  500. .main_file = "013_while3.zig",
  501. .output = "1 2 4 7 8 11 13 14 16 17 19",
  502. },
  503. .{
  504. .main_file = "014_while4.zig",
  505. .output = "n=4",
  506. },
  507. .{
  508. .main_file = "015_for.zig",
  509. .output = "A Dramatic Story: :-) :-) :-( :-| :-) The End.",
  510. },
  511. .{
  512. .main_file = "016_for2.zig",
  513. .output = "The value of bits '1101': 13.",
  514. },
  515. .{
  516. .main_file = "017_quiz2.zig",
  517. .output = "1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16,",
  518. .hint = "This is a famous game!",
  519. },
  520. .{
  521. .main_file = "018_functions.zig",
  522. .output = "Answer to the Ultimate Question: 42",
  523. .hint = "Can you help write the function?",
  524. },
  525. .{
  526. .main_file = "019_functions2.zig",
  527. .output = "Powers of two: 2 4 8 16",
  528. },
  529. .{
  530. .main_file = "020_quiz3.zig",
  531. .output = "32 64 128 256",
  532. .hint = "Unexpected pop quiz! Help!",
  533. },
  534. .{
  535. .main_file = "021_errors.zig",
  536. .output = "2<4. 3<4. 4=4. 5>4. 6>4.",
  537. .hint = "What's the deal with fours?",
  538. },
  539. .{
  540. .main_file = "022_errors2.zig",
  541. .output = "I compiled!",
  542. .hint = "Get the error union type right to allow this to compile.",
  543. },
  544. .{
  545. .main_file = "023_errors3.zig",
  546. .output = "a=64, b=22",
  547. },
  548. .{
  549. .main_file = "024_errors4.zig",
  550. .output = "a=20, b=14, c=10",
  551. },
  552. .{
  553. .main_file = "025_errors5.zig",
  554. .output = "a=0, b=19, c=0",
  555. },
  556. .{
  557. .main_file = "026_hello2.zig",
  558. .output = "Hello world!",
  559. .hint = "Try using a try!",
  560. .check_stdout = true,
  561. },
  562. .{
  563. .main_file = "027_defer.zig",
  564. .output = "One Two",
  565. },
  566. .{
  567. .main_file = "028_defer2.zig",
  568. .output = "(Goat) (Cat) (Dog) (Dog) (Goat) (Unknown) done.",
  569. },
  570. .{
  571. .main_file = "029_errdefer.zig",
  572. .output = "Getting number...got 5. Getting number...failed!",
  573. },
  574. .{
  575. .main_file = "030_switch.zig",
  576. .output = "ZIG?",
  577. },
  578. .{
  579. .main_file = "031_switch2.zig",
  580. .output = "ZIG!",
  581. },
  582. .{
  583. .main_file = "032_unreachable.zig",
  584. .output = "1 2 3 9 8 7",
  585. },
  586. .{
  587. .main_file = "033_iferror.zig",
  588. .output = "2<4. 3<4. 4=4. 5>4. 6>4.",
  589. .hint = "Seriously, what's the deal with fours?",
  590. },
  591. .{
  592. .main_file = "034_quiz4.zig",
  593. .output = "my_num=42",
  594. .hint = "Can you make this work?",
  595. .check_stdout = true,
  596. },
  597. .{
  598. .main_file = "035_enums.zig",
  599. .output = "1 2 3 9 8 7",
  600. .hint = "This problem seems familiar...",
  601. },
  602. .{
  603. .main_file = "036_enums2.zig",
  604. .output =
  605. \\<p>
  606. \\ <span style="color: #ff0000">Red</span>
  607. \\ <span style="color: #00ff00">Green</span>
  608. \\ <span style="color: #0000ff">Blue</span>
  609. \\</p>
  610. ,
  611. .hint = "I'm feeling blue about this.",
  612. },
  613. .{
  614. .main_file = "037_structs.zig",
  615. .output = "Your wizard has 90 health and 25 gold.",
  616. },
  617. .{
  618. .main_file = "038_structs2.zig",
  619. .output =
  620. \\Character 1 - G:20 H:100 XP:10
  621. \\Character 2 - G:10 H:100 XP:20
  622. ,
  623. },
  624. .{
  625. .main_file = "039_pointers.zig",
  626. .output = "num1: 5, num2: 5",
  627. .hint = "Pointers aren't so bad.",
  628. },
  629. .{
  630. .main_file = "040_pointers2.zig",
  631. .output = "a: 12, b: 12",
  632. },
  633. .{
  634. .main_file = "041_pointers3.zig",
  635. .output = "foo=6, bar=11",
  636. },
  637. .{
  638. .main_file = "042_pointers4.zig",
  639. .output = "num: 5, more_nums: 1 1 5 1",
  640. },
  641. .{
  642. .main_file = "043_pointers5.zig",
  643. .output =
  644. \\Wizard (G:10 H:100 XP:20)
  645. \\ Mentor: Wizard (G:10000 H:100 XP:2340)
  646. ,
  647. },
  648. .{
  649. .main_file = "044_quiz5.zig",
  650. .output = "Elephant A. Elephant B. Elephant C.",
  651. .hint = "Oh no! We forgot Elephant B!",
  652. },
  653. .{
  654. .main_file = "045_optionals.zig",
  655. .output = "The Ultimate Answer: 42.",
  656. },
  657. .{
  658. .main_file = "046_optionals2.zig",
  659. .output = "Elephant A. Elephant B. Elephant C.",
  660. .hint = "Elephants again!",
  661. },
  662. .{
  663. .main_file = "047_methods.zig",
  664. .output = "5 aliens. 4 aliens. 1 aliens. 0 aliens. Earth is saved!",
  665. .hint = "Use the heat ray. And the method!",
  666. },
  667. .{
  668. .main_file = "048_methods2.zig",
  669. .output = "A B C",
  670. .hint = "This just needs one little fix.",
  671. },
  672. .{
  673. .main_file = "049_quiz6.zig",
  674. .output = "A B C Cv Bv Av",
  675. .hint = "Now you're writing Zig!",
  676. },
  677. .{
  678. .main_file = "050_no_value.zig",
  679. .output = "That is not dead which can eternal lie / And with strange aeons even death may die.",
  680. },
  681. .{
  682. .main_file = "051_values.zig",
  683. .output = "1:false!. 2:true!. 3:true!. XP before:0, after:200.",
  684. },
  685. .{
  686. .main_file = "052_slices.zig",
  687. .output =
  688. \\Hand1: A 4 K 8
  689. \\Hand2: 5 2 Q J
  690. ,
  691. },
  692. .{
  693. .main_file = "053_slices2.zig",
  694. .output = "'all your base are belong to us.' 'for great justice.'",
  695. },
  696. .{
  697. .main_file = "054_manypointers.zig",
  698. .output = "Memory is a resource.",
  699. },
  700. .{
  701. .main_file = "055_unions.zig",
  702. .output = "Insect report! Ant alive is: true. Bee visited 15 flowers.",
  703. },
  704. .{
  705. .main_file = "056_unions2.zig",
  706. .output = "Insect report! Ant alive is: true. Bee visited 16 flowers.",
  707. },
  708. .{
  709. .main_file = "057_unions3.zig",
  710. .output = "Insect report! Ant alive is: true. Bee visited 17 flowers.",
  711. },
  712. .{
  713. .main_file = "058_quiz7.zig",
  714. .output = "Archer's Point--2->Bridge--1->Dogwood Grove--3->Cottage--2->East Pond--1->Fox Pond",
  715. .hint = "This is the biggest program we've seen yet. But you can do it!",
  716. },
  717. .{
  718. .main_file = "059_integers.zig",
  719. .output = "Zig is cool.",
  720. },
  721. .{
  722. .main_file = "060_floats.zig",
  723. .output = "Shuttle liftoff weight: 1995796kg",
  724. },
  725. .{
  726. .main_file = "061_coercions.zig",
  727. .output = "Letter: A",
  728. },
  729. .{
  730. .main_file = "062_loop_expressions.zig",
  731. .output = "Current language: Zig",
  732. .hint = "Surely the current language is 'Zig'!",
  733. },
  734. .{
  735. .main_file = "063_labels.zig",
  736. .output = "Enjoy your Cheesy Chili!",
  737. },
  738. .{
  739. .main_file = "064_builtins.zig",
  740. .output = "1101 + 0101 = 0010 (true). Without overflow: 00010010. Furthermore, 11110000 backwards is 00001111.",
  741. },
  742. .{
  743. .main_file = "065_builtins2.zig",
  744. .output = "A Narcissus loves all Narcissuses. He has room in his heart for: me myself.",
  745. },
  746. .{
  747. .main_file = "066_comptime.zig",
  748. .output = "Immutable: 12345, 987.654; Mutable: 54321, 456.789; Types: comptime_int, comptime_float, u32, f32",
  749. .hint = "It may help to read this one out loud to your favorite stuffed animal until it sinks in completely.",
  750. },
  751. .{
  752. .main_file = "067_comptime2.zig",
  753. .output = "A BB CCC DDDD",
  754. },
  755. .{
  756. .main_file = "068_comptime3.zig",
  757. .output =
  758. \\Minnow (1:32, 4 x 2)
  759. \\Shark (1:16, 8 x 5)
  760. \\Whale (1:1, 143 x 95)
  761. ,
  762. },
  763. .{
  764. .main_file = "069_comptime4.zig",
  765. .output = "s1={ 1, 2, 3 }, s2={ 1, 2, 3, 4, 5 }, s3={ 1, 2, 3, 4, 5, 6, 7 }",
  766. },
  767. .{
  768. .main_file = "070_comptime5.zig",
  769. .output =
  770. \\"Quack." ducky1: true, "Squeek!" ducky2: true, ducky3: false
  771. ,
  772. .hint = "Have you kept the wizard hat on?",
  773. },
  774. .{
  775. .main_file = "071_comptime6.zig",
  776. .output = "Narcissus has room in his heart for: me myself.",
  777. },
  778. .{
  779. .main_file = "072_comptime7.zig",
  780. .output = "26",
  781. },
  782. .{
  783. .main_file = "073_comptime8.zig",
  784. .output = "My llama value is 25.",
  785. },
  786. .{
  787. .main_file = "074_comptime9.zig",
  788. .output = "My llama value is 2.",
  789. },
  790. .{
  791. .main_file = "075_quiz8.zig",
  792. .output = "Archer's Point--2->Bridge--1->Dogwood Grove--3->Cottage--2->East Pond--1->Fox Pond",
  793. .hint = "Roll up those sleeves. You get to WRITE some code for this one.",
  794. },
  795. .{
  796. .main_file = "076_sentinels.zig",
  797. .output = "Array:123056. Many-item pointer:123.",
  798. },
  799. .{
  800. .main_file = "077_sentinels2.zig",
  801. .output = "Weird Data!",
  802. },
  803. .{
  804. .main_file = "078_sentinels3.zig",
  805. .output = "Weird Data!",
  806. },
  807. .{
  808. .main_file = "079_quoted_identifiers.zig",
  809. .output = "Sweet freedom: 55, false.",
  810. .hint = "Help us, Zig Programmer, you're our only hope!",
  811. },
  812. .{
  813. .main_file = "080_anonymous_structs.zig",
  814. .output = "[Circle(i32): 25,70,15] [Circle(f32): 25.2,71.0,15.7]",
  815. },
  816. .{
  817. .main_file = "081_anonymous_structs2.zig",
  818. .output = "x:205 y:187 radius:12",
  819. },
  820. .{
  821. .main_file = "082_anonymous_structs3.zig",
  822. .output =
  823. \\"0"(bool):true "1"(bool):false "2"(i32):42 "3"(f32):3.14159202e+00
  824. ,
  825. .hint = "This one is a challenge! But you have everything you need.",
  826. },
  827. .{
  828. .main_file = "083_anonymous_lists.zig",
  829. .output = "I say hello!",
  830. },
  831. // Skipped because of https://github.com/ratfactor/ziglings/issues/163
  832. // direct link: https://github.com/ziglang/zig/issues/6025
  833. .{
  834. .main_file = "084_async.zig",
  835. .output = "foo() A",
  836. .hint = "Read the facts. Use the facts.",
  837. .skip = true,
  838. },
  839. .{
  840. .main_file = "085_async2.zig",
  841. .output = "Hello async!",
  842. .skip = true,
  843. },
  844. .{
  845. .main_file = "086_async3.zig",
  846. .output = "5 4 3 2 1",
  847. .skip = true,
  848. },
  849. .{
  850. .main_file = "087_async4.zig",
  851. .output = "1 2 3 4 5",
  852. .skip = true,
  853. },
  854. .{
  855. .main_file = "088_async5.zig",
  856. .output = "Example Title.",
  857. .skip = true,
  858. },
  859. .{
  860. .main_file = "089_async6.zig",
  861. .output = ".com: Example Title, .org: Example Title.",
  862. .skip = true,
  863. },
  864. .{
  865. .main_file = "090_async7.zig",
  866. .output = "beef? BEEF!",
  867. .skip = true,
  868. },
  869. .{
  870. .main_file = "091_async8.zig",
  871. .output = "ABCDEF",
  872. .skip = true,
  873. },
  874. .{
  875. .main_file = "092_interfaces.zig",
  876. .output =
  877. \\Daily Insect Report:
  878. \\Ant is alive.
  879. \\Bee visited 17 flowers.
  880. \\Grasshopper hopped 32 meters.
  881. ,
  882. },
  883. .{
  884. .main_file = "093_hello_c.zig",
  885. .output = "Hello C from Zig! - C result is 17 chars written.",
  886. .link_libc = true,
  887. },
  888. .{
  889. .main_file = "094_c_math.zig",
  890. .output = "The normalized angle of 765.2 degrees is 45.2 degrees.",
  891. .link_libc = true,
  892. },
  893. .{
  894. .main_file = "095_for3.zig",
  895. .output = "1 2 4 7 8 11 13 14 16 17 19",
  896. },
  897. .{
  898. .main_file = "096_memory_allocation.zig",
  899. .output = "Running Average: 0.30 0.25 0.20 0.18 0.22",
  900. },
  901. .{
  902. .main_file = "097_bit_manipulation.zig",
  903. .output = "x = 0; y = 1",
  904. },
  905. .{
  906. .main_file = "098_bit_manipulation2.zig",
  907. .output = "Is this a pangram? true!",
  908. },
  909. .{
  910. .main_file = "099_formatting.zig",
  911. .output =
  912. \\
  913. \\ X | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  914. \\---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  915. \\ 1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  916. \\
  917. \\ 2 | 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
  918. \\
  919. \\ 3 | 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45
  920. \\
  921. \\ 4 | 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
  922. \\
  923. \\ 5 | 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
  924. \\
  925. \\ 6 | 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90
  926. \\
  927. \\ 7 | 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105
  928. \\
  929. \\ 8 | 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120
  930. \\
  931. \\ 9 | 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135
  932. \\
  933. \\10 | 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
  934. \\
  935. \\11 | 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165
  936. \\
  937. \\12 | 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180
  938. \\
  939. \\13 | 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195
  940. \\
  941. \\14 | 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210
  942. \\
  943. \\15 | 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225
  944. ,
  945. },
  946. .{
  947. .main_file = "100_for4.zig",
  948. .output = "Arrays match!",
  949. },
  950. .{
  951. .main_file = "101_for5.zig",
  952. .output =
  953. \\1. Wizard (Gold: 25, XP: 40)
  954. \\2. Bard (Gold: 11, XP: 17)
  955. \\3. Bard (Gold: 5, XP: 55)
  956. \\4. Warrior (Gold: 7392, XP: 21)
  957. ,
  958. },
  959. .{
  960. .main_file = "102_testing.zig",
  961. .output = "",
  962. .kind = .@"test",
  963. },
  964. .{
  965. .main_file = "999_the_end.zig",
  966. .output =
  967. \\
  968. \\This is the end for now!
  969. \\We hope you had fun and were able to learn a lot, so visit us again when the next exercises are available.
  970. ,
  971. },
  972. };