aboutsummaryrefslogtreecommitdiff
path: root/lib/core/coroutines.nom
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-01-14 15:42:48 -0800
committerBruce Hill <bruce@bruce-hill.com>2019-01-14 15:43:24 -0800
commitc1c32688a4afc43f6addb99b8b5fa878944a70e3 (patch)
treec886f21b5b08a9053aa74fcba4b241dae5ede76d /lib/core/coroutines.nom
parent2309b696fc34b24f05f6658b94f9105ca8ee76e4 (diff)
Overhaul in progress, mostly working. Moved all the nomsu packages into
lib/, including core/*. Changes to how nomsu environments and importing work.
Diffstat (limited to 'lib/core/coroutines.nom')
-rw-r--r--lib/core/coroutines.nom40
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/core/coroutines.nom b/lib/core/coroutines.nom
new file mode 100644
index 0000000..6a99f7e
--- /dev/null
+++ b/lib/core/coroutines.nom
@@ -0,0 +1,40 @@
+#!/usr/bin/env nomsu -V6.14
+#
+ This file defines the code that creates and manipulates coroutines
+
+use "core/metaprogramming"
+use "core/operators"
+use "core/control_flow"
+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+test:
+ $co =
+ ->:
+ yield 4
+ yield 5
+ repeat 3 times:
+ yield 6
+ $nums = []
+ for $ in coroutine $co:
+ $nums, add $
+
+ unless ($nums == [4, 5, 6, 6, 6]):
+ fail "Coroutine iteration failed"
+
+ $d = {.x = 0}
+ $co2 =
+ coroutine:
+ $d.x += 1
+ yield 1
+ $d.x += 1
+ yield
+ $d.x += 1
+ repeat while ((coroutine status of $co2) != "dead"): resume $co2
+ assume $d.x == 3
+(coroutine $body) parses as (coroutine from (-> $body))
+(for $ in coroutine $co $body) compiles to ("
+ for \($ as lua expr) in coroutine_wrap(\($co as lua expr)) do
+ \($body as lua)
+ end
+")