2020-12-23 12:02:35 -05:00
|
|
|
#!/bin/bash
|
|
|
|
|
2021-01-03 18:55:45 -05:00
|
|
|
# ziglings takes one parameter: the exercise number to jump to
|
|
|
|
jump_to=${1:-0}
|
|
|
|
|
2020-12-23 12:02:35 -05:00
|
|
|
echo
|
|
|
|
echo " _ _ _ "
|
|
|
|
echo " ___(_) __ _| (_)_ __ __ _ ___ "
|
|
|
|
echo " |_ | |/ _' | | | '_ \ / _' / __| "
|
|
|
|
echo " / /| | (_| | | | | | | (_| \__ \ "
|
|
|
|
echo " /___|_|\__, |_|_|_| |_|\__, |___/ "
|
|
|
|
echo " |___/ |___/ "
|
|
|
|
echo
|
|
|
|
|
|
|
|
# Capture terminal escape sequences (ANSI) for formatting
|
|
|
|
fmt_err=$( tput setaf 1 ) # red foreground
|
|
|
|
fmt_yay=$( tput setaf 2 ) # green foreground
|
|
|
|
fmt_off=$( tput sgr0 ) # reset colors/effects
|
|
|
|
|
2021-01-03 18:55:45 -05:00
|
|
|
exercise_num=0
|
|
|
|
|
2021-01-03 12:21:11 -05:00
|
|
|
function check_it {
|
|
|
|
source_file=$1
|
|
|
|
correct_output=$2
|
|
|
|
hint=$3
|
2020-12-23 12:02:35 -05:00
|
|
|
|
2021-01-03 18:55:45 -05:00
|
|
|
# If the current exercise is less than the requested one, skip it
|
|
|
|
let exercise_num+=1
|
|
|
|
if [[ $exercise_num -lt $jump_to ]]
|
|
|
|
then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2021-01-03 12:21:11 -05:00
|
|
|
# Compile/run the source and capture the result and exit value
|
|
|
|
cmd="zig run $source_file"
|
|
|
|
echo "$ $cmd"
|
|
|
|
result=$($cmd 2>&1)
|
|
|
|
result_status=$?
|
2020-12-23 12:02:35 -05:00
|
|
|
|
2021-01-03 12:21:11 -05:00
|
|
|
# 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
|
2020-12-23 12:02:35 -05:00
|
|
|
|
2021-01-03 12:21:11 -05:00
|
|
|
# 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
|
|
|
|
}
|
2020-12-23 12:02:35 -05:00
|
|
|
|
|
|
|
|
2021-01-03 12:21:11 -05:00
|
|
|
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"
|
2021-01-03 18:55:45 -05:00
|
|
|
check_it 03_assignment.zig "55 314159 -11"
|
2021-01-03 12:21:11 -05:00
|
|
|
|
|
|
|
echo
|
|
|
|
echo " __ __ _ "
|
|
|
|
echo " \ \ / __ _ _ _| | "
|
|
|
|
echo " \ V / _' | | | | | "
|
|
|
|
echo " | | (_| | |_| |_| "
|
|
|
|
echo " |_|\__,_|\__, (_) "
|
|
|
|
echo " |___/ "
|
2020-12-23 12:02:35 -05:00
|
|
|
echo
|
2021-01-03 12:21:11 -05:00
|
|
|
echo "You've completed all of the Ziglings exercises!"
|
2020-12-23 12:02:35 -05:00
|
|
|
echo
|