factor/extra/advice/advice.factor

49 lines
1.2 KiB
Factor
Raw Normal View History

2008-11-05 09:19:59 -05:00
! Copyright (C) 2008 James Cash
! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences symbols fry words assocs tools.annotations coroutines ;
2008-11-05 09:19:59 -05:00
IN: advice
2008-11-06 00:20:15 -05:00
SYMBOLS: before after around advised ;
2008-11-05 09:19:59 -05:00
2008-11-06 00:44:11 -05:00
<PRIVATE
: advise ( quot name word loc -- )
word-prop set-at ;
2008-11-06 00:44:11 -05:00
PRIVATE>
: advise-before ( quot name word -- )
before advise ;
: advise-after ( quot name word -- )
after advise ;
: advise-around ( quot name word -- )
2008-11-06 00:44:11 -05:00
[ \ coterminate suffix ] 2dip
around advise ;
2008-11-05 09:19:59 -05:00
: get-advice ( word type -- seq )
word-prop values ;
: call-before ( word -- )
before get-advice [ call ] each ;
: call-after ( word -- )
after get-advice [ call ] each ;
: call-around ( main word -- )
2008-11-06 00:44:11 -05:00
around get-advice [ cocreate ] map tuck
2008-11-06 00:20:15 -05:00
[ [ coresume ] each ] [ call ] [ reverse [ coresume ] each ] tri* ;
2008-11-05 09:19:59 -05:00
: remove-advice ( name word loc -- )
word-prop delete-at ;
: ad-do-it ( input -- result )
coyield ;
2008-11-06 00:20:15 -05:00
: advised? ( word -- ? )
advised word-prop ;
2008-11-05 09:19:59 -05:00
: make-advised ( word -- )
[ dup [ over dup '[ _ call-before _ _ call-around _ call-after ] ] annotate ]
2008-11-06 00:20:15 -05:00
[ { before after around } [ H{ } clone swap set-word-prop ] with each ]
[ t advised set-word-prop ] tri ;
2008-11-05 09:19:59 -05:00