nomsu/core/coroutines.nom
Bruce Hill 652c29bdef Major overhaul, splitting nomsu_compiler into nomsu_environment,
nomsu_compiler, and nomsu_decompiler. Also added comprehensions.
2018-11-08 15:24:15 -08:00

32 lines
923 B
Plaintext

#!/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 (..)
Lua value "\
..(function()
\(%body as lua statements)
end)"
(->) compiles to (Lua value "coroutine.yield(true)")
(-> %) compiles to (Lua value "coroutine.yield(true, \(% as lua expr))")
(for % in coroutine %co %body) compiles to (..)
Lua "\
..for junk,\(% as lua expr) in coroutine.wrap(\(%co as lua expr)) do
\(%body as lua statements)
end"