factor/extra/delegate/delegate.factor

94 lines
2.4 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-05 00:14:40 -04:00
vectors definitions prettyprint ;
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>
seq-diff forget-all-methods ;
: define-protocol ( protocol wordlist -- )
2dup forget-old-definitions
{ } like "protocol-words" set-word-prop ;
: PROTOCOL:
2008-04-05 00:14:40 -04:00
CREATE-WORD
dup define-symbol
dup f "inline" set-word-prop
parse-definition 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 ;
M: protocol definition protocol-words ;
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: generic group-words
2008-04-03 20:17:58 -04:00
1array ;
M: tuple-class group-words
2008-04-05 00:14:40 -04:00
"slots" word-prop
[ [ slot-spec-reader ] map ]
[ [ slot-spec-writer ] map ] bi append ;
! Consultation
: define-consult-method ( word class quot -- )
pick suffix >r swap create-method r> 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-03 20:17:58 -04:00
: declare-consult ( class group -- )
2008-04-05 00:14:40 -04:00
"protocol-users" [ add ] change-word-prop ;
: define-consult ( class group quot -- )
2008-04-03 20:17:58 -04:00
>r 2dup declare-consult group-words swap r>
2008-03-17 04:27:41 -04:00
[ define-consult-method ] 2curry each ;
: CONSULT:
scan-word scan-word parse-definition swapd define-consult ; parsing
2008-04-05 00:14:40 -04:00
! Mimic still needs to be updated
: define-mimic ( group mimicker mimicked -- )
2008-04-05 00:14:40 -04:00
rot group-words -rot [
2007-12-08 03:21:32 -05:00
pick "methods" word-prop at dup
2008-04-05 00:14:40 -04:00
[ >r swap create-method-in r> word-def define ]
2008-03-05 16:24:13 -05:00
[ 3drop ] if
] 2curry each ;
: MIMIC:
scan-word scan-word scan-word define-mimic ; parsing