Skip to content

Commit d618414

Browse files
committed
Added Ex. 2, polished script, added LICENSE
1 parent 3b56788 commit d618414

File tree

5 files changed

+99
-91
lines changed

5 files changed

+99
-91
lines changed

01_hello.zig

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,16 @@
1-
// Oh no! This program is supposed to print "Hello world!" but it has some
2-
// mistakes. Let's fix it.
31
//
4-
// We're trying to import the Standard Library into the top level of our
5-
// application. The standard library is not named "foo", it is named "std".
2+
// Oh no! This program is supposed to print "Hello world!" but it needs
3+
// your help!
64
//
7-
// Please correct the name in both places in the import here:
8-
const foo = @import("foo");
9-
10-
// Zig applications start by calling a function named 'main'. It needs to be
11-
// public so that it is accessible outside our file!
12-
//
13-
// Public functions are declared with the 'pub' keyword like so:
5+
// Hint: Zig functions are private by default.
6+
// The main() function should be public.
7+
// Declare a public function with "pub fn ..."
148
//
15-
// pub fn my_function() void { ... }
9+
// Try to fix the program and run `ziglings` to see if it passes.
1610
//
17-
// Please make the main() function public:
18-
fn main() void {
19-
20-
// The easiest way to display our "Hello world" message in the
21-
// terminal is to use the std.debug.print() function.
22-
23-
// Please fix this silly "foo" mistake here:
24-
foo.debug.print("Hello world!\n", .{});
11+
const std = @import("std");
2512

26-
// The print function above takes two values:
27-
//
28-
// 1. A string of characters: "Hello world!\n". "\n" prints a new line.
29-
//
30-
// 2. A struct containing data to be displayed. .{} is an empty, nameless
31-
// struct fulfilling the requirement. More about structs later.
32-
//
33-
//
34-
// Now we're ready to...What's this!? Oh, we wanted to print a Goodbye
35-
// message as well!
36-
//
37-
// Please fix this to use the same print function as above:
38-
"Goodbye!\n"
13+
fn main() void {
14+
std.debug.print("Hello world!\n", .{});
3915
}
4016

41-
// Once you're done with the changes above, run `ziglings` to see if it passes.
42-
//
43-
// Finally, all files will contain the following comment:
44-
//
45-
// I AM NOT DONE
46-
//
47-
// Delete it when you're ready to continue to the next exercise!

02_std.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Oops! This program is supposed to print a line like our Hello World
3+
// example. But we forgot how to import the Zig Standard Library.
4+
//
5+
// Hint 1: The @import() built-in function returns a value representing
6+
// imported code. We need to give that value a name to use it.
7+
// Hint 2: We use the name "std" in the main function (see below).
8+
// Hint 3: Imports need to be named by declaring them as "const" values.
9+
// Hint 4: Take a look at how the previous exercise did this!
10+
//
11+
@import("std");
12+
13+
pub fn main() void {
14+
std.debug.print("Standard Library.\n", .{});
15+
}
16+
17+
// Going deeper: imports must be declared as "constants" (with the 'const'
18+
// keyword rather than "variables" (with the 'var' keyword) is that they
19+
// can only be used at "compile time" rather than "run time". Zig evaluates
20+
// const values at compile time. Don't worry if none of this makes sense
21+
// yet! See also this answer: https://stackoverflow.com/a/62567550/695615

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Dave Gauer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,12 @@ Then run the `ziglings` script and follow the instructions to begin!
3939
## Manual Usage
4040

4141
If you can't (or don't want to) use the script, you can manually verify each
42-
exercise with the Zig compiler.
43-
44-
Some exercises need to be "run" (compiled and executed):
42+
exercise with the Zig compiler:
4543

4644
```bash
4745
zig run exercises/01_hello.zig
4846
```
4947

50-
Some exercises need to be tested:
51-
52-
```bash
53-
zig test exercises/02_hello_test.zig
54-
```
55-
5648
## TODO
5749

5850
Contributions are very welcome! I'm writing this to teach myself and to create
@@ -65,8 +57,8 @@ the learning resource I wished for. There will be tons of room for improvement:
6557

6658
Planned exercises:
6759

68-
* [x] Hello world
69-
* [ ] Hello tests
60+
* [x] Hello world (main needs to be public)
61+
* [x] Importing standard library
7062
* [ ] Assignment
7163
* [ ] Arrays
7264
* [ ] If

ziglings

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/bin/bash
22

3-
# Minimum viable working example!
4-
53
echo
64
echo " _ _ _ "
75
echo " ___(_) __ _| (_)_ __ __ _ ___ "
@@ -16,48 +14,55 @@ fmt_err=$( tput setaf 1 ) # red foreground
1614
fmt_yay=$( tput setaf 2 ) # green foreground
1715
fmt_off=$( tput sgr0 ) # reset colors/effects
1816

17+
function check_it {
18+
source_file=$1
19+
correct_output=$2
20+
hint=$3
1921

22+
# Compile/run the source and capture the result and exit value
23+
cmd="zig run $source_file"
24+
echo "$ $cmd"
25+
result=$($cmd 2>&1)
26+
result_status=$?
2027

21-
# TODO: most of this belongs in a generalized function
22-
if grep -q "I AM NOT DONE" 01_hello.zig
23-
then
28+
# Echo the result to the screen so user can see what their program does
29+
echo "$result"
30+
if [[ $result_status -ne 0 ]]
31+
then
32+
echo
33+
printf "${fmt_err}Uh oh! Looks like there was an error.${fmt_off}\n"
34+
if [[ ! -z "$hint" ]]
35+
then
36+
echo "$hint"
37+
fi
38+
echo
39+
echo "Edit '$source_file' and run me again."
40+
echo
41+
exit 1
42+
fi
2443

25-
echo
26-
echo "* Exercise: Hello world *"
27-
28-
result=$(zig run 01_hello.zig 2>&1)
29-
result_status=$?
30-
echo =========================================================================
31-
echo "$result"
32-
echo =========================================================================
33-
if [[ $result_status -eq 0 ]]
34-
then
35-
printf "${fmt_yay}Zig was able to compile your submission.${fmt_off}\n"
36-
else
37-
printf "${fmt_err}Uh oh! Looks like there was an error.${fmt_off}\n"
38-
exit
39-
fi
40-
41-
if [[ $result == *Hello*Goodbye* ]]
42-
then
43-
printf "${fmt_yay}Excellent! I see that you're printing both Hello and Goodbye!${fmt_off}\n"
44-
else
45-
printf "${fmt_err}It seems to compile, but...${fmt_off}\n"
46-
exit
47-
fi
48-
49-
echo "Now you're ready to move on!"
50-
echo "Delete the line I AM NOT DONE from the source file when you're ready"
51-
echo "to continue."
52-
53-
exit
54-
55-
else # end of exercise one - I AM NOT DONE is removed!
56-
printf "${fmt_yay}DONE - Hello world${fmt_off}\n"
57-
fi
44+
# Wildcards to be lenient with anything AROUND the correct output
45+
if [[ "$result" == *$correct_output* ]]
46+
then
47+
printf "${fmt_yay}** PASSED **${fmt_off}\n"
48+
else
49+
printf "${fmt_err}It seems to compile, but I wanted to see '$correct_output'.${fmt_off}\n"
50+
echo
51+
exit 1
52+
fi
53+
}
5854

5955

56+
check_it 01_hello.zig "Hello world" "Note the error: the source file has a hint for fixing 'main'."
57+
check_it 02_std.zig "Standard Library"
58+
59+
echo
60+
echo " __ __ _ "
61+
echo " \ \ / __ _ _ _| | "
62+
echo " \ V / _' | | | | | "
63+
echo " | | (_| | |_| |_| "
64+
echo " |_|\__,_|\__, (_) "
65+
echo " |___/ "
6066
echo
61-
echo "* Exercise: Hello test *"
67+
echo "You've completed all of the Ziglings exercises!"
6268
echo
63-
echo "TODO: this and other exercises :-)"

0 commit comments

Comments
 (0)