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 classes.tuple
5 compiler.units definitions effects fry generic generic.standard
6 hashtables kernel lexer make math parser sequences sets slots
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 ;
47 TUPLE: broadcast < consultation ;
49 : <consultation> ( group class quot -- consultation )
52 : <broadcast> ( group class quot -- consultation )
53 [ check-broadcast-group ] 2dip f broadcast boa ;
55 : create-consult-method ( word consultation -- method )
56 [ class>> swap first create-method dup fake-definition ] keep
57 [ drop ] [ "consultation" set-word-prop ] 2bi ;
59 PREDICATE: consult-method < method
60 "consultation" word-prop >boolean ;
62 M: consult-method reset-word
63 [ call-next-method ] [ "consultation" remove-word-prop ] bi ;
65 GENERIC#: (consult-method-quot) 2 ( consultation quot word -- object )
67 M: consultation (consult-method-quot)
68 '[ _ call _ execute ] nip ;
70 M: broadcast (consult-method-quot)
71 '[ _ call [ _ execute ] each ] nip ;
73 : consult-method-quot ( consultation word -- object )
75 [ second [ [ dip ] curry ] times ] [ first ] bi
76 (consult-method-quot) ;
78 : define-consult-method ( word consultation -- )
79 [ create-consult-method ]
80 [ swap consult-method-quot ] 2bi
83 : each-generic ( consultation quot -- )
84 [ [ group>> group-words ] keep ] dip curry each ; inline
86 : register-consult ( consultation -- )
87 [ group>> "protocol-consult" ] [ ] [ class>> ] tri
88 '[ [ _ _ ] dip ?set-at ] change-word-prop ;
90 : consult-methods ( consultation -- )
91 [ define-consult-method ] each-generic ;
93 : unregister-consult ( consultation -- )
94 [ class>> ] [ group>> ] bi
95 "protocol-consult" word-prop delete-at ;
97 : unconsult-method ( word consultation -- )
98 [ class>> swap first ?lookup-method ] keep
100 over "consultation" word-prop eq?
101 [ forget ] [ drop ] if
104 : unconsult-methods ( consultation -- )
105 [ unconsult-method ] each-generic ;
109 : define-consult ( consultation -- )
110 [ register-consult ] [ consult-methods ] bi ;
113 scan-word scan-word parse-definition <consultation>
114 [ save-location ] [ define-consult ] bi ;
117 scan-word scan-word parse-definition <broadcast>
118 [ save-location ] [ define-consult ] bi ;
120 M: consultation where loc>> ;
122 M: consultation set-where loc<< ;
124 M: consultation forget*
125 [ unconsult-methods ] [ unregister-consult ] bi ;
130 : forget-all-methods ( classes words -- )
131 [ first ?lookup-method forget ] cartesian-each ;
133 : protocol-users ( protocol -- users )
134 protocol-consult keys ;
136 : lost-words ( protocol wordlist -- lost-words )
137 [ protocol-words ] dip diff ;
139 : forget-old-definitions ( protocol new-wordlist -- )
140 [ drop protocol-users ] [ lost-words ] 2bi
143 : added-words ( protocol wordlist -- added-words )
144 swap protocol-words diff ;
146 : add-new-definitions ( protocol wordlist -- )
147 [ drop protocol-consult values ] [ added-words ] 2bi
148 [ swap define-consult-method ] cartesian-each ;
150 : initialize-protocol-props ( protocol wordlist -- )
152 drop "protocol-consult"
153 [ H{ } assoc-like ] change-word-prop
154 ] [ { } like "protocol-words" set-word-prop ] 2bi ;
156 : fill-in-depth ( wordlist -- wordlist' )
157 [ dup word? [ 0 2array ] when ] map ;
159 : show-words ( wordlist' -- wordlist )
160 [ dup second zero? [ first ] when ] map ;
162 : check-generic ( generic -- )
163 dup array? [ first ] when generic check-instance drop ;
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 ;
184 M: protocol definition protocol-words show-words ;
186 M: protocol definer drop \ PROTOCOL: \ ; ;
188 M: protocol group-words protocol-words ;
190 SYNTAX: SLOT-PROTOCOL:
192 [ [ reader-word ] [ writer-word ] bi 2array ]
193 map-tokens concat define-protocol ;