aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--README.md5
-rw-r--r--build.zig7
-rw-r--r--exercises/084_async.zig56
-rw-r--r--patches/patches/084_async.patch4
4 files changed, 67 insertions, 5 deletions
diff --git a/README.md b/README.md
index 7a44cf0..44ceb22 100644
--- a/README.md
+++ b/README.md
@@ -149,10 +149,7 @@ Core Language
* [x] Sentinel termination
* [x] Quoted identifiers @""
* [x] Anonymous structs/tuples/lists
-* [ ] Suspend / Resume
-* [ ] Async / Await
-* [ ] Nosuspend
-* [ ] Async Frames, Suspend Blocks
+* [ ] Async
* [ ] Working with C?
Modules and the Zig Standard Library
diff --git a/build.zig b/build.zig
index 1269a0e..fc831b8 100644
--- a/build.zig
+++ b/build.zig
@@ -407,12 +407,17 @@ const exercises = [_]Exercise{
.{
.main_file = "082_anonymous_structs3.zig",
.output = "\"0\"(bool):true \"1\"(bool):false \"2\"(i32):42 \"3\"(f32):3.14159202e+00",
- .hint = "This one is a challenge! But you have everything you need."
+ .hint = "This one is a challenge! But you have everything you need.",
},
.{
.main_file = "083_anonymous_lists.zig",
.output = "I say hello!",
},
+ .{
+ .main_file = "084_async.zig",
+ .output = "foo() A",
+ .hint = "Read the facts. Use the facts.",
+ },
};
/// Check the zig version to make sure it can compile the examples properly.
diff --git a/exercises/084_async.zig b/exercises/084_async.zig
new file mode 100644
index 0000000..fe68b03
--- /dev/null
+++ b/exercises/084_async.zig
@@ -0,0 +1,56 @@
+//
+// Six Facts:
+//
+// 1. The memory space allocated to your program for the
+// invocation of a function and all of its data is called a
+// "stack frame".
+//
+// 2. The 'return' keyword "pops" the current function
+// invocation's frame off of the stack (it is no longer needed)
+// and returns control to the place where the function was
+// called.
+//
+// fn foo() void {
+// return; // Pop the frame and return control
+// }
+//
+// 3. Like 'return', the 'suspend' keyword returns control to the
+// place where the function was called BUT the function
+// invocation's frame remains so that it can regain control again
+// at a later time. Functions which do this are "async"
+// functions.
+//
+// fn fooThatSuspends() void {
+// suspend; // return control, but leave the frame alone
+// }
+//
+// 4. To call any function in async context and get a reference
+// to its frame for later use, use the 'async' keyword:
+//
+// var foo_frame = async fooThatSuspends();
+//
+// 5. If you call an async function without the 'async' keyword,
+// the function FROM WHICH you called the async function itself
+// becomes async! In this example, the bar() function is now
+// async because it calls fooThatSuspends(), which is async.
+//
+// fn bar() void {
+// fooThatSuspends();
+// }
+//
+// 6. The main() function cannot be async!
+//
+// Given facts 3 and 4, how do we fix this program (broken by facts
+// 5 and 6)?
+//
+const print = @import("std").debug.print;
+
+pub fn main() void {
+ foo();
+}
+
+fn foo() void {
+ print("foo() A\n", .{});
+ suspend;
+ print("foo() B\n", .{});
+}
diff --git a/patches/patches/084_async.patch b/patches/patches/084_async.patch
new file mode 100644
index 0000000..6164775
--- /dev/null
+++ b/patches/patches/084_async.patch
@@ -0,0 +1,4 @@
+49c49
+< foo();
+---
+> var foo_frame = async foo();