2021-02-11 23:04:36 -05:00
const std = @import ( " std " ) ;
2021-10-09 10:19:34 -04:00
const builtin = @import ( " builtin " ) ;
2023-04-03 05:06:39 -04:00
const compat = @import ( " src/compat.zig " ) ;
2023-04-09 12:33:59 -04:00
const tests = @import ( " test/tests.zig " ) ;
2023-04-03 05:06:39 -04:00
const Build = compat . Build ;
const Step = compat . build . Step ;
2023-04-13 12:03:00 -04:00
const Child = std . process . Child ;
2023-04-03 05:06:39 -04:00
2021-02-11 23:04:36 -05:00
const assert = std . debug . assert ;
const print = std . debug . print ;
2023-04-09 12:33:59 -04:00
pub const Exercise = struct {
2021-02-11 23:04:36 -05:00
/// main_file must have the format key_name.zig.
/// The key will be used as a shorthand to build
/// just one example.
main_file : [ ] const u8 ,
/// This is the desired output of the program.
/// A program passes if its output ends with this string.
output : [ ] const u8 ,
/// This is an optional hint to give if the program does not succeed.
hint : [ ] const u8 = " " ,
/// By default, we verify output against stderr.
/// Set this to true to check stdout instead.
check_stdout : bool = false ,
2022-08-29 03:17:42 -04:00
/// 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 ,
2023-02-15 16:55:44 -05:00
/// This exercise makes use of C functions
/// We need to keep track of this, so we compile with libc
C : bool = false ,
2023-04-08 06:56:51 -04:00
/// This exercise is not supported by the current Zig compiler.
skip : bool = false ,
2021-02-11 23:04:36 -05:00
/// 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 ] ;
}
2021-03-12 18:59:46 -05: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-11 23:04:36 -05:00
pub fn key ( self : Exercise ) [ ] const u8 {
const end_index = std . mem . indexOfScalar ( u8 , self . main_file , '_' ) ;
assert ( end_index ! = null ) ; // main file must be key_description.zig
2021-03-12 18:59:46 -05:00
// remove zero padding by advancing index past '0's
var start_index : usize = 0 ;
while ( self . main_file [ start_index ] = = '0' ) start_index + = 1 ;
return self . main_file [ start_index . . end_index . ? ] ;
2021-02-11 23:04:36 -05: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 06:47:08 -04:00
/// Returns the exercise key as an integer.
pub fn number ( self : Exercise ) usize {
return std . fmt . parseInt ( usize , self . key ( ) , 10 ) catch unreachable ;
}
2021-02-11 23:04:36 -05:00
} ;
const exercises = [ _ ] Exercise {
. {
2021-03-12 18:59:46 -05:00
. main_file = " 001_hello.zig " ,
2022-11-16 07:00:36 -05:00
. output = " Hello world! " ,
2021-02-27 16:52:57 -05:00
. hint = " DON'T PANIC! \n Read the error above. \n See how it has something to do with 'main'? \n Open up the source file as noted and read the comments. \n You can do this! " ,
2021-02-11 23:04:36 -05:00
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 002_std.zig " ,
2022-11-16 07:00:36 -05:00
. output = " Standard Library. " ,
2021-02-11 23:04:36 -05:00
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 003_assignment.zig " ,
2021-02-11 23:04:36 -05:00
. output = " 55 314159 -11 " ,
. hint = " There are three mistakes in this one! " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 004_arrays.zig " ,
2022-06-11 08:27:11 -04:00
. output = " First: 2, Fourth: 7, Length: 8 " ,
2021-02-11 23:04:36 -05:00
. hint = " There are two things to complete here. " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 005_arrays2.zig " ,
2021-02-11 23:04:36 -05:00
. output = " LEET: 1337, Bits: 100110011001 " ,
. hint = " Fill in the two arrays. " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 006_strings.zig " ,
2021-02-11 23:04:36 -05:00
. output = " d=d ha ha ha Major Tom " ,
. hint = " Each '???' needs something filled in. " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 007_strings2.zig " ,
2021-04-04 16:23:27 -04:00
. output = " Ziggy played guitar \n Jamming good with Andrew Kelley \n And the Spiders from Mars " ,
2021-02-11 23:04:36 -05:00
. hint = " Please fix the lyrics! " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 008_quiz.zig " ,
2021-04-04 16:23:27 -04:00
. output = " Program in Zig! " ,
2021-02-11 23:04:36 -05:00
. hint = " See if you can fix the program! " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 009_if.zig " ,
2021-02-11 23:04:36 -05:00
. output = " Foo is 1! " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 010_if2.zig " ,
2021-04-04 16:23:27 -04:00
. output = " With the discount, the price is $17. " ,
2021-02-11 23:04:36 -05:00
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 011_while.zig " ,
2021-04-04 16:23:27 -04:00
. output = " 2 4 8 16 32 64 128 256 512 n=1024 " ,
2021-02-11 23:04:36 -05:00
. hint = " You probably want a 'less than' condition. " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 012_while2.zig " ,
2021-04-04 16:23:27 -04:00
. output = " 2 4 8 16 32 64 128 256 512 n=1024 " ,
2021-02-11 23:04:36 -05:00
. hint = " It might help to look back at the previous exercise. " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 013_while3.zig " ,
2021-02-11 23:04:36 -05:00
. output = " 1 2 4 7 8 11 13 14 16 17 19 " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 014_while4.zig " ,
2021-02-11 23:04:36 -05:00
. output = " n=4 " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 015_for.zig " ,
2021-02-11 23:04:36 -05:00
. output = " A Dramatic Story: :-) :-) :-( :-| :-) The End. " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 016_for2.zig " ,
2022-11-16 07:43:34 -05:00
. output = " The value of bits '1101': 13. " ,
2021-02-11 23:04:36 -05:00
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 017_quiz2.zig " ,
2021-04-04 16:23:27 -04:00
. output = " 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, " ,
2021-02-11 23:04:36 -05:00
. hint = " This is a famous game! " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 018_functions.zig " ,
2021-04-04 16:23:27 -04:00
. output = " Answer to the Ultimate Question: 42 " ,
2021-02-11 23:04:36 -05:00
. hint = " Can you help write the function? " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 019_functions2.zig " ,
2022-11-16 07:43:34 -05:00
. output = " Powers of two: 2 4 8 16 " ,
2021-02-11 23:04:36 -05:00
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 020_quiz3.zig " ,
2021-02-11 23:04:36 -05:00
. output = " 32 64 128 256 " ,
. hint = " Unexpected pop quiz! Help! " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 021_errors.zig " ,
2021-02-11 23:04:36 -05:00
. output = " 2<4. 3<4. 4=4. 5>4. 6>4. " ,
. hint = " What's the deal with fours? " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 022_errors2.zig " ,
2022-11-16 07:43:34 -05:00
. output = " I compiled! " ,
2021-02-11 23:04:36 -05:00
. hint = " Get the error union type right to allow this to compile. " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 023_errors3.zig " ,
2021-02-11 23:04:36 -05:00
. output = " a=64, b=22 " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 024_errors4.zig " ,
2021-02-11 23:04:36 -05:00
. output = " a=20, b=14, c=10 " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 025_errors5.zig " ,
2021-02-11 23:04:36 -05:00
. output = " a=0, b=19, c=0 " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 026_hello2.zig " ,
2021-04-04 16:23:27 -04:00
. output = " Hello world! " ,
2021-02-11 23:04:36 -05:00
. hint = " Try using a try! " ,
. check_stdout = true ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 027_defer.zig " ,
2021-02-11 23:04:36 -05:00
. output = " One Two " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 028_defer2.zig " ,
2021-02-11 23:04:36 -05:00
. output = " (Goat) (Cat) (Dog) (Dog) (Goat) (Unknown) done. " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 029_errdefer.zig " ,
2021-02-11 23:04:36 -05:00
. output = " Getting number...got 5. Getting number...failed! " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 030_switch.zig " ,
2021-02-11 23:04:36 -05:00
. output = " ZIG? " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 031_switch2.zig " ,
2021-02-11 23:04:36 -05:00
. output = " ZIG! " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 032_unreachable.zig " ,
2021-02-11 23:04:36 -05:00
. output = " 1 2 3 9 8 7 " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 033_iferror.zig " ,
2021-02-11 23:04:36 -05:00
. output = " 2<4. 3<4. 4=4. 5>4. 6>4. " ,
. hint = " Seriously, what's the deal with fours? " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 034_quiz4.zig " ,
2021-02-11 23:04:36 -05:00
. output = " my_num=42 " ,
. hint = " Can you make this work? " ,
. check_stdout = true ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 035_enums.zig " ,
2021-02-11 23:04:36 -05:00
. output = " 1 2 3 9 8 7 " ,
. hint = " This problem seems familiar... " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 036_enums2.zig " ,
2021-04-04 16:23:27 -04:00
. output = " <p> \n <span style= \" color: #ff0000 \" >Red</span> \n <span style= \" color: #00ff00 \" >Green</span> \n <span style= \" color: #0000ff \" >Blue</span> \n </p> " ,
2021-02-11 23:04:36 -05:00
. hint = " I'm feeling blue about this. " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 037_structs.zig " ,
2021-02-11 23:04:36 -05:00
. output = " Your wizard has 90 health and 25 gold. " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 038_structs2.zig " ,
2021-04-04 16:23:27 -04:00
. output = " Character 1 - G:20 H:100 XP:10 \n Character 2 - G:10 H:100 XP:20 " ,
2021-02-11 23:04:36 -05:00
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 039_pointers.zig " ,
2021-02-11 23:04:36 -05:00
. output = " num1: 5, num2: 5 " ,
. hint = " Pointers aren't so bad. " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 040_pointers2.zig " ,
2021-02-11 23:04:36 -05:00
. output = " a: 12, b: 12 " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 041_pointers3.zig " ,
2021-02-11 23:04:36 -05:00
. output = " foo=6, bar=11 " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 042_pointers4.zig " ,
2021-02-11 23:04:36 -05:00
. output = " num: 5, more_nums: 1 1 5 1 " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 043_pointers5.zig " ,
2022-07-31 15:57:20 -04:00
. output = " Wizard (G:10 H:100 XP:20) \n Mentor: Wizard (G:10000 H:100 XP:2340) " ,
2021-02-11 23:04:36 -05:00
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 044_quiz5.zig " ,
2021-02-11 23:04:36 -05:00
. output = " Elephant A. Elephant B. Elephant C. " ,
. hint = " Oh no! We forgot Elephant B! " ,
} ,
2021-02-14 10:49:09 -05:00
. {
2021-03-12 18:59:46 -05:00
. main_file = " 045_optionals.zig " ,
2021-02-14 10:49:09 -05:00
. output = " The Ultimate Answer: 42. " ,
} ,
2021-02-16 20:21:32 -05:00
. {
2021-03-12 18:59:46 -05:00
. main_file = " 046_optionals2.zig " ,
2021-02-16 20:21:32 -05:00
. output = " Elephant A. Elephant B. Elephant C. " ,
2021-02-27 16:52:57 -05:00
. hint = " Elephants again! " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 047_methods.zig " ,
2021-02-27 16:52:57 -05:00
. output = " 5 aliens. 4 aliens. 1 aliens. 0 aliens. Earth is saved! " ,
. hint = " Use the heat ray. And the method! " ,
2021-02-16 20:21:32 -05:00
} ,
2021-02-28 13:23:22 -05:00
. {
2021-03-12 18:59:46 -05:00
. main_file = " 048_methods2.zig " ,
2021-02-28 13:51:33 -05:00
. output = " A B C " ,
. hint = " This just needs one little fix. " ,
} ,
. {
2021-03-12 18:59:46 -05:00
. main_file = " 049_quiz6.zig " ,
2021-02-28 13:51:33 -05:00
. output = " A B C Cv Bv Av " ,
2021-04-24 21:07:58 -04:00
. hint = " Now you're writing Zig! " ,
2021-02-28 13:23:22 -05:00
} ,
2021-02-28 18:36:38 -05:00
. {
2021-03-12 18:59:46 -05:00
. main_file = " 050_no_value.zig " ,
2021-02-28 18:36:38 -05:00
. output = " That is not dead which can eternal lie / And with strange aeons even death may die. " ,
} ,
2021-03-05 11:01:12 -05:00
. {
2021-03-12 18:59:46 -05:00
. main_file = " 051_values.zig " ,
2021-03-05 11:01:12 -05:00
. output = " 1:false!. 2:true!. 3:true!. XP before:0, after:200. " ,
} ,
2021-03-06 18:20:50 -05:00
. {
2021-03-12 18:59:46 -05:00
. main_file = " 052_slices.zig " ,
2021-07-21 06:04:52 -04:00
. output = " Hand1: A 4 K 8 \n Hand2: 5 2 Q J " ,
2021-03-06 18:20:50 -05:00
} ,
2021-03-06 19:16:28 -05:00
. {
2021-03-12 18:59:46 -05:00
. main_file = " 053_slices2.zig " ,
2021-03-06 19:16:28 -05:00
. output = " 'all your base are belong to us.' 'for great justice.' " ,
} ,
2021-03-06 21:31:02 -05:00
. {
2021-03-12 18:59:46 -05:00
. main_file = " 054_manypointers.zig " ,
2021-03-06 21:31:02 -05:00
. output = " Memory is a resource. " ,
} ,
2021-03-09 20:04:43 -05:00
. {
2021-03-12 18:59:46 -05:00
. main_file = " 055_unions.zig " ,
2021-03-09 20:04:43 -05:00
. output = " Insect report! Ant alive is: true. Bee visited 15 flowers. " ,
} ,
2021-03-09 20:51:00 -05:00
. {
2021-03-12 18:59:46 -05:00
. main_file = " 056_unions2.zig " ,
2021-03-09 20:51:00 -05:00
. output = " Insect report! Ant alive is: true. Bee visited 16 flowers. " ,
} ,
2021-03-10 20:14:25 -05:00
. {
2021-03-12 18:59:46 -05:00
. main_file = " 057_unions3.zig " ,
. output = " Insect report! Ant alive is: true. Bee visited 17 flowers. " ,
2021-03-10 20:14:25 -05:00
} ,
2021-07-21 11:33:32 -04:00
. {
. 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! " ,
2021-07-21 10:02:13 -04:00
} ,
2021-04-08 18:42:19 -04:00
. {
. main_file = " 059_integers.zig " ,
. output = " Zig is cool. " ,
} ,
2021-04-09 13:02:36 -04:00
. {
. main_file = " 060_floats.zig " ,
. output = " Shuttle liftoff weight: 1995796kg " ,
} ,
2021-04-09 14:41:25 -04:00
. {
. main_file = " 061_coercions.zig " ,
. output = " Letter: A " ,
} ,
2021-04-09 19:24:59 -04:00
. {
. main_file = " 062_loop_expressions.zig " ,
. output = " Current language: Zig " ,
2021-04-11 11:19:58 -04:00
. hint = " Surely the current language is 'Zig'! " ,
2021-04-09 19:24:59 -04:00
} ,
2021-04-10 11:39:11 -04:00
. {
. main_file = " 063_labels.zig " ,
. output = " Enjoy your Cheesy Chili! " ,
} ,
2021-04-10 20:26:17 -04:00
. {
. main_file = " 064_builtins.zig " ,
2022-04-21 01:09:21 -04:00
. output = " 1101 + 0101 = 0010 (true). Without overflow: 00010010. Furthermore, 11110000 backwards is 00001111. " ,
2021-04-10 20:26:17 -04:00
} ,
2021-04-11 11:19:58 -04:00
. {
. main_file = " 065_builtins2.zig " ,
. output = " A Narcissus loves all Narcissuses. He has room in his heart for: me myself. " ,
} ,
2021-07-21 11:33:32 -04:00
. {
. 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. " ,
2021-07-21 10:03:44 -04:00
} ,
2021-04-15 20:58:12 -04:00
. {
. main_file = " 067_comptime2.zig " ,
. output = " A BB CCC DDDD " ,
} ,
2021-04-18 18:05:36 -04:00
. {
. main_file = " 068_comptime3.zig " ,
. output = " Minnow (1:32, 4 x 2) \n Shark (1:16, 8 x 5) \n Whale (1:1, 143 x 95) \n " ,
} ,
2021-04-21 09:47:16 -04: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 } " ,
} ,
2021-04-21 20:08:34 -04:00
. {
. main_file = " 070_comptime5.zig " ,
. output = " \" Quack. \" ducky1: true, \" Squeek! \" ducky2: true, ducky3: false " ,
. hint = " Have you kept the wizard hat on? " ,
} ,
2021-04-22 17:17:25 -04:00
. {
. main_file = " 071_comptime6.zig " ,
. output = " Narcissus has room in his heart for: me myself. " ,
} ,
2021-04-24 14:34:46 -04:00
. {
. main_file = " 072_comptime7.zig " ,
. output = " 26 " ,
} ,
2021-04-26 20:01:54 -04:00
. {
. main_file = " 073_comptime8.zig " ,
. output = " My llama value is 25. " ,
} ,
2021-04-30 21:35:56 -04:00
. {
. 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. " ,
} ,
2021-05-06 20:32:36 -04:00
. {
. main_file = " 076_sentinels.zig " ,
2021-05-08 18:51:08 -04:00
. output = " Array:123056. Many-item pointer:123. " ,
} ,
. {
. main_file = " 077_sentinels2.zig " ,
. output = " Weird Data! " ,
} ,
. {
. main_file = " 078_sentinels3.zig " ,
. output = " Weird Data! " ,
2021-05-06 20:32:36 -04:00
} ,
2021-05-09 13:10:09 -04:00
. {
. main_file = " 079_quoted_identifiers.zig " ,
. output = " Sweet freedom: 55, false. " ,
. hint = " Help us, Zig Programmer, you're our only hope! " ,
} ,
2021-05-09 13:24:25 -04:00
. {
. main_file = " 080_anonymous_structs.zig " ,
. output = " [Circle(i32): 25,70,15] [Circle(f32): 25.2,71.0,15.7] " ,
} ,
2021-05-09 14:25:51 -04:00
. {
. main_file = " 081_anonymous_structs2.zig " ,
. output = " x:205 y:187 radius:12 " ,
} ,
2021-05-09 18:58:56 -04:00
. {
. main_file = " 082_anonymous_structs3.zig " ,
. output = " \" 0 \" (bool):true \" 1 \" (bool):false \" 2 \" (i32):42 \" 3 \" (f32):3.14159202e+00 " ,
2021-05-12 20:35:29 -04:00
. hint = " This one is a challenge! But you have everything you need. " ,
2021-05-09 18:58:56 -04:00
} ,
2021-05-09 19:53:14 -04:00
. {
. main_file = " 083_anonymous_lists.zig " ,
. output = " I say hello! " ,
} ,
2023-04-08 06:56:51 -04:00
// Skipped because of https://github.com/ratfactor/ziglings/issues/163
2023-02-12 11:10:40 -05:00
// direct link: https://github.com/ziglang/zig/issues/6025
2023-04-08 06:56:51 -04:00
. {
. 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 ,
} ,
2023-02-16 13:28:10 -05:00
. {
. main_file = " 092_interfaces.zig " ,
. output = " Daily Insect Report: \n Ant is alive. \n Bee visited 17 flowers. \n Grasshopper hopped 32 meters. " ,
} ,
2023-02-15 16:55:44 -05:00
. {
. main_file = " 093_hello_c.zig " ,
2023-02-27 17:22:51 -05:00
. output = " Hello C from Zig! - C result is 17 chars written. " ,
2023-02-18 17:39:21 -05:00
. C = true ,
} ,
. {
. main_file = " 094_c_math.zig " ,
. output = " The normalized angle of 765.2 degrees is 45.2 degrees. " ,
2023-02-15 16:55:44 -05:00
. C = true ,
} ,
2023-03-04 10:07:53 -05:00
. {
. main_file = " 095_for_loops.zig " ,
. output = " 1 2 4 7 8 11 13 14 16 17 19 " ,
} ,
2023-03-08 12:51:07 -05:00
. {
. main_file = " 096_memory_allocation.zig " ,
. output = " Running Average: 0.30 0.25 0.20 0.18 0.22 " ,
} ,
2023-04-12 09:44:12 -04:00
. {
. main_file = " 097_bit_manipulation.zig " ,
. output = " x = 0; y = 1 " ,
} ,
2023-04-12 11:00:26 -04:00
. {
. main_file = " 098_bit_manipulation2.zig " ,
. output = " Is this a pangram? true! " ,
} ,
2023-01-21 17:26:53 -05:00
. {
2023-01-22 07:12:53 -05:00
. main_file = " 999_the_end.zig " ,
2023-02-02 04:38:02 -05:00
. output = " \n This is the end for now! \n We hope you had fun and were able to learn a lot, so visit us again when the next exercises are available. " ,
2023-01-21 17:26:53 -05:00
} ,
2021-02-11 23:04:36 -05:00
} ;
2023-04-03 05:06:39 -04:00
pub fn build ( b : * Build ) ! void {
if ( ! compat . is_compatible ) compat . die ( ) ;
2023-04-09 12:33:59 -04:00
if ( ! validate_exercises ( ) ) std . os . exit ( 1 ) ;
2021-02-11 23:04:36 -05:00
use_color_escapes = false ;
2023-03-18 19:59:42 -04: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-11 23:04:36 -05:00
}
if ( use_color_escapes ) {
red_text = " \x1b [31m " ;
green_text = " \x1b [32m " ;
bold_text = " \x1b [1m " ;
reset_text = " \x1b [0m " ;
}
2023-03-18 19:59:42 -04:00
const logo =
2021-02-11 23:04:36 -05:00
\\
\\ _ _ _
\\ ___(_) __ _| (_)_ __ __ _ ___
\\ |_ | |/ _' | | | '_ \ / _' / __|
2021-03-02 23:06:09 -05:00
\\ / /| | (_| | | | | | | (_| \__ \
2021-02-11 23:04:36 -05:00
\\ /___|_|\__, |_|_|_| |_|\__, |___/
\\ |___/ |___/
\\
\\
2023-03-18 19:59:42 -04:00
;
2023-04-02 08:59:22 -04: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 06:47:08 -04:00
const use_healed = b . option ( bool , " healed " , " Run exercises from patches/healed " ) orelse false ;
const exno : ? usize = b . option ( usize , " n " , " Select exercise " ) ;
2021-02-11 23:04:36 -05:00
2023-04-10 15:24:49 -04:00
const header_step = PrintStep . create ( b , logo ) ;
2021-02-11 23:04:36 -05:00
2023-04-10 12:14:58 -04: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 06:47:08 -04:00
std . os . exit ( 1 ) ;
2023-04-09 12:33:59 -04:00
}
2021-02-14 18:36:09 -05:00
2023-04-10 12:14:58 -04:00
const ex = exercises [ n - 1 ] ;
2023-03-19 13:23:35 -04:00
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 ;
2023-02-01 22:51:47 -05:00
2023-04-05 05:16:23 -04:00
const build_step = b . addExecutable ( . { . name = base_name , . root_source_file = . { . path = file_path } } ) ;
2023-04-08 03:51:07 -04:00
if ( ex . C ) {
build_step . linkLibC ( ) ;
}
2023-04-12 12:41:40 -04:00
b . installArtifact ( build_step ) ;
2021-02-11 23:04:36 -05:00
2023-04-12 12:41:40 -04: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 06:47:08 -04:00
const test_step = b . step ( " test " , b . fmt ( " Run {s} without checking output " , . { ex . main_file } ) ) ;
2023-04-08 06:56:51 -04:00
if ( ex . skip ) {
const skip_step = SkipStep . create ( b , ex ) ;
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 06:47:08 -04:00
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 ( ) ) ;
2023-03-19 13:23:35 -04:00
const verify_step = ZiglingStep . create ( b , ex , use_healed ) ;
2021-02-11 23:04:36 -05: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 06:47:08 -04: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-11 23:04:36 -05: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 06:47:08 -04:00
const start_step = b . step ( " start " , b . fmt ( " Check all solutions starting at {s} " , . { ex . main_file } ) ) ;
2021-02-11 23:04:36 -05: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 06:47:08 -04:00
var prev_step = verify_step ;
for ( exercises ) | exn | {
2023-04-10 12:14:58 -04:00
const nth = exn . number ( ) ;
if ( nth > 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 06:47:08 -04:00
const verify_stepn = ZiglingStep . create ( b , exn , use_healed ) ;
verify_stepn . step . dependOn ( & prev_step . step ) ;
2021-02-11 23:04:36 -05: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 06:47:08 -04:00
prev_step = verify_stepn ;
}
}
start_step . dependOn ( & prev_step . step ) ;
2023-04-08 03:02:38 -04:00
return ;
2023-04-13 11:47:17 -04:00
} else if ( use_healed and false ) {
2023-04-08 03:02:38 -04:00
const test_step = b . step ( " test " , " Test the healed exercises " ) ;
b . default_step = test_step ;
for ( exercises ) | ex | {
const base_name = ex . baseName ( ) ;
const file_path = std . fs . path . join ( b . allocator , & [ _ ] [ ] const u8 {
" patches/healed " , ex . main_file ,
} ) catch unreachable ;
const build_step = b . addExecutable ( . { . name = base_name , . root_source_file = . { . path = file_path } } ) ;
if ( ex . C ) {
build_step . linkLibC ( ) ;
}
2023-04-12 12:41:40 -04:00
b . installArtifact ( build_step ) ;
2023-04-08 03:02:38 -04:00
2023-04-12 12:41:40 -04:00
const run_step = b . addRunArtifact ( build_step ) ;
2023-04-08 06:56:51 -04: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 03:02:38 -04: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 06:47:08 -04:00
return ;
}
const ziglings_step = b . step ( " ziglings " , " Check all ziglings " ) ;
b . default_step = ziglings_step ;
2023-04-10 12:14:58 -04:00
// Don't use the "multi-object for loop" syntax, in order to avoid a syntax
// error with old Zig compilers.
2023-04-10 15:02:54 -04:00
var prev_step = & header_step . step ;
2023-04-10 12:14:58 -04:00
for ( exercises ) | ex | {
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 06:47:08 -04:00
const base_name = ex . baseName ( ) ;
const file_path = std . fs . path . join ( b . allocator , & [ _ ] [ ] const u8 {
2023-04-08 03:02:38 -04:00
" exercises " , ex . main_file ,
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 06:47:08 -04:00
} ) catch unreachable ;
2021-02-11 23:04:36 -05: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 06:47:08 -04:00
const build_step = b . addExecutable ( . { . name = base_name , . root_source_file = . { . path = file_path } } ) ;
2023-04-12 12:41:40 -04:00
b . installArtifact ( build_step ) ;
2021-02-11 23:04:36 -05: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 06:47:08 -04:00
const verify_stepn = ZiglingStep . create ( b , ex , use_healed ) ;
2023-04-10 15:02:54 -04:00
verify_stepn . step . dependOn ( prev_step ) ;
2021-02-11 23:04:36 -05:00
2023-04-10 15:02:54 -04:00
prev_step = & verify_stepn . step ;
2023-03-19 13:23:35 -04: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 06:47:08 -04:00
ziglings_step . dependOn ( prev_step ) ;
2023-04-09 12:33:59 -04:00
const test_step = b . step ( " test " , " Run all the tests " ) ;
test_step . dependOn ( tests . addCliTests ( b , & exercises ) ) ;
2021-02-11 23:04:36 -05: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-03 05:06:39 -04:00
builder : * Build ,
2021-02-14 18:36:09 -05:00
use_healed : bool ,
2021-02-11 23:04:36 -05:00
2023-04-03 05:06:39 -04:00
pub fn create ( builder : * Build , exercise : Exercise , use_healed : bool ) * @This ( ) {
2021-02-11 23:04:36 -05:00
const self = builder . allocator . create ( @This ( ) ) catch unreachable ;
self . * = . {
2023-03-18 19:59:42 -04:00
. step = Step . init ( Step . Options { . id = . custom , . name = exercise . main_file , . owner = builder , . makeFn = make } ) ,
2021-02-11 23:04:36 -05:00
. exercise = exercise ,
. builder = builder ,
2021-02-14 18:36:09 -05:00
. use_healed = use_healed ,
2021-02-11 23:04:36 -05:00
} ;
return self ;
}
2023-03-18 19:59:42 -04:00
fn make ( step : * Step , prog_node : * std . Progress . Node ) anyerror ! void {
2021-02-11 23:04:36 -05:00
const self = @fieldParentPtr ( @This ( ) , " step " , step ) ;
2023-04-08 06:56:51 -04:00
if ( self . exercise . skip ) {
print ( " Skipping {s} \n \n " , . { self . exercise . main_file } ) ;
return ;
}
2023-04-13 12:03:00 -04:00
self . makeInternal ( prog_node ) catch {
2021-02-11 23:04:36 -05:00
if ( self . exercise . hint . len > 0 ) {
2021-02-14 16:42:42 -05:00
print ( " \n {s}HINT: {s}{s} " , . { bold_text , self . exercise . hint , reset_text } ) ;
2021-02-11 23:04:36 -05:00
}
print ( " \n {s}Edit exercises/{s} and run this again.{s} " , . { red_text , self . exercise . main_file , reset_text } ) ;
2023-04-07 12:06:13 -04:00
print ( " \n {s}To continue from this zigling, use this command:{s} \n {s}zig build -Dn={s}{s} \n " , . { red_text , reset_text , bold_text , self . exercise . key ( ) , reset_text } ) ;
2022-11-16 07:05:25 -05:00
std . os . exit ( 1 ) ;
2021-02-11 23:04:36 -05:00
} ;
}
2023-04-13 12:03:00 -04:00
fn makeInternal ( self : * @This ( ) , prog_node : * std . Progress . Node ) ! void {
2021-02-11 23:04:36 -05:00
print ( " Compiling {s}... \n " , . { self . exercise . main_file } ) ;
2023-04-13 12:03:00 -04:00
const exe_file = try self . doCompile ( prog_node ) ;
2021-02-11 23:04:36 -05:00
2021-02-14 16:42:42 -05:00
print ( " Checking {s}... \n " , . { self . exercise . main_file } ) ;
2021-02-11 23:04:36 -05:00
2023-02-15 13:28:27 -05:00
const cwd = self . builder . build_root . path . ? ;
2021-02-11 23:04:36 -05:00
const argv = [ _ ] [ ] const u8 { exe_file } ;
2022-05-01 20:19:37 -04:00
var child = std . ChildProcess . init ( & argv , self . builder . allocator ) ;
2021-02-11 23:04:36 -05:00
child . cwd = cwd ;
child . env_map = self . builder . env_map ;
child . stdin_behavior = . Inherit ;
if ( self . exercise . check_stdout ) {
child . stdout_behavior = . Pipe ;
child . stderr_behavior = . Inherit ;
} else {
child . stdout_behavior = . Inherit ;
child . stderr_behavior = . Pipe ;
}
child . spawn ( ) catch | err | {
print ( " {s}Unable to spawn {s}: {s}{s} \n " , . { red_text , argv [ 0 ] , @errorName ( err ) , reset_text } ) ;
return err ;
} ;
// Allow up to 1 MB of stdout capture
const max_output_len = 1 * 1024 * 1024 ;
const output = if ( self . exercise . check_stdout )
try child . stdout . ? . reader ( ) . readAllAlloc ( self . builder . allocator , max_output_len )
else
try child . stderr . ? . reader ( ) . readAllAlloc ( self . builder . allocator , max_output_len ) ;
// at this point stdout is closed, wait for the process to terminate
const term = child . wait ( ) catch | err | {
print ( " {s}Unable to spawn {s}: {s}{s} \n " , . { red_text , argv [ 0 ] , @errorName ( err ) , reset_text } ) ;
return err ;
} ;
// make sure it exited cleanly.
switch ( term ) {
. Exited = > | code | {
if ( code ! = 0 ) {
print ( " {s}{s} exited with error code {d} (expected {d}){s} \n " , . { red_text , self . exercise . main_file , code , 0 , reset_text } ) ;
return error . BadExitCode ;
}
} ,
else = > {
print ( " {s}{s} terminated unexpectedly{s} \n " , . { red_text , self . exercise . main_file , reset_text } ) ;
return error . UnexpectedTermination ;
} ,
}
// validate the output
2023-02-02 04:38:02 -05:00
const trimOutput = std . mem . trimRight ( u8 , output , " \r \n " ) ;
const trimExerciseOutput = std . mem . trimRight ( u8 , self . exercise . output , " \r \n " ) ;
2022-11-16 07:00:36 -05:00
if ( std . mem . indexOf ( u8 , trimOutput , trimExerciseOutput ) = = null or trimOutput . len ! = trimExerciseOutput . len ) {
2021-02-11 23:04:36 -05:00
print (
\\
\\{s}----------- Expected this output -----------{s}
2022-11-16 07:00:36 -05:00
\\"{s}"
2021-02-11 23:04:36 -05:00
\\{s}----------- but found -----------{s}
2022-11-16 07:00:36 -05:00
\\"{s}"
2021-02-11 23:04:36 -05:00
\\{s}-----------{s}
\\
2022-11-16 07:00:36 -05:00
, . { red_text , reset_text , trimExerciseOutput , red_text , reset_text , trimOutput , red_text , reset_text } ) ;
2021-02-11 23:04:36 -05:00
return error . InvalidOutput ;
}
2021-07-21 11:30:51 -04:00
print ( " {s}PASSED: \n {s}{s} \n " , . { green_text , output , reset_text } ) ;
2021-02-11 23:04:36 -05:00
}
// The normal compile step calls os.exit, so we can't use it as a library :(
// This is a stripped down copy of std.build.LibExeObjStep.make.
2023-04-13 12:03:00 -04:00
fn doCompile ( self : * @This ( ) , prog_node : * std . Progress . Node ) ! [ ] const u8 {
2021-02-11 23:04:36 -05:00
const builder = self . builder ;
var zig_args = std . ArrayList ( [ ] const u8 ) . init ( builder . allocator ) ;
defer zig_args . deinit ( ) ;
zig_args . append ( builder . zig_exe ) catch unreachable ;
zig_args . append ( " build-exe " ) catch unreachable ;
2022-08-29 03:17:42 -04:00
// Enable the stage 1 compiler if using the async feature
2023-01-21 07:57:32 -05:00
// disabled because of https://github.com/ratfactor/ziglings/issues/163
// if (self.exercise.@"async") {
// zig_args.append("-fstage1") catch unreachable;
// }
2022-08-29 03:17:42 -04:00
2023-02-15 16:55:44 -05:00
// Enable C support for exercises that use C functions
if ( self . exercise . C ) {
zig_args . append ( " -lc " ) catch unreachable ;
}
2021-03-12 18:59:46 -05:00
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 ;
2021-02-11 23:04:36 -05:00
zig_args . append ( builder . pathFromRoot ( zig_file ) ) catch unreachable ;
zig_args . append ( " --cache-dir " ) catch unreachable ;
2023-02-15 13:28:27 -05:00
zig_args . append ( builder . pathFromRoot ( builder . cache_root . path . ? ) ) catch unreachable ;
2021-02-11 23:04:36 -05:00
2023-04-13 12:03:00 -04:00
zig_args . append ( " --listen=- " ) catch unreachable ;
2021-02-11 23:04:36 -05:00
const argv = zig_args . items ;
var code : u8 = undefined ;
2023-04-13 12:03:00 -04:00
const file_name = self . eval ( argv , & code , prog_node ) catch | err | {
2021-02-11 23:04:36 -05:00
switch ( err ) {
error . FileNotFound = > {
print ( " {s}{s}: Unable to spawn the following command: file not found{s} \n " , . { red_text , self . exercise . main_file , reset_text } ) ;
for ( argv ) | v | print ( " {s} " , . { v } ) ;
print ( " \n " , . { } ) ;
} ,
error . ExitCodeFailure = > {
print ( " {s}{s}: The following command exited with error code {}:{s} \n " , . { red_text , self . exercise . main_file , code , reset_text } ) ;
for ( argv ) | v | print ( " {s} " , . { v } ) ;
print ( " \n " , . { } ) ;
} ,
error . ProcessTerminated = > {
print ( " {s}{s}: The following command terminated unexpectedly:{s} \n " , . { red_text , self . exercise . main_file , reset_text } ) ;
for ( argv ) | v | print ( " {s} " , . { v } ) ;
print ( " \n " , . { } ) ;
} ,
else = > { } ,
}
2023-04-13 12:03:00 -04:00
2021-02-11 23:04:36 -05:00
return err ;
} ;
2023-04-13 12:03:00 -04:00
return file_name ;
}
// Code adapted from `std.Build.execAllowFail and `std.Build.Step.evalZigProcess`.
pub fn eval (
self : * ZiglingStep ,
argv : [ ] const [ ] const u8 ,
out_code : * u8 ,
prog_node : * std . Progress . Node ,
) ! [ ] const u8 {
assert ( argv . len ! = 0 ) ;
const b = self . step . owner ;
const allocator = b . allocator ;
var child = Child . init ( argv , allocator ) ;
child . env_map = b . env_map ;
child . stdin_behavior = . Pipe ;
child . stdout_behavior = . Pipe ;
child . stderr_behavior = . Pipe ;
try child . spawn ( ) ;
var poller = std . io . poll ( allocator , enum { stdout , stderr } , . {
. stdout = child . stdout . ? ,
. stderr = child . stderr . ? ,
2021-02-11 23:04:36 -05:00
} ) ;
2023-04-13 12:03:00 -04:00
defer poller . deinit ( ) ;
try sendMessage ( child . stdin . ? , . update ) ;
try sendMessage ( child . stdin . ? , . exit ) ;
const Header = std . zig . Server . Message . Header ;
var result : ? [ ] const u8 = null ;
var node_name : std . ArrayListUnmanaged ( u8 ) = . { } ;
defer node_name . deinit ( allocator ) ;
var sub_prog_node = prog_node . start ( " " , 0 ) ;
defer sub_prog_node . end ( ) ;
const stdout = poller . fifo ( . stdout ) ;
poll : while ( true ) {
while ( stdout . readableLength ( ) < @sizeOf ( Header ) ) {
if ( ! ( try poller . poll ( ) ) ) break : poll ;
}
const header = stdout . reader ( ) . readStruct ( Header ) catch unreachable ;
while ( stdout . readableLength ( ) < header . bytes_len ) {
if ( ! ( try poller . poll ( ) ) ) break : poll ;
}
const body = stdout . readableSliceOfLen ( header . bytes_len ) ;
switch ( header . tag ) {
. zig_version = > {
if ( ! std . mem . eql ( u8 , builtin . zig_version_string , body ) )
return error . ZigVersionMismatch ;
} ,
. error_bundle = > {
const EbHdr = std . zig . Server . Message . ErrorBundle ;
const eb_hdr = @ptrCast ( * align ( 1 ) const EbHdr , body ) ;
const extra_bytes =
body [ @sizeOf ( EbHdr ) . . ] [ 0 . . @sizeOf ( u32 ) * eb_hdr . extra_len ] ;
const string_bytes =
body [ @sizeOf ( EbHdr ) + extra_bytes . len . . ] [ 0 . . eb_hdr . string_bytes_len ] ;
// TODO: use @ptrCast when the compiler supports it
const unaligned_extra = std . mem . bytesAsSlice ( u32 , extra_bytes ) ;
const extra_array = try allocator . alloc ( u32 , unaligned_extra . len ) ;
// TODO: use @memcpy when it supports slices
for ( extra_array , unaligned_extra ) | * dst , src | dst . * = src ;
const error_bundle : std . zig . ErrorBundle = . {
. string_bytes = try allocator . dupe ( u8 , string_bytes ) ,
. extra = extra_array ,
} ;
// Print the compiler error bundle now.
// TODO: use the same ttyconf from the builder.
const ttyconf : std . debug . TTY . Config = if ( use_color_escapes )
. escape_codes
else
. no_color ;
error_bundle . renderToStdErr (
. { . ttyconf = ttyconf } ,
) ;
} ,
. progress = > {
node_name . clearRetainingCapacity ( ) ;
try node_name . appendSlice ( allocator , body ) ;
sub_prog_node . setName ( node_name . items ) ;
} ,
. emit_bin_path = > {
const EbpHdr = std . zig . Server . Message . EmitBinPath ;
// TODO: add cache support?
//const ebp_hdr = @ptrCast(*align(1) const EbpHdr, body);
//s.result_cached = ebp_hdr.flags.cache_hit;
result = try allocator . dupe ( u8 , body [ @sizeOf ( EbpHdr ) . . ] ) ;
} ,
else = > { } , // ignore other messages
}
stdout . discard ( body . len ) ;
}
const stderr = poller . fifo ( . stderr ) ;
if ( stderr . readableLength ( ) > 0 ) {
// Print the additional log and verbose messages now.
const messages = try stderr . toOwnedSlice ( ) ;
print ( " {s} \n " , . { messages } ) ;
}
// 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-11 23:04:36 -05:00
}
} ;
2023-04-02 08:59:22 -04:00
2023-04-13 12:03:00 -04:00
fn sendMessage ( file : std . fs . File , tag : std . zig . Client . Message . Tag ) ! void {
const header : std . zig . Client . Message . Header = . {
. tag = tag ,
. bytes_len = 0 ,
} ;
try file . writeAll ( std . mem . asBytes ( & header ) ) ;
}
2023-04-10 15:24:49 -04:00
// Print a message to stderr.
2023-04-02 08:59:22 -04:00
const PrintStep = struct {
step : Step ,
message : [ ] const u8 ,
2023-04-10 15:24:49 -04:00
pub fn create ( owner : * Build , message : [ ] const u8 ) * PrintStep {
2023-04-02 08:59:22 -04:00
const self = owner . allocator . create ( PrintStep ) catch @panic ( " OOM " ) ;
self . * = . {
. step = Step . init ( . {
. id = . custom ,
2023-04-09 12:48:19 -04:00
. name = " print " ,
2023-04-02 08:59:22 -04: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 15:24:49 -04:00
print ( " {s} " , . { p . message } ) ;
2023-04-02 08:59:22 -04:00
}
} ;
2023-04-08 06:56:51 -04: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 12:48:19 -04:00
. name = owner . fmt ( " skip {s} " , . { exercise . main_file } ) ,
2023-04-08 06:56:51 -04:00
. owner = owner ,
. makeFn = make ,
} ) ,
. exercise = exercise ,
} ;
return self ;
}
fn make ( step : * Step , prog_node : * std . Progress . Node ) ! void {
_ = prog_node ;
const p = @fieldParentPtr ( SkipStep , " step " , step ) ;
if ( p . exercise . skip ) {
print ( " {s} skipped \n " , . { p . exercise . main_file } ) ;
}
}
} ;
2023-04-09 12:33:59 -04:00
// Check that each exercise number, excluding the last, forms the sequence `[1, exercise.len)`.
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 ;
}
}
return true ;
}