1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays byte-arrays kernel kernel.private math namespaces
4 make sequences strings effects generic generic.standard
5 classes classes.algebra slots.private combinators accessors
6 words sequences.private assocs alien quotations hashtables ;
9 TUPLE: slot-spec name offset class initial read-only ;
11 PREDICATE: reader < word "reader" word-prop ;
13 PREDICATE: reader-method < method "reading" word-prop ;
15 PREDICATE: writer < word "writer" word-prop ;
17 PREDICATE: writer-method < method "writing" word-prop ;
19 : <slot-spec> ( -- slot-spec )
21 object bootstrap-word >>class ;
23 : define-typecheck ( class generic quot props -- )
24 [ create-method ] 2dip
25 [ [ props>> ] [ drop ] [ ] tri* update ]
30 GENERIC# reader-quot 1 ( class slot-spec -- quot )
36 dup class>> object bootstrap-word eq?
37 [ drop ] [ class>> 1array , \ declare , ] if
40 : reader-word ( name -- word )
41 ">>" append "accessors" create
42 dup t "reader" set-word-prop ;
44 : reader-props ( slot-spec -- assoc )
47 : define-reader-generic ( name -- )
48 reader-word (( object -- value )) define-simple-generic ;
50 : define-reader ( class slot-spec -- )
51 [ nip name>> define-reader-generic ]
55 [ nip name>> reader-word ]
58 } 2cleave define-typecheck
61 : writer-word ( name -- word )
62 "(>>" ")" surround "accessors" create
63 dup t "writer" set-word-prop ;
65 ERROR: bad-slot-value value class ;
67 : (instance-check-quot) ( class -- quot )
70 [ "predicate" word-prop % ]
71 [ [ bad-slot-value ] curry , ] bi
75 : instance-check-quot ( class -- quot )
77 { [ dup object bootstrap-word eq? ] [ drop [ ] ] }
78 { [ dup "coercer" word-prop ] [ "coercer" word-prop ] }
79 { [ dup integer bootstrap-word eq? ] [ drop [ >integer ] ] }
80 [ (instance-check-quot) ]
83 GENERIC# writer-quot 1 ( class slot-spec -- quot )
87 [ class>> instance-check-quot dup empty? [ [ dip ] curry ] unless ]
88 [ offset>> [ set-slot ] curry ]
91 : writer-props ( slot-spec -- assoc )
94 : define-writer-generic ( name -- )
95 writer-word (( value object -- )) define-simple-generic ;
97 : define-writer ( class slot-spec -- )
98 [ nip name>> define-writer-generic ] [
101 [ nip name>> writer-word ]
104 } 2cleave define-typecheck
107 : setter-word ( name -- word )
108 ">>" prepend "accessors" create ;
110 : define-setter ( name -- )
111 dup setter-word dup deferred? [
112 [ \ over , swap writer-word , ] [ ] make
113 (( object value -- object )) define-inline
116 : changer-word ( name -- word )
117 "change-" prepend "accessors" create ;
119 : define-changer ( name -- )
120 dup changer-word dup deferred? [
123 over reader-word 1quotation
124 [ dip call ] curry [ ] like [ dip swap ] curry %
126 ] [ ] make (( object quot -- object )) define-inline
129 : define-slot-methods ( class slot-spec -- )
132 dup read-only>> [ 2drop ] [
133 [ name>> define-setter drop ]
134 [ name>> define-changer drop ]
140 : define-accessors ( class specs -- )
141 [ define-slot-methods ] with each ;
143 : define-protocol-slot ( name -- )
145 [ define-reader-generic ]
146 [ define-writer-generic ]
151 ERROR: no-initial-value class ;
153 GENERIC: initial-value* ( class -- object )
155 M: class initial-value* no-initial-value ;
157 : initial-value ( class -- object )
159 { [ dup "initial-value" word-prop ] [ dup "initial-value" word-prop ] }
160 { [ \ f bootstrap-word over class<= ] [ f ] }
161 { [ \ array-capacity bootstrap-word over class<= ] [ 0 ] }
162 { [ float bootstrap-word over class<= ] [ 0.0 ] }
163 { [ string bootstrap-word over class<= ] [ "" ] }
164 { [ array bootstrap-word over class<= ] [ { } ] }
165 { [ byte-array bootstrap-word over class<= ] [ B{ } ] }
166 { [ pinned-alien bootstrap-word over class<= ] [ <bad-alien> ] }
167 { [ quotation bootstrap-word over class<= ] [ [ ] ] }
168 [ dup initial-value* ]
171 GENERIC: make-slot ( desc -- slot-spec )
177 : peel-off-name ( slot-spec array -- slot-spec array )
178 [ first >>name ] [ rest ] bi ; inline
180 : peel-off-class ( slot-spec array -- slot-spec array )
183 [ first >>class ] [ rest ] bi
187 ERROR: bad-slot-attribute key ;
189 : peel-off-attributes ( slot-spec array -- slot-spec array )
192 { initial: [ [ first >>initial ] [ rest ] bi ] }
193 { read-only [ [ t >>read-only ] dip ] }
194 [ bad-slot-attribute ]
198 ERROR: bad-initial-value name ;
200 : check-initial-value ( slot-spec -- slot-spec )
203 dup [ initial>> ] [ class>> ] bi instance?
204 [ name>> bad-initial-value ] unless
207 dup class>> initial-value >>initial
215 [ dup empty? ] [ peel-off-attributes ] until drop
216 check-initial-value ;
218 M: slot-spec make-slot
219 check-initial-value ;
221 : make-slots ( slots -- specs )
224 : finalize-slots ( specs base -- specs )
225 over length iota [ + ] with map [ >>offset ] 2map ;
227 : slot-named* ( name specs -- offset spec/f )
228 [ name>> = ] with find ;
230 : slot-named ( name specs -- spec/f )