factor/library/continuations.factor

70 lines
2.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: errors
USING: kernel-internals ;
: catchstack ( -- cs ) 6 getenv ;
: set-catchstack ( cs -- ) 6 setenv ;
2005-09-14 00:37:50 -04:00
IN: kernel
USING: namespaces sequences ;
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
: ifcc ( terminator balance -- | quot: continuation -- )
[
t continuation
dup continuation-data dup pop* f swap push
swap >r -rot r>
] call -rot ifte ; inline
: infer-only ( quot -- )
#! For stack effect inference, pretend the quotation is
#! there, but ignore it during execution.
drop ;
: (callcc0) ( -- ) [ drop ] infer-only ; inline
: (callcc1) ( -- value ) (callcc0) 9 getenv ; inline
: callcc1 ( quot -- | quot: continuation -- )
#! Call a quotation with the current continuation, which may
#! be restored using continue-with.
[ (callcc1) ] ifcc ; inline
: callcc0 ( quot -- | quot: continuation -- )
#! Call a quotation with the current continuation, which may
#! be restored using continue-with.
[ (callcc0) ] ifcc ; inline
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.
swap 9 setenv continue ; inline