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