From 6f482a1f7a7142d42991a9eff26549f2db21a354 Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Sun, 30 Apr 2023 15:27:51 +0200 Subject: [PATCH 1/7] build: move exercises at the end of the file The exercises slice takes about 460 lines, making it hard to read the source code of build.zig. Closes #225 --- build.zig | 1010 ++++++++++++++++++++++++++--------------------------- 1 file changed, 505 insertions(+), 505 deletions(-) diff --git a/build.zig b/build.zig index c5c0fc8..0e2c885 100644 --- a/build.zig +++ b/build.zig @@ -65,470 +65,6 @@ pub const Exercise = struct { } }; -const exercises = [_]Exercise{ - .{ - .main_file = "001_hello.zig", - .output = "Hello world!", - .hint = "DON'T PANIC!\nRead the error above.\nSee how it has something to do with 'main'?\nOpen up the source file as noted and read the comments.\nYou can do this!", - }, - .{ - .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", - .output = "Ziggy played guitar\nJamming good with Andrew Kelley\nAnd the Spiders from Mars", - .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", - .output = "

\n Red\n Green\n Blue\n

", - .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", - .output = "Character 1 - G:20 H:100 XP:10\nCharacter 2 - G:10 H:100 XP:20", - }, - .{ - .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", - .output = "Wizard (G:10 H:100 XP:20)\n Mentor: Wizard (G:10000 H:100 XP:2340)", - }, - .{ - .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", - .output = "Hand1: A 4 K 8 \nHand2: 5 2 Q J", - }, - .{ - .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", - .output = "Minnow (1:32, 4 x 2)\nShark (1:16, 8 x 5)\nWhale (1:1, 143 x 95)", - }, - .{ - .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", - .output = "\"Quack.\" ducky1: true, \"Squeek!\" ducky2: true, ducky3: false", - .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", - .output = "\"0\"(bool):true \"1\"(bool):false \"2\"(i32):42 \"3\"(f32):3.14159202e+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.", - .@"async" = true, - .skip = true, - }, - .{ - .main_file = "085_async2.zig", - .output = "Hello async!", - .@"async" = true, - .skip = true, - }, - .{ - .main_file = "086_async3.zig", - .output = "5 4 3 2 1", - .@"async" = true, - .skip = true, - }, - .{ - .main_file = "087_async4.zig", - .output = "1 2 3 4 5", - .@"async" = true, - .skip = true, - }, - .{ - .main_file = "088_async5.zig", - .output = "Example Title.", - .@"async" = true, - .skip = true, - }, - .{ - .main_file = "089_async6.zig", - .output = ".com: Example Title, .org: Example Title.", - .@"async" = true, - .skip = true, - }, - .{ - .main_file = "090_async7.zig", - .output = "beef? BEEF!", - .@"async" = true, - .skip = true, - }, - .{ - .main_file = "091_async8.zig", - .output = "ABCDEF", - .@"async" = true, - .skip = true, - }, - - .{ - .main_file = "092_interfaces.zig", - .output = "Daily Insect Report:\nAnt is alive.\nBee visited 17 flowers.\nGrasshopper hopped 32 meters.", - }, - .{ - .main_file = "093_hello_c.zig", - .output = "Hello C from Zig! - C result is 17 chars written.", - .C = true, - }, - .{ - .main_file = "094_c_math.zig", - .output = "The normalized angle of 765.2 degrees is 45.2 degrees.", - .C = true, - }, - .{ - .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", - .output = "\n X | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 \n---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n 1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 \n\n 2 | 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 \n\n 3 | 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 \n\n 4 | 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 \n\n 5 | 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 \n\n 6 | 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 \n\n 7 | 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 \n\n 8 | 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 \n\n 9 | 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 \n\n10 | 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 \n\n11 | 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 \n\n12 | 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180 \n\n13 | 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195 \n\n14 | 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210 \n\n15 | 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225", - }, - .{ - .main_file = "100_for4.zig", - .output = "Arrays match!", - }, - .{ - .main_file = "999_the_end.zig", - .output = "\nThis is the end for now!\nWe hope you had fun and were able to learn a lot, so visit us again when the next exercises are available.", - }, -}; - pub fn build(b: *Build) !void { if (!compat.is_compatible) compat.die(); if (!validate_exercises()) std.os.exit(1); @@ -1049,48 +585,512 @@ const SkipStep = struct { .exercise = exercise, }; - return self; + return self; + } + + fn make(step: *Step, prog_node: *std.Progress.Node) !void { + _ = prog_node; + const p = @fieldParentPtr(SkipStep, "step", step); + + if (p.exercise.skip) { + print("{s} skipped\n", .{p.exercise.main_file}); + } + } +}; + +// Check that each exercise number, excluding the last, forms the sequence +// `[1, exercise.len)`. +// +// Additionally check that the output field does not contain trailing whitespace. +fn validate_exercises() bool { + // Don't use the "multi-object for loop" syntax, in order to avoid a syntax + // error with old Zig compilers. + var i: usize = 0; + for (exercises[0 .. exercises.len - 1]) |ex| { + i += 1; + if (ex.number() != i) { + print("exercise {s} has an incorrect number: expected {}, got {s}\n", .{ + ex.main_file, + i, + ex.key(), + }); + + return false; + } + + const output = std.mem.trimRight(u8, ex.output, " \r\n"); + if (output.len != ex.output.len) { + print("exercise {s} output field has extra trailing whitespace\n", .{ + ex.main_file, + }); + + return false; + } } - fn make(step: *Step, prog_node: *std.Progress.Node) !void { - _ = prog_node; - const p = @fieldParentPtr(SkipStep, "step", step); + return true; +} + +const exercises = [_]Exercise{ + .{ + .main_file = "001_hello.zig", + .output = "Hello world!", + .hint = "DON'T PANIC!\nRead the error above.\nSee how it has something to do with 'main'?\nOpen up the source file as noted and read the comments.\nYou can do this!", + }, + .{ + .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", + .output = "Ziggy played guitar\nJamming good with Andrew Kelley\nAnd the Spiders from Mars", + .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", + .output = "

\n Red\n Green\n Blue\n

", + .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", + .output = "Character 1 - G:20 H:100 XP:10\nCharacter 2 - G:10 H:100 XP:20", + }, + .{ + .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", + .output = "Wizard (G:10 H:100 XP:20)\n Mentor: Wizard (G:10000 H:100 XP:2340)", + }, + .{ + .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", + .output = "Hand1: A 4 K 8 \nHand2: 5 2 Q J", + }, + .{ + .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", + .output = "Minnow (1:32, 4 x 2)\nShark (1:16, 8 x 5)\nWhale (1:1, 143 x 95)", + }, + .{ + .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", + .output = "\"Quack.\" ducky1: true, \"Squeek!\" ducky2: true, ducky3: false", + .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", + .output = "\"0\"(bool):true \"1\"(bool):false \"2\"(i32):42 \"3\"(f32):3.14159202e+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.", + .@"async" = true, + .skip = true, + }, + .{ + .main_file = "085_async2.zig", + .output = "Hello async!", + .@"async" = true, + .skip = true, + }, + .{ + .main_file = "086_async3.zig", + .output = "5 4 3 2 1", + .@"async" = true, + .skip = true, + }, + .{ + .main_file = "087_async4.zig", + .output = "1 2 3 4 5", + .@"async" = true, + .skip = true, + }, + .{ + .main_file = "088_async5.zig", + .output = "Example Title.", + .@"async" = true, + .skip = true, + }, + .{ + .main_file = "089_async6.zig", + .output = ".com: Example Title, .org: Example Title.", + .@"async" = true, + .skip = true, + }, + .{ + .main_file = "090_async7.zig", + .output = "beef? BEEF!", + .@"async" = true, + .skip = true, + }, + .{ + .main_file = "091_async8.zig", + .output = "ABCDEF", + .@"async" = true, + .skip = true, + }, - if (p.exercise.skip) { - print("{s} skipped\n", .{p.exercise.main_file}); - } - } + .{ + .main_file = "092_interfaces.zig", + .output = "Daily Insect Report:\nAnt is alive.\nBee visited 17 flowers.\nGrasshopper hopped 32 meters.", + }, + .{ + .main_file = "093_hello_c.zig", + .output = "Hello C from Zig! - C result is 17 chars written.", + .C = true, + }, + .{ + .main_file = "094_c_math.zig", + .output = "The normalized angle of 765.2 degrees is 45.2 degrees.", + .C = true, + }, + .{ + .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", + .output = "\n X | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 \n---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n 1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 \n\n 2 | 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 \n\n 3 | 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 \n\n 4 | 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 \n\n 5 | 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 \n\n 6 | 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 \n\n 7 | 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 \n\n 8 | 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 \n\n 9 | 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 \n\n10 | 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 \n\n11 | 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 \n\n12 | 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180 \n\n13 | 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195 \n\n14 | 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210 \n\n15 | 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225", + }, + .{ + .main_file = "100_for4.zig", + .output = "Arrays match!", + }, + .{ + .main_file = "999_the_end.zig", + .output = "\nThis is the end for now!\nWe hope you had fun and were able to learn a lot, so visit us again when the next exercises are available.", + }, }; - -// Check that each exercise number, excluding the last, forms the sequence -// `[1, exercise.len)`. -// -// Additionally check that the output field does not contain trailing whitespace. -fn validate_exercises() bool { - // Don't use the "multi-object for loop" syntax, in order to avoid a syntax - // error with old Zig compilers. - var i: usize = 0; - for (exercises[0 .. exercises.len - 1]) |ex| { - i += 1; - if (ex.number() != i) { - print("exercise {s} has an incorrect number: expected {}, got {s}\n", .{ - ex.main_file, - i, - ex.key(), - }); - - return false; - } - - const output = std.mem.trimRight(u8, ex.output, " \r\n"); - if (output.len != ex.output.len) { - print("exercise {s} output field has extra trailing whitespace\n", .{ - ex.main_file, - }); - - return false; - } - } - - return true; -} From 070734a520d4bb1d08edae24a09180d52345a895 Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Sun, 30 Apr 2023 15:33:29 +0200 Subject: [PATCH 2/7] build: remove the Exercise.async field The stage1 C++ compiler is gone forever. Remove the custom support and documentation for the old stage1 compiler in build.zig and README.md. --- README.md | 3 --- build.zig | 18 ------------------ 2 files changed, 21 deletions(-) diff --git a/README.md b/README.md index 8e0148d..548303a 100644 --- a/README.md +++ b/README.md @@ -74,9 +74,6 @@ Once you have a build of the Zig compiler that works with Ziglings, they'll continue to work together. But keep in mind that if you update one, you may need to also update the other. -Also note that the current "stage 1" Zig compiler is very strict -about input: -[no tab characters or Windows CR/LF newlines are allowed](https://github.com/ziglang/zig/issues/544). ### Version Changes diff --git a/build.zig b/build.zig index 0e2c885..f7a279a 100644 --- a/build.zig +++ b/build.zig @@ -29,10 +29,6 @@ pub const Exercise = struct { /// Set this to true to check stdout instead. check_stdout: bool = false, - /// This exercise makes use of the async feature. - /// We need to keep track of this, so we compile without the self hosted compiler - @"async": bool = false, - /// This exercise makes use of C functions /// We need to keep track of this, so we compile with libc C: bool = false, @@ -356,12 +352,6 @@ const ZiglingStep = struct { zig_args.append(builder.zig_exe) catch unreachable; zig_args.append("build-exe") catch unreachable; - // Enable the stage 1 compiler if using the async feature - // disabled because of https://github.com/ratfactor/ziglings/issues/163 - // if (self.exercise.@"async") { - // zig_args.append("-fstage1") catch unreachable; - // } - // Enable C support for exercises that use C functions if (self.exercise.C) { zig_args.append("-lc") catch unreachable; @@ -1005,49 +995,41 @@ const exercises = [_]Exercise{ .main_file = "084_async.zig", .output = "foo() A", .hint = "Read the facts. Use the facts.", - .@"async" = true, .skip = true, }, .{ .main_file = "085_async2.zig", .output = "Hello async!", - .@"async" = true, .skip = true, }, .{ .main_file = "086_async3.zig", .output = "5 4 3 2 1", - .@"async" = true, .skip = true, }, .{ .main_file = "087_async4.zig", .output = "1 2 3 4 5", - .@"async" = true, .skip = true, }, .{ .main_file = "088_async5.zig", .output = "Example Title.", - .@"async" = true, .skip = true, }, .{ .main_file = "089_async6.zig", .output = ".com: Example Title, .org: Example Title.", - .@"async" = true, .skip = true, }, .{ .main_file = "090_async7.zig", .output = "beef? BEEF!", - .@"async" = true, .skip = true, }, .{ .main_file = "091_async8.zig", .output = "ABCDEF", - .@"async" = true, .skip = true, }, From ec1976e9abd777b674c3090ffd17a79630292284 Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Sun, 30 Apr 2023 16:07:30 +0200 Subject: [PATCH 3/7] build: reduce code duplication when setting the work path Currently, the code for defining the path to the exercises directory is duplicate 4 times. Add the constants `healed_path` and `work_path`, and use work_path instead of the duplicated if expression. Update ZiglingStep to take `work_path` instead of `use_healed` as argument. Reduce code length by using `join` instead of `std.fs.path.join` and replace the use of a slice with a tuple. Additionally, in case of an error from the `join` function, use @panic instead of unreachable. Document why the special branch, when the exercises are healed by the eowyn script, has been disabled. --- build.zig | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/build.zig b/build.zig index f7a279a..1dcce9b 100644 --- a/build.zig +++ b/build.zig @@ -9,6 +9,7 @@ const Step = compat.build.Step; const Child = std.process.Child; const assert = std.debug.assert; +const join = std.fs.path.join; const print = std.debug.print; pub const Exercise = struct { @@ -108,6 +109,9 @@ pub fn build(b: *Build) !void { const use_healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse false; const exno: ?usize = b.option(usize, "n", "Select exercise"); + const healed_path = "patches/healed"; + const work_path = if (use_healed) healed_path else "exercises"; + const header_step = PrintStep.create(b, logo); if (exno) |n| { @@ -118,9 +122,8 @@ pub fn build(b: *Build) !void { const ex = exercises[n - 1]; const base_name = ex.baseName(); - const file_path = std.fs.path.join(b.allocator, &[_][]const u8{ - if (use_healed) "patches/healed" else "exercises", ex.main_file, - }) catch unreachable; + const file_path = join(b.allocator, &.{ work_path, ex.main_file }) catch + @panic("OOM"); const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); if (ex.C) { @@ -144,7 +147,7 @@ pub fn build(b: *Build) !void { const uninstall_step = b.step("uninstall", b.fmt("Uninstall {s} from prefix path", .{ex.main_file})); uninstall_step.dependOn(b.getUninstallStep()); - const verify_step = ZiglingStep.create(b, ex, use_healed); + const verify_step = ZiglingStep.create(b, ex, work_path); const zigling_step = b.step("zigling", b.fmt("Check the solution of {s}", .{ex.main_file})); zigling_step.dependOn(&verify_step.step); @@ -156,7 +159,7 @@ pub fn build(b: *Build) !void { for (exercises) |exn| { const nth = exn.number(); if (nth > n) { - const verify_stepn = ZiglingStep.create(b, exn, use_healed); + const verify_stepn = ZiglingStep.create(b, exn, work_path); verify_stepn.step.dependOn(&prev_step.step); prev_step = verify_stepn; @@ -166,14 +169,18 @@ pub fn build(b: *Build) !void { return; } else if (use_healed and false) { + // Special case when healed by the eowyn script, where we can make the + // code more efficient. + // + // TODO: this branch is disabled because it prevents the normal case to + // be executed. const test_step = b.step("test", "Test the healed exercises"); b.default_step = test_step; for (exercises) |ex| { const base_name = ex.baseName(); - const file_path = std.fs.path.join(b.allocator, &[_][]const u8{ - "patches/healed", ex.main_file, - }) catch unreachable; + const file_path = join(b.allocator, &.{ healed_path, ex.main_file }) catch + @panic("OOM"); const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); if (ex.C) { @@ -201,14 +208,13 @@ pub fn build(b: *Build) !void { var prev_step = &header_step.step; for (exercises) |ex| { const base_name = ex.baseName(); - const file_path = std.fs.path.join(b.allocator, &[_][]const u8{ - "exercises", ex.main_file, - }) catch unreachable; + const file_path = join(b.allocator, &.{ "exercises", ex.main_file }) catch + @panic("OOM"); const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); b.installArtifact(build_step); - const verify_stepn = ZiglingStep.create(b, ex, use_healed); + const verify_stepn = ZiglingStep.create(b, ex, work_path); verify_stepn.step.dependOn(prev_step); prev_step = &verify_stepn.step; @@ -229,18 +235,18 @@ const ZiglingStep = struct { step: Step, exercise: Exercise, builder: *Build, - use_healed: bool, + work_path: []const u8, result_messages: []const u8 = "", result_error_bundle: std.zig.ErrorBundle = std.zig.ErrorBundle.empty, - pub fn create(builder: *Build, exercise: Exercise, use_healed: bool) *@This() { + pub fn create(builder: *Build, exercise: Exercise, work_path: []const u8) *@This() { const self = builder.allocator.create(@This()) catch unreachable; self.* = .{ .step = Step.init(Step.Options{ .id = .custom, .name = exercise.main_file, .owner = builder, .makeFn = make }), .exercise = exercise, .builder = builder, - .use_healed = use_healed, + .work_path = work_path, }; return self; } @@ -357,7 +363,7 @@ const ZiglingStep = struct { zig_args.append("-lc") catch unreachable; } - const zig_file = std.fs.path.join(builder.allocator, &[_][]const u8{ if (self.use_healed) "patches/healed" else "exercises", self.exercise.main_file }) catch unreachable; + const zig_file = join(builder.allocator, &.{ self.work_path, self.exercise.main_file }) catch unreachable; zig_args.append(builder.pathFromRoot(zig_file)) catch unreachable; zig_args.append("--cache-dir") catch unreachable; From 123fd4b1057783de7b8c9b7b536f61133dbad016 Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Sun, 30 Apr 2023 16:25:15 +0200 Subject: [PATCH 4/7] build: rename the Exercise.C field to link_libc The name "C" does not follow the naming conventions for container fields. --- build.zig | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/build.zig b/build.zig index 1dcce9b..efeab39 100644 --- a/build.zig +++ b/build.zig @@ -32,7 +32,7 @@ pub const Exercise = struct { /// This exercise makes use of C functions /// We need to keep track of this, so we compile with libc - C: bool = false, + link_libc: bool = false, /// This exercise is not supported by the current Zig compiler. skip: bool = false, @@ -126,7 +126,7 @@ pub fn build(b: *Build) !void { @panic("OOM"); const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); - if (ex.C) { + if (ex.link_libc) { build_step.linkLibC(); } b.installArtifact(build_step); @@ -183,7 +183,7 @@ pub fn build(b: *Build) !void { @panic("OOM"); const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); - if (ex.C) { + if (ex.link_libc) { build_step.linkLibC(); } b.installArtifact(build_step); @@ -359,7 +359,7 @@ const ZiglingStep = struct { zig_args.append("build-exe") catch unreachable; // Enable C support for exercises that use C functions - if (self.exercise.C) { + if (self.exercise.link_libc) { zig_args.append("-lc") catch unreachable; } @@ -1046,12 +1046,12 @@ const exercises = [_]Exercise{ .{ .main_file = "093_hello_c.zig", .output = "Hello C from Zig! - C result is 17 chars written.", - .C = true, + .link_libc = true, }, .{ .main_file = "094_c_math.zig", .output = "The normalized angle of 765.2 degrees is 45.2 degrees.", - .C = true, + .link_libc = true, }, .{ .main_file = "095_for3.zig", From 69103a3b821d8b8955eac59489b0dabb6f4735ff Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Sun, 30 Apr 2023 17:47:16 +0200 Subject: [PATCH 5/7] build: add the Exercise.addExecutable method Currently addExecutable is called 3 times, unnecessarily making the code more complex. The method takes as argument the path to the exercises directory. Additionally, use the new std.Build.ExecutableOptions.link_libc field. The new field was added in ziglang/zig@adc9b77d5f on 2023-04-13. Update the required Zig compiler version. Note that I added the **current** zig version to the changelog, since the reason for the change is known only to the person updating the version. --- README.md | 6 ++++-- build.zig | 36 ++++++++++++++++-------------------- src/compat.zig | 2 +- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 548303a..dff3f2d 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Verify the installation and build number of `zig` like so: ``` $ zig version -0.11.0-dev.2560+xxxxxxxxx +0.11.0-dev.2704+xxxxxxxxx ``` Clone this repository with Git: @@ -77,7 +77,9 @@ need to also update the other. ### Version Changes -Version-0.11.0-dev.2560+602029bb2 +Version-0.11.0-dev.2704+83970b6d9 +* *2023-04-30* zig 0.11.0-dev.2704 - use of the new `std.Build.ExecutableOptions.link_libc` field +* *2023-04-12* zig 0.11.0-dev.2560 - changes in `std.Build` - remove run() and install() * *2023-04-07* zig 0.11.0-dev.2401 - fixes of the new build system - see [#212](https://github.com/ratfactor/ziglings/pull/212) * *2023-02-21* zig 0.11.0-dev.2157 - changes in `build system` - new: parallel processing of the build steps * *2023-02-21* zig 0.11.0-dev.1711 - changes in `for loops` - new: Multi-Object For-Loops + Struct-of-Arrays diff --git a/build.zig b/build.zig index efeab39..b9c59d6 100644 --- a/build.zig +++ b/build.zig @@ -5,6 +5,7 @@ const ipc = @import("src/ipc.zig"); const tests = @import("test/tests.zig"); const Build = compat.Build; +const CompileStep = compat.build.CompileStep; const Step = compat.build.Step; const Child = std.process.Child; @@ -60,6 +61,18 @@ pub const Exercise = struct { pub fn number(self: Exercise) usize { return std.fmt.parseInt(usize, self.key(), 10) catch unreachable; } + + /// Returns the CompileStep for this exercise. + pub fn addExecutable(self: Exercise, b: *Build, work_path: []const u8) *CompileStep { + const file_path = join(b.allocator, &.{ work_path, self.main_file }) catch + @panic("OOM"); + + return b.addExecutable(.{ + .name = self.baseName(), + .root_source_file = .{ .path = file_path }, + .link_libc = self.link_libc, + }); + } }; pub fn build(b: *Build) !void { @@ -121,14 +134,8 @@ pub fn build(b: *Build) !void { } const ex = exercises[n - 1]; - const base_name = ex.baseName(); - const file_path = join(b.allocator, &.{ work_path, ex.main_file }) catch - @panic("OOM"); - const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); - if (ex.link_libc) { - build_step.linkLibC(); - } + const build_step = ex.addExecutable(b, work_path); b.installArtifact(build_step); const run_step = b.addRunArtifact(build_step); @@ -178,14 +185,7 @@ pub fn build(b: *Build) !void { b.default_step = test_step; for (exercises) |ex| { - const base_name = ex.baseName(); - const file_path = join(b.allocator, &.{ healed_path, ex.main_file }) catch - @panic("OOM"); - - const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); - if (ex.link_libc) { - build_step.linkLibC(); - } + const build_step = ex.addExecutable(b, healed_path); b.installArtifact(build_step); const run_step = b.addRunArtifact(build_step); @@ -207,11 +207,7 @@ pub fn build(b: *Build) !void { // error with old Zig compilers. var prev_step = &header_step.step; for (exercises) |ex| { - const base_name = ex.baseName(); - const file_path = join(b.allocator, &.{ "exercises", ex.main_file }) catch - @panic("OOM"); - - const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); + const build_step = ex.addExecutable(b, "exercises"); b.installArtifact(build_step); const verify_stepn = ZiglingStep.create(b, ex, work_path); diff --git a/src/compat.zig b/src/compat.zig index cd7f3e5..42ecb6b 100644 --- a/src/compat.zig +++ b/src/compat.zig @@ -15,7 +15,7 @@ const print = if (@hasDecl(debug, "print")) debug.print else debug.warn; // When changing this version, be sure to also update README.md in two places: // 1) Getting Started // 2) Version Changes -const needed_version_str = "0.11.0-dev.2560"; +const needed_version_str = "0.11.0-dev.2704"; fn isCompatible() bool { if (!@hasDecl(builtin, "zig_version") or !@hasDecl(std, "SemanticVersion")) { From 5c20e2b55338e829b2c2db2bbd5b1fe410b656af Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Sun, 30 Apr 2023 18:48:52 +0200 Subject: [PATCH 6/7] build: rename Exercise.baseName to name The name "baseName" is confusing, since it refers to the UNIX basename command, and not to the basename function in some programming languages including Zig. Use the std.fs.path.stem function to remove the file extension, instead of slicing. Remove the use of the assertion, since it is no longer necessary. Instead, add a check to ensure that the exercise must be a Zig source file in the validate_exercises function. Update the validate_exercises function to check the last exercise, too. --- build.zig | 20 ++++++++++++++------ test/tests.zig | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/build.zig b/build.zig index b9c59d6..e1f5556 100644 --- a/build.zig +++ b/build.zig @@ -39,9 +39,8 @@ pub const Exercise = struct { skip: bool = false, /// Returns the name of the main file with .zig stripped. - pub fn baseName(self: Exercise) []const u8 { - assert(std.mem.endsWith(u8, self.main_file, ".zig")); - return self.main_file[0 .. self.main_file.len - 4]; + pub fn name(self: Exercise) []const u8 { + return std.fs.path.stem(self.main_file); } /// Returns the key of the main file, the string before the '_' with @@ -68,7 +67,7 @@ pub const Exercise = struct { @panic("OOM"); return b.addExecutable(.{ - .name = self.baseName(), + .name = self.name(), .root_source_file = .{ .path = file_path }, .link_libc = self.link_libc, }); @@ -598,9 +597,12 @@ fn validate_exercises() bool { // Don't use the "multi-object for loop" syntax, in order to avoid a syntax // error with old Zig compilers. var i: usize = 0; - for (exercises[0 .. exercises.len - 1]) |ex| { + for (exercises[0..]) |ex| { + const exno = ex.number(); + const last = 999; i += 1; - if (ex.number() != i) { + + if (exno != i and exno != last) { print("exercise {s} has an incorrect number: expected {}, got {s}\n", .{ ex.main_file, i, @@ -618,6 +620,12 @@ fn validate_exercises() bool { return false; } + + if (!std.mem.endsWith(u8, ex.main_file, ".zig")) { + print("exercise {s} is not a zig source file\n", .{ex.main_file}); + + return false; + } } return true; diff --git a/test/tests.zig b/test/tests.zig index f9ad9c4..8786d91 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -323,7 +323,7 @@ fn heal(allocator: Allocator, exercises: []const Exercise, outdir: []const u8) ! const patches_path = "patches/patches"; for (exercises) |ex| { - const name = ex.baseName(); + const name = ex.name(); // Use the POSIX patch variant. const file = try join(allocator, &.{ exercises_path, ex.main_file }); From a86d4c279577cb263d83f33d9d9606eeafda05ab Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Mon, 1 May 2023 18:23:13 +0200 Subject: [PATCH 7/7] build: don't override the top level steps When running `zig build -Dn=n`, the install and uninstall steps where overridden in order to improve the description. In recent version of Zig this is no longer allowed. --- build.zig | 6 ------ 1 file changed, 6 deletions(-) diff --git a/build.zig b/build.zig index e1f5556..383a231 100644 --- a/build.zig +++ b/build.zig @@ -147,12 +147,6 @@ pub fn build(b: *Build) !void { test_step.dependOn(&run_step.step); } - const install_step = b.step("install", b.fmt("Install {s} to prefix path", .{ex.main_file})); - install_step.dependOn(b.getInstallStep()); - - const uninstall_step = b.step("uninstall", b.fmt("Uninstall {s} from prefix path", .{ex.main_file})); - uninstall_step.dependOn(b.getUninstallStep()); - const verify_step = ZiglingStep.create(b, ex, work_path); const zigling_step = b.step("zigling", b.fmt("Check the solution of {s}", .{ex.main_file}));