1 ! Copyright (C) 2005 Chris Double, 2007 Clemens Hofreither, 2008 James Cash.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel hashtables namespaces make continuations quotations
9 TUPLE: coroutine resumecc exitcc originalcc ;
11 : cocreate ( quot -- co )
13 dup current-coro associate
15 swapd , , \ with-variables ,
16 "Coroutine has terminated illegally." , \ throw ,
18 [ >>resumecc ] [ >>originalcc ] bi ;
20 : coresume ( v co -- result )
24 #! At this point, the coroutine quotation must have terminated
25 #! normally (without calling coyield, coreset, or coterminate).
26 #! This shouldn't happen.
30 : coresume* ( v co -- ) coresume drop ; inline
31 : *coresume ( co -- result ) f swap coresume ; inline
33 : coyield ( v -- result )
36 [ continue-with ] curry
38 exitcc>> continue-with
41 : coyield* ( v -- ) coyield drop ; inline
42 : *coyield ( -- v ) f coyield ; inline
44 : coterminate ( v -- )
47 exitcc>> continue-with ;
51 originalcc>> >>resumecc
52 exitcc>> continue-with ;