2019-03-20 15:55:57 -07:00
|
|
|
#!/usr/bin/env nomsu -V7.0.0
|
|
|
|
###
|
2018-06-14 23:25:05 -07:00
|
|
|
This file defines the code that creates and manipulates coroutines
|
2018-11-11 15:50:46 -08:00
|
|
|
|
2019-01-10 16:33:37 -08:00
|
|
|
use "core/metaprogramming"
|
|
|
|
use "core/operators"
|
|
|
|
use "core/control_flow"
|
2018-07-20 20:27:15 -07:00
|
|
|
|
2018-11-08 15:23:22 -08:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2018-07-22 13:59:08 -07:00
|
|
|
test:
|
2019-03-09 15:53:55 -08:00
|
|
|
$fn =
|
2018-11-17 14:38:05 -08:00
|
|
|
->:
|
|
|
|
yield 4
|
|
|
|
yield 5
|
2018-11-19 17:37:37 -08:00
|
|
|
repeat 3 times:
|
|
|
|
yield 6
|
2019-03-27 15:22:46 -07:00
|
|
|
|
2018-12-14 20:21:03 -08:00
|
|
|
$nums = []
|
2019-03-20 15:55:57 -07:00
|
|
|
for $ in (coroutine from $fn): $nums, add $
|
2019-03-09 15:53:55 -08:00
|
|
|
assume ($nums == [4, 5, 6, 6, 6])
|
2018-12-30 19:04:34 -08:00
|
|
|
$d = {.x = 0}
|
2019-03-09 15:53:55 -08:00
|
|
|
$co =
|
2018-11-17 14:38:05 -08:00
|
|
|
coroutine:
|
2018-12-14 20:21:03 -08:00
|
|
|
$d.x += 1
|
2018-11-17 14:38:05 -08:00
|
|
|
yield 1
|
2018-12-14 20:21:03 -08:00
|
|
|
$d.x += 1
|
2018-11-17 14:38:05 -08:00
|
|
|
yield
|
2018-12-14 20:21:03 -08:00
|
|
|
$d.x += 1
|
2019-03-27 15:22:46 -07:00
|
|
|
|
2019-03-09 15:53:55 -08:00
|
|
|
repeat while ((coroutine status of $co) != "dead"): resume $co
|
2018-12-14 20:21:03 -08:00
|
|
|
assume $d.x == 3
|
2019-03-09 15:53:55 -08:00
|
|
|
$(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
|
2019-03-20 15:55:57 -07:00
|
|
|
assume ($t == [4, 5, nil, 6])
|
2019-03-09 15:53:55 -08:00
|
|
|
$t = []
|
|
|
|
for ($k = $) in
|
|
|
|
coroutine:
|
|
|
|
yield 4
|
|
|
|
yield 5
|
|
|
|
yield
|
|
|
|
yield 6
|
|
|
|
..:
|
|
|
|
$t, add {.key = $k, .value = $}
|
2019-03-20 15:55:57 -07:00
|
|
|
|
2019-03-09 15:53:55 -08:00
|
|
|
assume $t == [
|
|
|
|
{.key = 1, .value = 4}, {.key = 2, .value = 5}, {.key = 3}, {.key = 4, .value = 6}
|
|
|
|
]
|
2019-03-20 15:55:57 -07:00
|
|
|
(coroutine $body) parses as (coroutine from ->$body)
|
2019-03-09 15:53:55 -08:00
|
|
|
external:
|
|
|
|
($ is a dead coroutine) means
|
2019-03-20 15:55:57 -07:00
|
|
|
((lua type of $) == "thread") and ((coroutine status of $) == "dead")
|