aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-01-03 12:21:11 -0500
committerDave Gauer <dave@ratfactor.com>2021-01-03 12:21:11 -0500
commitd618414c9cd144e3d63f2b12df7b512b98df041c (patch)
tree2b62be68efd03c9454eab640966abf0dc0d21793
parent3b5678815f010bd016ca561e4672d2d83271cb2e (diff)
Added Ex. 2, polished script, added LICENSE
-rw-r--r--01_hello.zig49
-rw-r--r--02_std.zig21
-rw-r--r--LICENSE21
-rw-r--r--README.md14
-rwxr-xr-xziglings85
5 files changed, 99 insertions, 91 deletions
diff --git a/01_hello.zig b/01_hello.zig
index a77d919..d61a999 100644
--- a/01_hello.zig
+++ b/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");
-
-// 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:
+// Hint: Zig functions are private by default.
+// The main() function should be public.
+// Declare a public function with "pub fn ..."
//
-// pub fn my_function() void { ... }
+// Try to fix the program and run `ziglings` to see if it passes.
//
-// 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", .{});
+const std = @import("std");
- // 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"
+fn main() void {
+ 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!
diff --git a/02_std.zig b/02_std.zig
new file mode 100644
index 0000000..62ce040
--- /dev/null
+++ b/02_std.zig
@@ -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
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..bc944dc
--- /dev/null
+++ b/LICENSE
@@ -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.
diff --git a/README.md b/README.md
index 2e49f83..1bb905d 100644
--- a/README.md
+++ b/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
diff --git a/ziglings b/ziglings
index 3c91fee..70cb9b5 100755
--- a/ziglings
+++ b/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=$?
-# TODO: most of this belongs in a generalized function
-if grep -q "I AM NOT DONE" 01_hello.zig
-then
+ # 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
-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
+ # 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
+}
+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 " __ __ _ "
+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 :-)"