factor/extra/delegate/delegate.factor

103 lines
2.7 KiB
Factor
Raw Normal View History

! Copyright (C) 2007 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
2008-04-03 20:17:58 -04:00
USING: parser generic kernel classes words slots assocs sequences arrays
2008-04-14 03:48:01 -04:00
vectors definitions prettyprint combinators.lib math sets ;
IN: delegate
2008-04-05 00:14:40 -04:00
! Protocols
: cross-2each ( seq1 seq2 quot -- )
[ with each ] 2curry each ; inline
: forget-all-methods ( classes words -- )
[ 2array forget ] cross-2each ;
: protocol-words ( protocol -- words )
"protocol-words" word-prop ;
: protocol-users ( protocol -- users )
"protocol-users" word-prop ;
: users-and-words ( protocol -- users words )
[ protocol-users ] [ protocol-words ] bi ;
: forget-old-definitions ( protocol new-wordlist -- )
>r users-and-words r>
diff forget-all-methods ;
2008-04-05 00:14:40 -04:00
: define-protocol ( protocol wordlist -- )
2008-04-05 03:44:54 -04:00
! 2dup forget-old-definitions
2008-04-05 00:14:40 -04:00
{ } like "protocol-words" set-word-prop ;
2008-04-05 03:44:54 -04:00
: fill-in-depth ( wordlist -- wordlist' )
[ dup word? [ 0 2array ] when ] map ;
: PROTOCOL:
2008-04-05 00:14:40 -04:00
CREATE-WORD
dup define-symbol
dup f "inline" set-word-prop
2008-04-05 03:44:54 -04:00
parse-definition fill-in-depth define-protocol ; parsing
2008-04-05 00:14:40 -04:00
PREDICATE: protocol < word protocol-words ; ! Subclass of symbol?
M: protocol forget*
[ users-and-words forget-all-methods ] [ call-next-method ] bi ;
2008-04-05 03:44:54 -04:00
: show-words ( wordlist' -- wordlist )
[ dup second zero? [ first ] when ] map ;
M: protocol definition protocol-words show-words ;
2008-04-05 00:14:40 -04:00
M: protocol definer drop \ PROTOCOL: \ ; ;
M: protocol synopsis* word-synopsis ; ! Necessary?
GENERIC: group-words ( group -- words )
M: protocol group-words
"protocol-words" word-prop ;
M: tuple-class group-words
2008-04-05 03:44:54 -04:00
"slot-names" word-prop [
[ reader-word ] [ writer-word ] bi
2array [ 0 2array ] map
] map concat ;
2008-04-05 00:14:40 -04:00
! Consultation
: define-consult-method ( word class quot -- )
2008-04-05 03:44:54 -04:00
[ drop swap first create-method ]
[ nip swap first2 swapd [ ndip ] 2curry swap suffix ] 3bi define ;
2008-04-03 20:17:58 -04:00
: change-word-prop ( word prop quot -- )
>r swap word-props r> change-at ; inline
2008-04-05 00:14:40 -04:00
: add ( item vector/f -- vector )
2dup member? [ nip ] [ ?push ] if ;
2008-04-05 03:44:54 -04:00
: use-protocol ( class group -- )
2008-04-05 00:14:40 -04:00
"protocol-users" [ add ] change-word-prop ;
2008-04-05 03:44:54 -04:00
: define-consult ( group class quot -- )
swapd >r 2dup use-protocol group-words swap r>
2008-03-17 04:27:41 -04:00
[ define-consult-method ] 2curry each ;
: CONSULT:
2008-04-05 03:44:54 -04:00
scan-word scan-word parse-definition define-consult ; parsing
2008-04-05 00:14:40 -04:00
! Mimic still needs to be updated
2008-04-05 03:44:54 -04:00
: mimic-method ( mimicker mimicked generic -- )
tuck method
[ [ create-method-in ] [ word-def ] bi* define ]
[ 2drop ] if* ;
: define-mimic ( group mimicker mimicked -- )
2008-04-05 03:44:54 -04:00
[ drop swap use-protocol ] [
rot group-words -rot
[ rot first mimic-method ] 2curry each
] 3bi ;
: MIMIC:
scan-word scan-word scan-word define-mimic ; parsing