]> gitweb.factorcode.org Git - factor.git/blob - basis/delegate/delegate.factor
factor: Rename GENERIC# to GENERIC#:.
[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 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
7 compiler.units make ;
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: broadcast < consultation ;
48
49 : <consultation> ( group class quot -- consultation )
50     f consultation boa ;
51
52 : <broadcast> ( group class quot -- consultation )
53     [ check-broadcast-group ] 2dip f broadcast boa ;
54
55 : create-consult-method ( word consultation -- method )
56     [ class>> swap first create-method dup fake-definition ] keep
57     [ drop ] [ "consultation" set-word-prop ] 2bi ;
58
59 PREDICATE: consult-method < method
60     "consultation" word-prop >boolean ;
61
62 M: consult-method reset-word
63     [ call-next-method ] [ f "consultation" set-word-prop ] bi ;
64
65 GENERIC#: (consult-method-quot) 2 ( consultation quot word -- object )
66
67 M: consultation (consult-method-quot)
68     '[ _ call _ execute ] nip ;
69
70 M: broadcast (consult-method-quot)
71     '[ _ call [ _ execute ] each ] nip ;
72
73 : consult-method-quot ( consultation word -- object )
74     [ dup quot>> ] dip
75     [ second [ [ dip ] curry ] times ] [ first ] bi
76     (consult-method-quot) ;
77
78 : define-consult-method ( word consultation -- )
79     [ create-consult-method ]
80     [ swap consult-method-quot ] 2bi
81     define ;
82
83 : each-generic ( consultation quot -- )
84     [ [ group>> group-words ] keep ] dip curry each ; inline
85
86 : register-consult ( consultation -- )
87     [ group>> "protocol-consult" ] [ ] [ class>> ] tri
88     '[ [ _ _ ] dip ?set-at ] change-word-prop ;
89
90 : consult-methods ( consultation -- )
91     [ define-consult-method ] each-generic ;
92
93 : unregister-consult ( consultation -- )
94     [ class>> ] [ group>> ] bi
95     "protocol-consult" word-prop delete-at ;
96
97 : unconsult-method ( word consultation -- )
98     [ class>> swap first ?lookup-method ] keep
99     over [
100         over "consultation" word-prop eq?
101         [ forget ] [ drop ] if
102     ] [ 2drop ] if ;
103
104 : unconsult-methods ( consultation -- )
105     [ unconsult-method ] each-generic ;
106
107 PRIVATE>
108
109 : define-consult ( consultation -- )
110     [ register-consult ] [ consult-methods ] bi ;
111
112 SYNTAX: CONSULT:
113     scan-word scan-word parse-definition <consultation>
114     [ save-location ] [ define-consult ] bi ;
115
116 SYNTAX: BROADCAST:
117     scan-word scan-word parse-definition <broadcast>
118     [ save-location ] [ define-consult ] bi ;
119
120 M: consultation where loc>> ;
121
122 M: consultation set-where loc<< ;
123
124 M: consultation forget*
125     [ unconsult-methods ] [ unregister-consult ] bi ;
126
127 ! Protocols
128 <PRIVATE
129
130 : forget-all-methods ( classes words -- )
131     [ first ?lookup-method forget ] cartesian-each ;
132
133 : protocol-users ( protocol -- users )
134     protocol-consult keys ;
135
136 : lost-words ( protocol wordlist -- lost-words )
137     [ protocol-words ] dip diff ;
138
139 : forget-old-definitions ( protocol new-wordlist -- )
140     [ drop protocol-users ] [ lost-words ] 2bi
141     forget-all-methods ;
142
143 : added-words ( protocol wordlist -- added-words )
144     swap protocol-words diff ;
145
146 : add-new-definitions ( protocol wordlist -- )
147     [ drop protocol-consult values ] [ added-words ] 2bi
148     [ swap define-consult-method ] cartesian-each ;
149
150 : initialize-protocol-props ( protocol wordlist -- )
151     [
152         drop "protocol-consult"
153         [ H{ } assoc-like ] change-word-prop
154     ] [ { } like "protocol-words" set-word-prop ] 2bi ;
155
156 : fill-in-depth ( wordlist -- wordlist' )
157     [ dup word? [ 0 2array ] when ] map ;
158
159 : show-words ( wordlist' -- wordlist )
160     [ dup second zero? [ first ] when ] map ;
161
162 ERROR: not-a-generic word ;
163
164 : check-generic ( generic -- )
165     dup array? [ first ] when
166     dup generic? [ drop ] [ not-a-generic ] if ;
167
168 PRIVATE>
169
170 : define-protocol ( protocol wordlist -- )
171     dup [ check-generic ] each
172     [ drop define-symbol ] [
173         fill-in-depth
174         [ forget-old-definitions ]
175         [ add-new-definitions ]
176         [ initialize-protocol-props ] 2tri
177     ] 2bi ;
178
179 SYNTAX: PROTOCOL:
180     scan-new-word parse-definition define-protocol ;
181
182 PREDICATE: protocol < word protocol-words ; ! Subclass of symbol?
183
184 M: protocol forget*
185     [ f forget-old-definitions ] [ call-next-method ] bi ;
186
187 M: protocol definition protocol-words show-words ;
188
189 M: protocol definer drop \ PROTOCOL: \ ; ;
190
191 M: protocol group-words protocol-words ;
192
193 SYNTAX: SLOT-PROTOCOL:
194     scan-new-word ";"
195     [ [ reader-word ] [ writer-word ] bi 2array ]
196     map-tokens concat define-protocol ;