]> gitweb.factorcode.org Git - factor.git/blob - basis/delegate/delegate.factor
Add HOOK-CONSULT: to delegate
[factor.git] / basis / delegate / delegate.factor
1 ! Copyright (C) 2007, 2008 Daniel Ehrenberg
2 ! Portions copyright (C) 2009, 2010 Slava Pestov, Joe Groff
3 ! See https://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 namespaces parser sequences
7 sets slots words words.symbol ;
8 IN: delegate
9
10 ERROR: broadcast-words-must-have-no-outputs group ;
11
12 <PRIVATE
13
14 : protocol-words ( protocol -- words )
15     "protocol-words" word-prop ;
16
17 : protocol-consult ( protocol -- consulters )
18     "protocol-consult" word-prop ;
19
20 GENERIC: group-words ( group -- words )
21
22 M: standard-generic group-words
23     dup "combination" word-prop #>> 2array 1array ;
24
25 : slot-words, ( slot-spec -- )
26     [ name>> reader-word 0 2array , ]
27     [
28         dup read-only>> [ drop ] [
29             name>> writer-word 0 2array ,
30         ] if
31     ] bi ;
32
33 : slot-group-words ( slots -- words )
34     [ [ slot-words, ] each ] { } make ;
35
36 M: tuple-class group-words
37     all-slots slot-group-words ;
38
39 : check-broadcast-group ( group -- group )
40     dup group-words [ first stack-effect out>> empty? ] all?
41     [ broadcast-words-must-have-no-outputs ] unless ;
42
43 ! Consultation
44
45 TUPLE: consultation group class quot loc ;
46
47 TUPLE: hook-consultation < consultation hook-var ;
48
49 TUPLE: broadcast < consultation ;
50
51 : <consultation> ( group class quot -- consultation )
52     f consultation boa ;
53
54 : <broadcast> ( group class quot -- consultation )
55     [ check-broadcast-group ] 2dip f broadcast boa ;
56
57 :: <hook-consultation> ( group class var quot -- hook-consultation )
58     hook-consultation new
59     group >>group
60     class >>class
61     quot >>quot
62     var >>hook-var ;
63
64 : create-consult-method ( word consultation -- method )
65     [ class>> swap first create-method dup fake-definition ] keep
66     [ drop ] [ "consultation" set-word-prop ] 2bi ;
67
68 PREDICATE: consult-method < method
69     "consultation" word-prop >boolean ;
70
71 M: consult-method reset-word
72     [ call-next-method ] [ "consultation" remove-word-prop ] bi ;
73
74 GENERIC#: (consult-method-quot) 2 ( consultation quot word -- object )
75
76 M: consultation (consult-method-quot)
77     '[ _ call _ execute ] nip ;
78
79 M: broadcast (consult-method-quot)
80     '[ _ call [ _ execute ] each ] nip ;
81
82 M:: hook-consultation (consult-method-quot) ( consultation quot word -- object )
83     [ quot call consultation hook-var>> [ word execute ] with-variable ] ;
84
85 : consult-method-quot ( consultation word -- object )
86     [ dup quot>> ] dip
87     [ second [ [ dip ] curry ] times ] [ first ] bi
88     (consult-method-quot) ;
89
90 : define-consult-method ( word consultation -- )
91     [ create-consult-method ]
92     [ swap consult-method-quot ] 2bi
93     define ;
94
95 : each-generic ( consultation quot -- )
96     [ [ group>> group-words ] keep ] dip curry each ; inline
97
98 : register-consult ( consultation -- )
99     [ group>> "protocol-consult" ] [ ] [ class>> ] tri
100     '[ [ _ _ ] dip ?set-at ] change-word-prop ;
101
102 : consult-methods ( consultation -- )
103     [ define-consult-method ] each-generic ;
104
105 : unregister-consult ( consultation -- )
106     [ class>> ] [ group>> ] bi
107     "protocol-consult" word-prop delete-at ;
108
109 : unconsult-method ( word consultation -- )
110     [ class>> swap first ?lookup-method ] keep
111     over [
112         over "consultation" word-prop eq?
113         [ forget ] [ drop ] if
114     ] [ 2drop ] if ;
115
116 : unconsult-methods ( consultation -- )
117     [ unconsult-method ] each-generic ;
118
119 PRIVATE>
120
121 : define-consult ( consultation -- )
122     [ register-consult ] [ consult-methods ] bi ;
123
124 SYNTAX: CONSULT:
125     scan-word scan-word parse-definition <consultation>
126     [ save-location ] [ define-consult ] bi ;
127
128 SYNTAX: HOOK-CONSULT:
129     scan-word scan-word scan-word parse-definition <hook-consultation>
130     [ save-location ] [ define-consult ] bi ;
131
132 SYNTAX: BROADCAST:
133     scan-word scan-word parse-definition <broadcast>
134     [ save-location ] [ define-consult ] bi ;
135
136 M: consultation where loc>> ;
137
138 M: consultation set-where loc<< ;
139
140 M: consultation forget*
141     [ unconsult-methods ] [ unregister-consult ] bi ;
142
143 ! Protocols
144 <PRIVATE
145
146 : forget-all-methods ( classes words -- )
147     [ first ?lookup-method forget ] cartesian-each ;
148
149 : protocol-users ( protocol -- users )
150     protocol-consult keys ;
151
152 : lost-words ( protocol wordlist -- lost-words )
153     [ protocol-words ] dip diff ;
154
155 : forget-old-definitions ( protocol new-wordlist -- )
156     [ drop protocol-users ] [ lost-words ] 2bi
157     forget-all-methods ;
158
159 : added-words ( protocol wordlist -- added-words )
160     swap protocol-words diff ;
161
162 : add-new-definitions ( protocol wordlist -- )
163     [ drop protocol-consult values ] [ added-words ] 2bi
164     [ swap define-consult-method ] cartesian-each ;
165
166 : initialize-protocol-props ( protocol wordlist -- )
167     [
168         drop "protocol-consult"
169         [ H{ } assoc-like ] change-word-prop
170     ] [ { } like "protocol-words" set-word-prop ] 2bi ;
171
172 : fill-in-depth ( wordlist -- wordlist' )
173     [ dup word? [ 0 2array ] when ] map ;
174
175 : show-words ( wordlist' -- wordlist )
176     [ dup second zero? [ first ] when ] map ;
177
178 : check-generic ( generic -- )
179     dup array? [ first ] when generic check-instance drop ;
180
181 PRIVATE>
182
183 : define-protocol ( protocol wordlist -- )
184     dup [ check-generic ] each
185     [ drop define-symbol ] [
186         fill-in-depth
187         [ forget-old-definitions ]
188         [ add-new-definitions ]
189         [ initialize-protocol-props ] 2tri
190     ] 2bi ;
191
192 SYNTAX: PROTOCOL:
193     scan-new-word parse-definition define-protocol ;
194
195 PREDICATE: protocol < word protocol-words ; ! Subclass of symbol?
196
197 M: protocol forget*
198     [ f forget-old-definitions ] [ call-next-method ] bi ;
199
200 M: protocol definition protocol-words show-words ;
201
202 M: protocol definer drop \ PROTOCOL: \ ; ;
203
204 M: protocol group-words protocol-words ;
205
206 SYNTAX: SLOT-PROTOCOL:
207     scan-new-word ";"
208     [ [ reader-word ] [ writer-word ] bi 2array ]
209     map-tokens concat define-protocol ;