nomsu/lib/core/coroutines.nom
Bruce Hill a1849da175 Autoformat (mostly just to do with the new
blank-line-after-end-of-multi-indent-block rule
2019-03-27 15:22:46 -07:00

65 lines
1.5 KiB
Plaintext

#!/usr/bin/env nomsu -V7.0.0
###
This file defines the code that creates and manipulates coroutines
use "core/metaprogramming"
use "core/operators"
use "core/control_flow"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test:
$fn =
->:
yield 4
yield 5
repeat 3 times:
yield 6
$nums = []
for $ in (coroutine from $fn): $nums, add $
assume ($nums == [4, 5, 6, 6, 6])
$d = {.x = 0}
$co =
coroutine:
$d.x += 1
yield 1
$d.x += 1
yield
$d.x += 1
repeat while ((coroutine status of $co) != "dead"): resume $co
assume $d.x == 3
$(co) = (coroutine: yield 5)
[$ok, $val] = (co)
assume ($ok == (yes))
assume ($val == 5)
$t = []
$i = 1
for $ in
coroutine:
yield 4
yield 5
yield
yield 6
..:
$t.$i = $
$i += 1
assume ($t == [4, 5, nil, 6])
$t = []
for ($k = $) in
coroutine:
yield 4
yield 5
yield
yield 6
..:
$t, add {.key = $k, .value = $}
assume $t == [
{.key = 1, .value = 4}, {.key = 2, .value = 5}, {.key = 3}, {.key = 4, .value = 6}
]
(coroutine $body) parses as (coroutine from ->$body)
external:
($ is a dead coroutine) means
((lua type of $) == "thread") and ((coroutine status of $) == "dead")