aboutsummaryrefslogtreecommitdiff
path: root/core/coroutines.nom
blob: 27797fae39a50893ab9a8cd51bde2ea7210406b7 (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
#!/usr/bin/env nomsu -V4.8.10
#
    This file defines the code that creates and manipulates coroutines

use "core/metaprogramming.nom"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

test:
    %nums = []
    %co = (..)
        coroutine:
            -> 4
            -> 5
            repeat 3 times: -> 6
    
    for % in coroutine %co: %nums::add %
    assume (%nums == [4, 5, 6, 6, 6]) or barf "Coroutine iteration failed"
[coroutine %body, generator %body] all compile to "\
    ..(function()
        \(%body as lua)
    end)"

(-> %) compiles to "coroutine.yield(true, \((% as lua expr) if % else "nil"))"
(for % in coroutine %co %body) compiles to "\
    ..for _junk,\(% as lua expr) in coroutine.wrap(\(%co as lua expr)) do
        \(%body as lua)
    end"