2005-01-29 14:18:28 -05:00
|
|
|
! Copyright (C) 2003, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-09-14 00:37:50 -04:00
|
|
|
IN: kernel
|
2005-09-16 02:39:33 -04:00
|
|
|
USING: arrays errors lists namespaces sequences words vectors ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-09-18 23:22:58 -04:00
|
|
|
TUPLE: continuation data c call name catch ;
|
|
|
|
|
|
|
|
: c-stack ( -- c-stack )
|
|
|
|
#! In the interpreter, this is a no-op. The compiler has an
|
|
|
|
#! an intrinsic for this word.
|
|
|
|
f ;
|
|
|
|
|
|
|
|
: set-c-stack ( c-stack -- )
|
|
|
|
[ "not supported" throw ] when ;
|
2004-08-23 01:13:09 -04:00
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: continuation ( -- interp )
|
|
|
|
#! The continuation is reified from after the *caller* of
|
|
|
|
#! this word returns.
|
2005-09-18 23:22:58 -04:00
|
|
|
datastack c-stack callstack dup pop* dup pop*
|
|
|
|
namestack catchstack <continuation> ; inline
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-09-18 23:22:58 -04:00
|
|
|
: >continuation< ( continuation -- data c call name catch )
|
|
|
|
[ continuation-data ] keep
|
|
|
|
[ continuation-c ] keep
|
|
|
|
[ continuation-call ] keep
|
|
|
|
[ continuation-name ] keep
|
|
|
|
continuation-catch ; inline
|
2005-06-15 23:27:28 -04:00
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: continue ( continuation -- )
|
|
|
|
#! Restore a continuation.
|
2005-09-18 23:22:58 -04:00
|
|
|
>continuation< set-catchstack set-namestack set-callstack
|
|
|
|
>r set-datastack r> set-c-stack ;
|
2005-09-14 00:37:50 -04:00
|
|
|
|
|
|
|
: continue-with ( object continuation -- object )
|
|
|
|
#! Restore a continuation, and place the object in the
|
|
|
|
#! restored data stack.
|
2005-09-18 23:22:58 -04:00
|
|
|
>continuation< set-catchstack set-namestack set-callstack
|
|
|
|
>r swap >r set-datastack r> r> set-c-stack ;
|
2005-09-14 00:37:50 -04:00
|
|
|
|
2005-09-18 01:37:28 -04:00
|
|
|
: (callcc) ( terminator balance -- | quot: continuation -- )
|
|
|
|
#! Direct calls to this word will not compile correctly;
|
|
|
|
#! use callcc0 or callcc1 to declare continuation arity
|
|
|
|
#! instead. The terminator branch always executes. It might
|
|
|
|
#! contain a call to 'continue', which ends control flow.
|
|
|
|
#! The balance branch is never called, but it is there to
|
|
|
|
#! give the callcc form a stack effect.
|
|
|
|
>r >r
|
2005-09-18 23:22:58 -04:00
|
|
|
continuation dup continuation-call dup pop* pop*
|
|
|
|
t r> r> ifte ; inline
|
2005-09-18 01:37:28 -04:00
|
|
|
|
|
|
|
: callcc0 ( quot -- | quot: continuation -- )
|
2005-09-14 00:37:50 -04:00
|
|
|
#! Call a quotation with the current continuation, which may
|
2005-09-18 01:37:28 -04:00
|
|
|
#! be restored using continue.
|
|
|
|
[ drop ] (callcc) ; inline
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-09-18 01:37:28 -04:00
|
|
|
: callcc1 ( quot -- | quot: continuation -- )
|
|
|
|
#! Call a quotation with the current continuation, which may
|
|
|
|
#! be restored using continue-with.
|
|
|
|
[ ] (callcc) ; inline
|