2008-07-14 14:37:24 -04:00
|
|
|
! Copyright (C) 2007, 2008 Daniel Ehrenberg
|
2009-02-09 01:25:33 -05:00
|
|
|
! Portions copyright (C) 2009 Slava Pestov
|
2007-11-28 10:49:43 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-01-16 17:39:24 -05:00
|
|
|
USING: accessors arrays assocs classes.tuple definitions
|
2009-02-09 01:25:33 -05:00
|
|
|
generalizations generic generic.standard hashtables kernel
|
|
|
|
lexer make math parser generic.parser sequences sets slots
|
|
|
|
words words.symbol fry ;
|
2007-11-28 10:49:43 -05:00
|
|
|
IN: delegate
|
|
|
|
|
2008-04-17 23:39:25 -04:00
|
|
|
: protocol-words ( protocol -- words )
|
|
|
|
\ protocol-words word-prop ;
|
|
|
|
|
|
|
|
: protocol-consult ( protocol -- consulters )
|
|
|
|
\ protocol-consult word-prop ;
|
|
|
|
|
|
|
|
GENERIC: group-words ( group -- words )
|
|
|
|
|
2009-02-09 01:25:33 -05:00
|
|
|
M: standard-generic group-words
|
|
|
|
dup "combination" word-prop #>> 2array 1array ;
|
|
|
|
|
2008-04-17 23:39:25 -04:00
|
|
|
M: tuple-class group-words
|
2008-07-14 14:37:24 -04:00
|
|
|
all-slots [
|
|
|
|
name>>
|
|
|
|
[ reader-word 0 2array ]
|
|
|
|
[ writer-word 0 2array ] bi
|
|
|
|
2array
|
2008-04-17 23:39:25 -04:00
|
|
|
] map concat ;
|
|
|
|
|
|
|
|
! Consultation
|
|
|
|
|
|
|
|
: consult-method ( word class quot -- )
|
2009-02-06 03:43:21 -05:00
|
|
|
[ drop swap first create-method-in ]
|
2008-11-25 17:47:47 -05:00
|
|
|
[ nip [ , dup second , \ ndip , first , ] [ ] make ] 3bi
|
2008-05-10 01:16:46 -04:00
|
|
|
define ;
|
2008-04-17 23:39:25 -04:00
|
|
|
|
|
|
|
: change-word-prop ( word prop quot -- )
|
2009-01-25 23:57:11 -05:00
|
|
|
[ swap props>> ] dip change-at ; inline
|
2008-04-17 23:39:25 -04:00
|
|
|
|
|
|
|
: register-protocol ( group class quot -- )
|
2009-01-25 23:57:11 -05:00
|
|
|
[ \ protocol-consult ] 2dip
|
|
|
|
'[ [ _ _ swap ] dip ?set-at ] change-word-prop ;
|
2008-04-17 23:39:25 -04:00
|
|
|
|
|
|
|
: define-consult ( group class quot -- )
|
2008-05-10 01:16:46 -04:00
|
|
|
[ register-protocol ]
|
2009-01-25 23:57:11 -05:00
|
|
|
[ [ group-words ] 2dip '[ _ _ consult-method ] each ]
|
2008-05-10 01:16:46 -04:00
|
|
|
3bi ;
|
2008-04-17 23:39:25 -04:00
|
|
|
|
|
|
|
: CONSULT:
|
|
|
|
scan-word scan-word parse-definition define-consult ; parsing
|
|
|
|
|
2008-04-05 00:14:40 -04:00
|
|
|
! Protocols
|
|
|
|
|
|
|
|
: cross-2each ( seq1 seq2 quot -- )
|
|
|
|
[ with each ] 2curry each ; inline
|
|
|
|
|
|
|
|
: forget-all-methods ( classes words -- )
|
2008-05-10 01:16:46 -04:00
|
|
|
[ first method forget ] cross-2each ;
|
2008-04-05 00:14:40 -04:00
|
|
|
|
|
|
|
: protocol-users ( protocol -- users )
|
2008-04-17 23:39:25 -04:00
|
|
|
protocol-consult keys ;
|
2008-04-05 00:14:40 -04:00
|
|
|
|
2008-04-17 23:39:25 -04:00
|
|
|
: lost-words ( protocol wordlist -- lost-words )
|
2008-09-15 11:33:03 -04:00
|
|
|
[ protocol-words ] dip diff ;
|
2008-04-05 00:14:40 -04:00
|
|
|
|
|
|
|
: forget-old-definitions ( protocol new-wordlist -- )
|
2008-05-10 02:14:36 -04:00
|
|
|
[ drop protocol-users ] [ lost-words ] 2bi
|
|
|
|
forget-all-methods ;
|
2008-04-05 00:14:40 -04:00
|
|
|
|
2008-04-17 23:39:25 -04:00
|
|
|
: added-words ( protocol wordlist -- added-words )
|
2008-05-10 01:16:46 -04:00
|
|
|
swap protocol-words diff ;
|
2008-04-17 23:39:25 -04:00
|
|
|
|
|
|
|
: add-new-definitions ( protocol wordlist -- )
|
2008-05-10 02:14:36 -04:00
|
|
|
[ drop protocol-consult >alist ] [ added-words ] 2bi
|
2008-05-10 01:16:46 -04:00
|
|
|
[ swap first2 consult-method ] cross-2each ;
|
2008-04-17 23:39:25 -04:00
|
|
|
|
|
|
|
: initialize-protocol-props ( protocol wordlist -- )
|
2008-05-10 01:16:46 -04:00
|
|
|
[
|
|
|
|
drop \ protocol-consult
|
|
|
|
[ H{ } assoc-like ] change-word-prop
|
|
|
|
] [ { } like \ protocol-words set-word-prop ] 2bi ;
|
2007-11-28 10:49:43 -05:00
|
|
|
|
2008-04-05 03:44:54 -04:00
|
|
|
: fill-in-depth ( wordlist -- wordlist' )
|
|
|
|
[ dup word? [ 0 2array ] when ] map ;
|
|
|
|
|
2008-04-17 23:39:25 -04:00
|
|
|
: define-protocol ( protocol wordlist -- )
|
2009-01-16 17:39:24 -05:00
|
|
|
[ drop define-symbol ] [
|
|
|
|
fill-in-depth
|
|
|
|
[ forget-old-definitions ]
|
|
|
|
[ add-new-definitions ]
|
|
|
|
[ initialize-protocol-props ] 2tri
|
|
|
|
] 2bi ;
|
2008-04-17 23:39:25 -04:00
|
|
|
|
2007-11-28 10:49:43 -05:00
|
|
|
: PROTOCOL:
|
2009-01-16 17:39:24 -05:00
|
|
|
CREATE-WORD parse-definition define-protocol ; parsing
|
2007-11-28 10:49:43 -05:00
|
|
|
|
2008-04-05 00:14:40 -04:00
|
|
|
PREDICATE: protocol < word protocol-words ; ! Subclass of symbol?
|
|
|
|
|
|
|
|
M: protocol forget*
|
2008-04-17 23:39:25 -04:00
|
|
|
[ f forget-old-definitions ] [ call-next-method ] bi ;
|
2008-04-05 00:14:40 -04:00
|
|
|
|
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: \ ; ;
|
|
|
|
|
2008-04-17 23:39:25 -04:00
|
|
|
M: protocol group-words protocol-words ;
|
2009-01-16 17:39:24 -05:00
|
|
|
|
|
|
|
: SLOT-PROTOCOL:
|
|
|
|
CREATE-WORD ";" parse-tokens
|
|
|
|
[ [ reader-word ] [ writer-word ] bi 2array ] map concat
|
|
|
|
define-protocol ; parsing
|