1 ! Copyright (C) 2007, 2008 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors parser generic kernel classes classes.tuple
4 words slots assocs sequences arrays vectors definitions
5 prettyprint math hashtables sets macros namespaces make ;
8 : protocol-words ( protocol -- words )
9 \ protocol-words word-prop ;
11 : protocol-consult ( protocol -- consulters )
12 \ protocol-consult word-prop ;
14 GENERIC: group-words ( group -- words )
16 M: tuple-class group-words
19 [ reader-word 0 2array ]
20 [ writer-word 0 2array ] bi
26 : consult-method ( word class quot -- )
27 [ drop swap first create-method ]
39 : change-word-prop ( word prop quot -- )
40 rot props>> swap change-at ; inline
42 : register-protocol ( group class quot -- )
43 rot \ protocol-consult [ swapd ?set-at ] change-word-prop ;
45 : define-consult ( group class quot -- )
47 [ rot group-words -rot [ consult-method ] 2curry each ]
51 scan-word scan-word parse-definition define-consult ; parsing
55 : cross-2each ( seq1 seq2 quot -- )
56 [ with each ] 2curry each ; inline
58 : forget-all-methods ( classes words -- )
59 [ first method forget ] cross-2each ;
61 : protocol-users ( protocol -- users )
62 protocol-consult keys ;
64 : lost-words ( protocol wordlist -- lost-words )
65 [ protocol-words ] dip diff ;
67 : forget-old-definitions ( protocol new-wordlist -- )
68 [ drop protocol-users ] [ lost-words ] 2bi
71 : added-words ( protocol wordlist -- added-words )
72 swap protocol-words diff ;
74 : add-new-definitions ( protocol wordlist -- )
75 [ drop protocol-consult >alist ] [ added-words ] 2bi
76 [ swap first2 consult-method ] cross-2each ;
78 : initialize-protocol-props ( protocol wordlist -- )
80 drop \ protocol-consult
81 [ H{ } assoc-like ] change-word-prop
82 ] [ { } like \ protocol-words set-word-prop ] 2bi ;
84 : fill-in-depth ( wordlist -- wordlist' )
85 [ dup word? [ 0 2array ] when ] map ;
87 : define-protocol ( protocol wordlist -- )
89 [ forget-old-definitions ]
90 [ add-new-definitions ]
91 [ initialize-protocol-props ] 2tri ;
96 [ f "inline" set-word-prop ]
97 [ parse-definition define-protocol ] tri ; parsing
99 PREDICATE: protocol < word protocol-words ; ! Subclass of symbol?
102 [ f forget-old-definitions ] [ call-next-method ] bi ;
104 : show-words ( wordlist' -- wordlist )
105 [ dup second zero? [ first ] when ] map ;
107 M: protocol definition protocol-words show-words ;
109 M: protocol definer drop \ PROTOCOL: \ ; ;
111 M: protocol synopsis* word-synopsis ; ! Necessary?
113 M: protocol group-words protocol-words ;