Просмотр исходного кода

Add exercise 3, exercise num param for script

Dave Gauer 4 лет назад
Родитель
Сommit
b3f74d9c30
3 измененных файлов с 44 добавлено и 1 удалено
  1. 30 0
      03_assignment.zig
  2. 1 1
      README.md
  3. 13 0
      ziglings

+ 30 - 0
03_assignment.zig

@@ -0,0 +1,30 @@
+//
+// Oh dear! It seems we got a little carried away making const u8 values.
+//     * const means constant (cannot be changed)
+//     * u8 means unsigned (cannot be negative), 8-bit integer
+//
+// Hint 1: Use 'var' for values that can change.
+// Hint 2: Use enough bits to hold the value you want:
+//             u8             255
+//             u16         65,535
+//             u32  4,294,967,295
+// Hint 3: Use 'i' (e.g. 'i8', 'i16') for signed integers.
+//
+const std = @import("std");
+
+pub fn main() void {
+    const n: u8 = 50;
+    n = n + 5;
+
+    const pi: u8 = 314159;
+
+    const negative_eleven: u8 = -11;
+
+    // There are no errors in the next line, just explanation:
+    // Perhaps you noticed before that the print function takes two
+    // parameters. Now it will make more sense: the first parameter
+    // is a string. The string may contain placeholders '{}', and the
+    // second parameter is an anonymous struct (data structure)
+    // with values to be printed in place of the placeholders.
+    std.debug.print("{} {} {}\n", .{n, pi, negative_eleven});
+}

+ 1 - 1
README.md

@@ -59,7 +59,7 @@ Planned exercises:
 
 * [x] Hello world (main needs to be public)
 * [x] Importing standard library
-* [ ] Assignment
+* [x] Assignment
 * [ ] Arrays
 * [ ] If
 * [ ] While

+ 13 - 0
ziglings

@@ -1,5 +1,8 @@
 #!/bin/bash
 
+# ziglings takes one parameter: the exercise number to jump to
+jump_to=${1:-0}
+
 echo
 echo "         _       _ _ "
 echo "     ___(_) __ _| (_)_ __   __ _ ___ "
@@ -14,11 +17,20 @@ 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"
@@ -55,6 +67,7 @@ function check_it {
 
 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"
 
 echo
 echo "    __   __          _ "