aboutsummaryrefslogtreecommitdiff
path: root/lib/core/coroutines.nom
blob: 6a99f7e94687d51c658b8a72e0153b874a355514 (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
#!/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
")