mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-21 21:53:11 -05:00
Added Ex. 2, polished script, added LICENSE
This commit is contained in:
parent
3b5678815f
commit
d618414c9c
5 changed files with 101 additions and 93 deletions
51
01_hello.zig
51
01_hello.zig
|
@ -1,47 +1,16 @@
|
|||
// Oh no! This program is supposed to print "Hello world!" but it has some
|
||||
// mistakes. Let's fix it.
|
||||
//
|
||||
// We're trying to import the Standard Library into the top level of our
|
||||
// application. The standard library is not named "foo", it is named "std".
|
||||
// Oh no! This program is supposed to print "Hello world!" but it needs
|
||||
// your help!
|
||||
//
|
||||
// Please correct the name in both places in the import here:
|
||||
const foo = @import("foo");
|
||||
// Hint: Zig functions are private by default.
|
||||
// The main() function should be public.
|
||||
// Declare a public function with "pub fn ..."
|
||||
//
|
||||
// Try to fix the program and run `ziglings` to see if it passes.
|
||||
//
|
||||
const std = @import("std");
|
||||
|
||||
// Zig applications start by calling a function named 'main'. It needs to be
|
||||
// public so that it is accessible outside our file!
|
||||
//
|
||||
// Public functions are declared with the 'pub' keyword like so:
|
||||
//
|
||||
// pub fn my_function() void { ... }
|
||||
//
|
||||
// Please make the main() function public:
|
||||
fn main() void {
|
||||
|
||||
// The easiest way to display our "Hello world" message in the
|
||||
// terminal is to use the std.debug.print() function.
|
||||
|
||||
// Please fix this silly "foo" mistake here:
|
||||
foo.debug.print("Hello world!\n", .{});
|
||||
|
||||
// The print function above takes two values:
|
||||
//
|
||||
// 1. A string of characters: "Hello world!\n". "\n" prints a new line.
|
||||
//
|
||||
// 2. A struct containing data to be displayed. .{} is an empty, nameless
|
||||
// struct fulfilling the requirement. More about structs later.
|
||||
//
|
||||
//
|
||||
// Now we're ready to...What's this!? Oh, we wanted to print a Goodbye
|
||||
// message as well!
|
||||
//
|
||||
// Please fix this to use the same print function as above:
|
||||
"Goodbye!\n"
|
||||
std.debug.print("Hello world!\n", .{});
|
||||
}
|
||||
|
||||
// Once you're done with the changes above, run `ziglings` to see if it passes.
|
||||
//
|
||||
// Finally, all files will contain the following comment:
|
||||
//
|
||||
// I AM NOT DONE
|
||||
//
|
||||
// Delete it when you're ready to continue to the next exercise!
|
||||
|
|
21
02_std.zig
Normal file
21
02_std.zig
Normal file
|
@ -0,0 +1,21 @@
|
|||
//
|
||||
// Oops! This program is supposed to print a line like our Hello World
|
||||
// example. But we forgot how to import the Zig Standard Library.
|
||||
//
|
||||
// Hint 1: The @import() built-in function returns a value representing
|
||||
// imported code. We need to give that value a name to use it.
|
||||
// Hint 2: We use the name "std" in the main function (see below).
|
||||
// Hint 3: Imports need to be named by declaring them as "const" values.
|
||||
// Hint 4: Take a look at how the previous exercise did this!
|
||||
//
|
||||
@import("std");
|
||||
|
||||
pub fn main() void {
|
||||
std.debug.print("Standard Library.\n", .{});
|
||||
}
|
||||
|
||||
// Going deeper: imports must be declared as "constants" (with the 'const'
|
||||
// keyword rather than "variables" (with the 'var' keyword) is that they
|
||||
// can only be used at "compile time" rather than "run time". Zig evaluates
|
||||
// const values at compile time. Don't worry if none of this makes sense
|
||||
// yet! See also this answer: https://stackoverflow.com/a/62567550/695615
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2021 Dave Gauer
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
14
README.md
14
README.md
|
@ -39,20 +39,12 @@ Then run the `ziglings` script and follow the instructions to begin!
|
|||
## Manual Usage
|
||||
|
||||
If you can't (or don't want to) use the script, you can manually verify each
|
||||
exercise with the Zig compiler.
|
||||
|
||||
Some exercises need to be "run" (compiled and executed):
|
||||
exercise with the Zig compiler:
|
||||
|
||||
```bash
|
||||
zig run exercises/01_hello.zig
|
||||
```
|
||||
|
||||
Some exercises need to be tested:
|
||||
|
||||
```bash
|
||||
zig test exercises/02_hello_test.zig
|
||||
```
|
||||
|
||||
## TODO
|
||||
|
||||
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:
|
|||
|
||||
Planned exercises:
|
||||
|
||||
* [x] Hello world
|
||||
* [ ] Hello tests
|
||||
* [x] Hello world (main needs to be public)
|
||||
* [x] Importing standard library
|
||||
* [ ] Assignment
|
||||
* [ ] Arrays
|
||||
* [ ] If
|
||||
|
|
87
ziglings
87
ziglings
|
@ -1,7 +1,5 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Minimum viable working example!
|
||||
|
||||
echo
|
||||
echo " _ _ _ "
|
||||
echo " ___(_) __ _| (_)_ __ __ _ ___ "
|
||||
|
@ -16,48 +14,55 @@ fmt_err=$( tput setaf 1 ) # red foreground
|
|||
fmt_yay=$( tput setaf 2 ) # green foreground
|
||||
fmt_off=$( tput sgr0 ) # reset colors/effects
|
||||
|
||||
function check_it {
|
||||
source_file=$1
|
||||
correct_output=$2
|
||||
hint=$3
|
||||
|
||||
# Compile/run the source and capture the result and exit value
|
||||
cmd="zig run $source_file"
|
||||
echo "$ $cmd"
|
||||
result=$($cmd 2>&1)
|
||||
result_status=$?
|
||||
|
||||
# Echo the result to the screen so user can see what their program does
|
||||
echo "$result"
|
||||
if [[ $result_status -ne 0 ]]
|
||||
then
|
||||
echo
|
||||
printf "${fmt_err}Uh oh! Looks like there was an error.${fmt_off}\n"
|
||||
if [[ ! -z "$hint" ]]
|
||||
then
|
||||
echo "$hint"
|
||||
fi
|
||||
echo
|
||||
echo "Edit '$source_file' and run me again."
|
||||
echo
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Wildcards to be lenient with anything AROUND the correct output
|
||||
if [[ "$result" == *$correct_output* ]]
|
||||
then
|
||||
printf "${fmt_yay}** PASSED **${fmt_off}\n"
|
||||
else
|
||||
printf "${fmt_err}It seems to compile, but I wanted to see '$correct_output'.${fmt_off}\n"
|
||||
echo
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# TODO: most of this belongs in a generalized function
|
||||
if grep -q "I AM NOT DONE" 01_hello.zig
|
||||
then
|
||||
check_it 01_hello.zig "Hello world" "Note the error: the source file has a hint for fixing 'main'."
|
||||
check_it 02_std.zig "Standard Library"
|
||||
|
||||
echo
|
||||
echo "* Exercise: Hello world *"
|
||||
|
||||
result=$(zig run 01_hello.zig 2>&1)
|
||||
result_status=$?
|
||||
echo =========================================================================
|
||||
echo "$result"
|
||||
echo =========================================================================
|
||||
if [[ $result_status -eq 0 ]]
|
||||
then
|
||||
printf "${fmt_yay}Zig was able to compile your submission.${fmt_off}\n"
|
||||
else
|
||||
printf "${fmt_err}Uh oh! Looks like there was an error.${fmt_off}\n"
|
||||
exit
|
||||
fi
|
||||
|
||||
if [[ $result == *Hello*Goodbye* ]]
|
||||
then
|
||||
printf "${fmt_yay}Excellent! I see that you're printing both Hello and Goodbye!${fmt_off}\n"
|
||||
else
|
||||
printf "${fmt_err}It seems to compile, but...${fmt_off}\n"
|
||||
exit
|
||||
fi
|
||||
|
||||
echo "Now you're ready to move on!"
|
||||
echo "Delete the line I AM NOT DONE from the source file when you're ready"
|
||||
echo "to continue."
|
||||
|
||||
exit
|
||||
|
||||
else # end of exercise one - I AM NOT DONE is removed!
|
||||
printf "${fmt_yay}DONE - Hello world${fmt_off}\n"
|
||||
fi
|
||||
|
||||
|
||||
echo " __ __ _ "
|
||||
echo " \ \ / __ _ _ _| | "
|
||||
echo " \ V / _' | | | | | "
|
||||
echo " | | (_| | |_| |_| "
|
||||
echo " |_|\__,_|\__, (_) "
|
||||
echo " |___/ "
|
||||
echo
|
||||
echo "* Exercise: Hello test *"
|
||||
echo "You've completed all of the Ziglings exercises!"
|
||||
echo
|
||||
echo "TODO: this and other exercises :-)"
|
||||
|
|
Loading…
Reference in a new issue