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-06-15 23:27:28 -04:00
|
|
|
TUPLE: interp data call name catch ;
|
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.
|
|
|
|
datastack callstack dup pop* dup pop*
|
2005-06-15 23:27:28 -04:00
|
|
|
namestack catchstack <interp> ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-06-15 23:27:28 -04:00
|
|
|
: >interp< ( interp -- data call name catch )
|
|
|
|
[ interp-data ] keep
|
|
|
|
[ interp-call ] keep
|
|
|
|
[ interp-name ] keep
|
|
|
|
interp-catch ;
|
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: continue ( continuation -- )
|
|
|
|
#! Restore a continuation.
|
|
|
|
>interp<
|
|
|
|
set-catchstack set-namestack set-callstack set-datastack ;
|
|
|
|
|
|
|
|
: continue-with ( object continuation -- object )
|
|
|
|
#! Restore a continuation, and place the object in the
|
|
|
|
#! restored data stack.
|
|
|
|
>interp< set-catchstack set-namestack
|
|
|
|
>r swap >r set-datastack r> r> set-callstack ;
|
|
|
|
|
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
|
|
|
|
continuation dup interp-call dup pop* pop*
|
|
|
|
t r> r> ifte ;
|
|
|
|
inline
|
|
|
|
|
|
|
|
: 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
|