1 ! Copyright (C) 2007, 2008 Daniel Ehrenberg
2 ! Portions copyright (C) 2009, 2010 Slava Pestov, Joe Groff
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays assocs classes.tuple definitions effects generic
5 generic.standard hashtables kernel lexer math parser
6 generic.parser sequences sets slots words words.symbol fry
10 ERROR: broadcast-words-must-have-no-outputs group ;
14 : protocol-words ( protocol -- words )
15 \ protocol-words word-prop ;
17 : protocol-consult ( protocol -- consulters )
18 \ protocol-consult word-prop ;
20 GENERIC: group-words ( group -- words )
22 M: standard-generic group-words
23 dup "combination" word-prop #>> 2array 1array ;
25 : slot-words, ( slot-spec -- )
26 [ name>> reader-word 0 2array , ]
28 dup read-only>> [ drop ] [
29 name>> writer-word 0 2array ,
33 : slot-group-words ( slots -- words )
34 [ [ slot-words, ] each ] { } make ;
36 M: tuple-class group-words
37 all-slots slot-group-words ;
39 : check-broadcast-group ( group -- group )
40 dup group-words [ first stack-effect out>> empty? ] all?
41 [ broadcast-words-must-have-no-outputs ] unless ;
45 TUPLE: consultation group class quot loc ;
46 TUPLE: broadcast < consultation ;
48 : <consultation> ( group class quot -- consultation )
50 : <broadcast> ( group class quot -- consultation )
51 [ check-broadcast-group ] 2dip f broadcast boa ;
53 : create-consult-method ( word consultation -- method )
54 [ class>> swap first create-method dup fake-definition ] keep
55 [ drop ] [ "consultation" set-word-prop ] 2bi ;
57 PREDICATE: consult-method < method
58 "consultation" word-prop >boolean ;
60 M: consult-method reset-word
61 [ call-next-method ] [ f "consultation" set-word-prop ] bi ;
63 GENERIC# (consult-method-quot) 2 ( consultation quot word -- object )
65 M: consultation (consult-method-quot)
66 '[ _ call _ execute ] nip ;
67 M: broadcast (consult-method-quot)
68 '[ _ call [ _ execute ] each ] nip ;
70 : consult-method-quot ( consultation word -- object )
72 [ second [ [ dip ] curry ] times ] [ first ] bi
73 (consult-method-quot) ;
75 : define-consult-method ( word consultation -- )
76 [ create-consult-method ]
77 [ swap consult-method-quot ] 2bi
80 : each-generic ( consultation quot -- )
81 [ [ group>> group-words ] keep ] dip curry each ; inline
83 : register-consult ( consultation -- )
84 [ group>> \ protocol-consult ] [ ] [ class>> ] tri
85 '[ [ _ _ ] dip ?set-at ] change-word-prop ;
87 : consult-methods ( consultation -- )
88 [ define-consult-method ] each-generic ;
90 : unregister-consult ( consultation -- )
91 [ class>> ] [ group>> ] bi
92 \ protocol-consult word-prop delete-at ;
94 : unconsult-method ( word consultation -- )
95 [ class>> swap first ?lookup-method ] keep
97 over "consultation" word-prop eq?
98 [ forget ] [ drop ] if
101 : unconsult-methods ( consultation -- )
102 [ unconsult-method ] each-generic ;
106 : define-consult ( consultation -- )
107 [ register-consult ] [ consult-methods ] bi ;
110 scan-word scan-word parse-definition <consultation>
111 [ save-location ] [ define-consult ] bi ;
114 scan-word scan-word parse-definition <broadcast>
115 [ save-location ] [ define-consult ] bi ;
117 M: consultation where loc>> ;
119 M: consultation set-where loc<< ;
121 M: consultation forget*
122 [ unconsult-methods ] [ unregister-consult ] bi ;
127 : forget-all-methods ( classes words -- )
128 [ first ?lookup-method forget ] cartesian-each ;
130 : protocol-users ( protocol -- users )
131 protocol-consult keys ;
133 : lost-words ( protocol wordlist -- lost-words )
134 [ protocol-words ] dip diff ;
136 : forget-old-definitions ( protocol new-wordlist -- )
137 [ drop protocol-users ] [ lost-words ] 2bi
140 : added-words ( protocol wordlist -- added-words )
141 swap protocol-words diff ;
143 : add-new-definitions ( protocol wordlist -- )
144 [ drop protocol-consult values ] [ added-words ] 2bi
145 [ swap define-consult-method ] cartesian-each ;
147 : initialize-protocol-props ( protocol wordlist -- )
149 drop \ protocol-consult
150 [ H{ } assoc-like ] change-word-prop
151 ] [ { } like \ protocol-words set-word-prop ] 2bi ;
153 : fill-in-depth ( wordlist -- wordlist' )
154 [ dup word? [ 0 2array ] when ] map ;
156 : show-words ( wordlist' -- wordlist )
157 [ dup second zero? [ first ] when ] map ;
159 ERROR: not-a-generic word ;
161 : check-generic ( generic -- )
162 dup array? [ first ] when
163 dup generic? [ drop ] [ not-a-generic ] if ;
167 : define-protocol ( protocol wordlist -- )
168 dup [ check-generic ] each
169 [ drop define-symbol ] [
171 [ forget-old-definitions ]
172 [ add-new-definitions ]
173 [ initialize-protocol-props ] 2tri
177 scan-new-word parse-definition define-protocol ;
179 PREDICATE: protocol < word protocol-words ; ! Subclass of symbol?
182 [ f forget-old-definitions ] [ call-next-method ] bi ;
185 M: protocol definition protocol-words show-words ;
187 M: protocol definer drop \ PROTOCOL: \ ; ;
189 M: protocol group-words protocol-words ;
191 SYNTAX: SLOT-PROTOCOL:
193 [ [ reader-word ] [ writer-word ] bi 2array ]
194 map-tokens concat define-protocol ;