aboutsummaryrefslogtreecommitdiff
path: root/lib/core/coroutines.nom
blob: b151a4b250b25eb89aae889ef8f2a9b87f44e632 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env nomsu -V6.15.13.8
#
    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")