summaryrefslogtreecommitdiff
path: root/ziglings
blob: 7825cf6dd00368bf5e46e1ff499f9ac7055e55d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash

# ziglings takes one parameter: the exercise number to jump to
jump_to=${1:-0}

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

exercise_num=0

function check_it {
    source_file=$1
    correct_output=$2
    hint=$3

    # 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

    # 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
}


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"
check_it 03_assignment.zig "55 314159 -11" "There are three mistakes in this one!"
check_it 04_arrays.zig "Fourth: 7, Length: 8" "There are two things to complete here."
check_it 05_arrays2.zig "LEET: 1337, Bits: 100110011001" "Fill in the two arrays."
check_it 06_strings.zig "d=d ha ha ha Major Tom" "Each '???' needs something filled in."
check_it 07_strings2.zig "Ziggy" "Please fix the lyrics!"
check_it 08_quiz.zig "Program in Zig" "See if you can fix the program!"

echo
echo "    __   __          _ "
echo "    \ \ / __ _ _   _| | "
echo "     \ V / _' | | | | | "
echo "      | | (_| | |_| |_| "
echo "      |_|\__,_|\__, (_) "
echo "               |___/ "
echo
echo "You've completed all of the Ziglings exercises!"
echo