1 ! Copyright (C) 2005, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien arrays assocs byte-arrays classes
4 classes.algebra classes.algebra.private classes.maybe
5 combinators generic generic.standard hashtables kernel
6 kernel.private make math quotations sequences sequences.private
7 slots.private strings words ;
10 TUPLE: slot-spec name offset class initial read-only ;
12 PREDICATE: reader < word "reader" word-prop ;
14 PREDICATE: reader-method < method "reading" word-prop >boolean ;
16 PREDICATE: writer < word "writer" word-prop ;
18 PREDICATE: writer-method < method "writing" word-prop >boolean ;
20 : <slot-spec> ( -- slot-spec )
22 object bootstrap-word >>class ;
24 : define-typecheck ( class generic quot props -- )
25 [ create-method ] 2dip
26 [ [ props>> ] [ drop ] [ ] tri* assoc-union! drop ]
31 GENERIC# reader-quot 1 ( class slot-spec -- quot )
37 dup class>> object bootstrap-word eq?
38 [ drop ] [ class>> 1array , \ declare , ] if
41 : reader-word ( name -- word )
42 ">>" append "accessors" create-word
43 dup t "reader" set-word-prop ;
45 : reader-props ( slot-spec -- assoc )
48 : define-reader-generic ( name -- )
49 reader-word ( object -- value ) define-simple-generic ;
51 : define-reader ( class slot-spec -- )
52 [ nip name>> define-reader-generic ]
56 [ nip name>> reader-word ]
59 } 2cleave define-typecheck
62 : writer-word ( name -- word )
63 "<<" append "accessors" create-word
64 dup t "writer" set-word-prop ;
66 ERROR: bad-slot-value value class ;
68 GENERIC: instance-check-quot ( obj -- quot )
70 M: class instance-check-quot ( class -- quot )
72 { [ dup object bootstrap-word eq? ] [ drop [ ] ] }
73 { [ dup "coercer" word-prop ] [ "coercer" word-prop ] }
77 M: object instance-check-quot
81 [ [ bad-slot-value ] curry , ] bi
85 GENERIC# writer-quot 1 ( class slot-spec -- quot )
89 [ class>> instance-check-quot dup empty? [ [ dip ] curry ] unless ]
90 [ offset>> [ set-slot ] curry ]
93 : writer-props ( slot-spec -- assoc )
96 : define-writer-generic ( name -- )
97 writer-word ( value object -- ) define-simple-generic ;
99 : define-writer ( class slot-spec -- )
100 [ nip name>> define-writer-generic ] [
103 [ nip name>> writer-word ]
106 } 2cleave define-typecheck
109 : setter-word ( name -- word )
110 ">>" prepend "accessors" create-word ;
112 : define-setter ( name -- )
113 dup setter-word dup deferred? [
114 [ \ over , swap writer-word , ] [ ] make
115 ( object value -- object ) define-inline
118 : changer-word ( name -- word )
119 "change-" prepend "accessors" create-word ;
121 : define-changer ( name -- )
122 dup changer-word dup deferred? [
125 over reader-word 1quotation
126 [ dip call ] curry [ ] like [ dip swap ] curry %
128 ] [ ] make ( object quot -- object ) define-inline
131 : define-slot-methods ( class slot-spec -- )
134 dup read-only>> [ 2drop ] [
135 [ name>> define-setter drop ]
136 [ name>> define-changer drop ]
142 : define-accessors ( class specs -- )
143 [ define-slot-methods ] with each ;
145 : define-protocol-slot ( name -- )
147 [ define-reader-generic ]
148 [ define-writer-generic ]
155 GENERIC: initial-value* ( class -- object ? )
157 M: class initial-value* drop f f ;
159 M: maybe initial-value* drop f t ;
161 ! Default initial value is f, 0, or the default initial value of
162 ! the smallest class. Special case 0 because float is ostensibly
163 ! smaller than integer in union{ integer float } because of
164 ! alphabetical sorting.
165 M: anonymous-union initial-value*
167 { [ f over instance? ] [ drop f t ] }
168 { [ 0 over instance? ] [ drop 0 t ] }
170 members>> sort-classes [ initial-value ] { } map>assoc
171 ?last [ second t ] [ f f ] if*
175 ! See if any of the initial values fit the intersection class,
176 ! or else return that none do, and leave it up to the user to
177 ! provide an initial: value.
178 M: anonymous-intersection initial-value*
180 { [ f over instance? ] [ drop f t ] }
181 { [ 0 over instance? ] [ drop 0 t ] }
184 [ participants>> sort-classes [ initial-value ] { } map>assoc ]
187 [ [ first2 nip ] dip instance? ] curry find swap [
195 : initial-value ( class -- object ? )
197 { [ dup only-classoid? ] [ dup initial-value* ] }
198 { [ dup "initial-value" word-prop ] [ dup "initial-value" word-prop t ] }
199 { [ \ f bootstrap-word over class<= ] [ f t ] }
200 { [ \ array-capacity bootstrap-word over class<= ] [ 0 t ] }
201 { [ bignum bootstrap-word over class<= ] [ 0 >bignum t ] }
202 { [ float bootstrap-word over class<= ] [ 0.0 t ] }
203 { [ string bootstrap-word over class<= ] [ "" t ] }
204 { [ array bootstrap-word over class<= ] [ { } t ] }
205 { [ byte-array bootstrap-word over class<= ] [ B{ } t ] }
206 { [ pinned-alien bootstrap-word over class<= ] [ <bad-alien> t ] }
207 { [ quotation bootstrap-word over class<= ] [ [ ] t ] }
208 [ dup initial-value* ]
209 } cond [ drop ] 2dip ;
211 GENERIC: make-slot ( desc -- slot-spec )
217 : peel-off-name ( slot-spec array -- slot-spec array )
218 [ first >>name ] [ rest ] bi ; inline
220 : init-slot-class ( slot-spec class -- slot-spec )
221 [ >>class ] [ initial-value [ >>initial ] [ drop ] if ] bi ;
223 : peel-off-class ( slot-spec array -- slot-spec array )
225 dup first classoid? [
226 [ first init-slot-class ]
232 ERROR: bad-slot-attribute key ;
234 : peel-off-attributes ( slot-spec array -- slot-spec array )
237 { initial: [ [ first >>initial ] [ rest ] bi ] }
238 { read-only [ [ t >>read-only ] dip ] }
239 [ bad-slot-attribute ]
243 ERROR: bad-initial-value name initial-value class ;
245 : check-initial-value ( slot-spec -- slot-spec )
247 [ ] [ initial>> ] [ class>> ] tri
251 [ name>> ] 2dip bad-initial-value
260 [ dup empty? ] [ peel-off-attributes ] until drop
261 check-initial-value ;
263 M: slot-spec make-slot
264 check-initial-value ;
266 : make-slots ( slots -- specs )
269 : finalize-slots ( specs base -- specs )
270 over length iota [ + ] with map [ >>offset ] 2map ;
272 : slot-named* ( name specs -- offset spec/f )
273 [ name>> = ] with find ;
275 : slot-named ( name specs -- spec/f )
278 ! Predefine some slots, because there are change-* words in other vocabs
279 ! that nondeterministically cause ambiguities when USEd alongside