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 "consultation" word-prop ;
59 M: consult-method reset-word
60 [ call-next-method ] [ f "consultation" set-word-prop ] bi ;
62 GENERIC# (consult-method-quot) 2 ( consultation quot word -- object )
64 M: consultation (consult-method-quot)
65 '[ _ call _ execute ] nip ;
66 M: broadcast (consult-method-quot)
67 '[ _ call [ _ execute ] each ] nip ;
69 : consult-method-quot ( consultation word -- object )
71 [ second [ [ dip ] curry ] times ] [ first ] bi
72 (consult-method-quot) ;
74 : consult-method ( word consultation -- )
75 [ create-consult-method ]
76 [ swap consult-method-quot ] 2bi
79 : change-word-prop ( word prop quot -- )
80 [ swap props>> ] dip change-at ; inline
82 : each-generic ( consultation quot -- )
83 [ [ group>> group-words ] keep ] dip curry each ; inline
85 : register-consult ( consultation -- )
86 [ group>> \ protocol-consult ] [ ] [ class>> ] tri
87 '[ [ _ _ ] dip ?set-at ] change-word-prop ;
89 : consult-methods ( consultation -- )
90 [ consult-method ] each-generic ;
92 : unregister-consult ( consultation -- )
93 [ class>> ] [ group>> ] bi
94 \ protocol-consult word-prop delete-at ;
96 : unconsult-method ( word consultation -- )
97 [ class>> swap first method ] keep
99 over "consultation" word-prop eq?
100 [ forget ] [ drop ] if
103 : unconsult-methods ( consultation -- )
104 [ unconsult-method ] each-generic ;
108 : define-consult ( consultation -- )
109 [ register-consult ] [ consult-methods ] bi ;
112 scan-word scan-word parse-definition <consultation>
113 [ save-location ] [ define-consult ] bi ;
116 scan-word scan-word parse-definition <broadcast>
117 [ save-location ] [ define-consult ] bi ;
119 M: consultation where loc>> ;
121 M: consultation set-where loc<< ;
123 M: consultation forget*
124 [ unconsult-methods ] [ unregister-consult ] bi ;
129 : forget-all-methods ( classes words -- )
130 [ first method forget ] cartesian-each ;
132 : protocol-users ( protocol -- users )
133 protocol-consult keys ;
135 : lost-words ( protocol wordlist -- lost-words )
136 [ protocol-words ] dip diff ;
138 : forget-old-definitions ( protocol new-wordlist -- )
139 [ drop protocol-users ] [ lost-words ] 2bi
142 : added-words ( protocol wordlist -- added-words )
143 swap protocol-words diff ;
145 : add-new-definitions ( protocol wordlist -- )
146 [ drop protocol-consult values ] [ added-words ] 2bi
147 [ swap consult-method ] cartesian-each ;
149 : initialize-protocol-props ( protocol wordlist -- )
151 drop \ protocol-consult
152 [ H{ } assoc-like ] change-word-prop
153 ] [ { } like \ protocol-words set-word-prop ] 2bi ;
155 : fill-in-depth ( wordlist -- wordlist' )
156 [ dup word? [ 0 2array ] when ] map ;
158 : show-words ( wordlist' -- wordlist )
159 [ dup second zero? [ first ] when ] map ;
163 : define-protocol ( protocol wordlist -- )
164 [ drop define-symbol ] [
166 [ forget-old-definitions ]
167 [ add-new-definitions ]
168 [ initialize-protocol-props ] 2tri
172 CREATE-WORD parse-definition define-protocol ;
174 PREDICATE: protocol < word protocol-words ; ! Subclass of symbol?
177 [ f forget-old-definitions ] [ call-next-method ] bi ;
180 M: protocol definition protocol-words show-words ;
182 M: protocol definer drop \ PROTOCOL: \ ; ;
184 M: protocol group-words protocol-words ;
186 SYNTAX: SLOT-PROTOCOL:
188 [ [ reader-word ] [ writer-word ] bi 2array ]
189 map-tokens concat define-protocol ;