forked from factor/factor
/
slots.factor
239 lines (190 loc) · 6.4 KB
/
slots.factor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
! Copyright (C) 2005, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays byte-arrays kernel kernel.private math namespaces
make sequences strings effects generic generic.standard
classes classes.algebra slots.private combinators accessors
words sequences.private assocs alien quotations hashtables ;
IN: slots
TUPLE: slot-spec name offset class initial read-only ;
PREDICATE: reader < word "reader" word-prop ;
PREDICATE: reader-method < method "reading" word-prop ;
PREDICATE: writer < word "writer" word-prop ;
PREDICATE: writer-method < method "writing" word-prop ;
: <slot-spec> ( -- slot-spec )
slot-spec new
object bootstrap-word >>class ;
: define-typecheck ( class generic quot props -- )
[ create-method ] 2dip
[ [ props>> ] [ drop ] [ ] tri* assoc-union! drop ]
[ drop define ]
[ 2drop make-inline ]
3tri ;
GENERIC# reader-quot 1 ( class slot-spec -- quot )
M: object reader-quot
nip [
dup offset>> ,
\ slot ,
dup class>> object bootstrap-word eq?
[ drop ] [ class>> 1array , \ declare , ] if
] [ ] make ;
: reader-word ( name -- word )
">>" append "accessors" create
dup t "reader" set-word-prop ;
: reader-props ( slot-spec -- assoc )
"reading" associate ;
: define-reader-generic ( name -- )
reader-word (( object -- value )) define-simple-generic ;
: define-reader ( class slot-spec -- )
[ nip name>> define-reader-generic ]
[
{
[ drop ]
[ nip name>> reader-word ]
[ reader-quot ]
[ nip reader-props ]
} 2cleave define-typecheck
] 2bi ;
: writer-word ( name -- word )
"<<" append "accessors" create
dup t "writer" set-word-prop ;
ERROR: bad-slot-value value class ;
: (instance-check-quot) ( class -- quot )
[
\ dup ,
[ "predicate" word-prop % ]
[ [ bad-slot-value ] curry , ] bi
\ unless ,
] [ ] make ;
: instance-check-quot ( class -- quot )
{
{ [ dup object bootstrap-word eq? ] [ drop [ ] ] }
{ [ dup "coercer" word-prop ] [ "coercer" word-prop ] }
{ [ dup integer bootstrap-word eq? ] [ drop [ >integer ] ] }
[ (instance-check-quot) ]
} cond ;
GENERIC# writer-quot 1 ( class slot-spec -- quot )
M: object writer-quot
nip
[ class>> instance-check-quot dup empty? [ [ dip ] curry ] unless ]
[ offset>> [ set-slot ] curry ]
bi append ;
: writer-props ( slot-spec -- assoc )
"writing" associate ;
: define-writer-generic ( name -- )
writer-word (( value object -- )) define-simple-generic ;
: define-writer ( class slot-spec -- )
[ nip name>> define-writer-generic ] [
{
[ drop ]
[ nip name>> writer-word ]
[ writer-quot ]
[ nip writer-props ]
} 2cleave define-typecheck
] 2bi ;
: setter-word ( name -- word )
">>" prepend "accessors" create ;
: define-setter ( name -- )
dup setter-word dup deferred? [
[ \ over , swap writer-word , ] [ ] make
(( object value -- object )) define-inline
] [ 2drop ] if ;
: changer-word ( name -- word )
"change-" prepend "accessors" create ;
: define-changer ( name -- )
dup changer-word dup deferred? [
[
\ over ,
over reader-word 1quotation
[ dip call ] curry [ ] like [ dip swap ] curry %
swap setter-word ,
] [ ] make (( object quot -- object )) define-inline
] [ 2drop ] if ;
: define-slot-methods ( class slot-spec -- )
[ define-reader ]
[
dup read-only>> [ 2drop ] [
[ name>> define-setter drop ]
[ name>> define-changer drop ]
[ define-writer ]
2tri
] if
] 2bi ;
: define-accessors ( class specs -- )
[ define-slot-methods ] with each ;
: define-protocol-slot ( name -- )
{
[ define-reader-generic ]
[ define-writer-generic ]
[ define-setter ]
[ define-changer ]
} cleave ;
ERROR: no-initial-value class ;
GENERIC: initial-value* ( class -- object )
M: class initial-value* no-initial-value ;
: initial-value ( class -- object )
{
{ [ dup "initial-value" word-prop ] [ dup "initial-value" word-prop ] }
{ [ \ f bootstrap-word over class<= ] [ f ] }
{ [ \ array-capacity bootstrap-word over class<= ] [ 0 ] }
{ [ float bootstrap-word over class<= ] [ 0.0 ] }
{ [ string bootstrap-word over class<= ] [ "" ] }
{ [ array bootstrap-word over class<= ] [ { } ] }
{ [ byte-array bootstrap-word over class<= ] [ B{ } ] }
{ [ pinned-alien bootstrap-word over class<= ] [ <bad-alien> ] }
{ [ quotation bootstrap-word over class<= ] [ [ ] ] }
[ dup initial-value* ]
} cond nip ;
GENERIC: make-slot ( desc -- slot-spec )
M: string make-slot
<slot-spec>
swap >>name ;
: peel-off-name ( slot-spec array -- slot-spec array )
[ first >>name ] [ rest ] bi ; inline
: peel-off-class ( slot-spec array -- slot-spec array )
dup empty? [
dup first class? [
[ first >>class ] [ rest ] bi
] when
] unless ;
ERROR: bad-slot-attribute key ;
: peel-off-attributes ( slot-spec array -- slot-spec array )
dup empty? [
unclip {
{ initial: [ [ first >>initial ] [ rest ] bi ] }
{ read-only [ [ t >>read-only ] dip ] }
[ bad-slot-attribute ]
} case
] unless ;
ERROR: bad-initial-value name ;
: check-initial-value ( slot-spec -- slot-spec )
dup initial>> [
[ ] [
dup [ initial>> ] [ class>> ] bi instance?
[ name>> bad-initial-value ] unless
] if-bootstrapping
] [
dup class>> initial-value >>initial
] if ;
M: array make-slot
<slot-spec>
swap
peel-off-name
peel-off-class
[ dup empty? ] [ peel-off-attributes ] until drop
check-initial-value ;
M: slot-spec make-slot
check-initial-value ;
: make-slots ( slots -- specs )
[ make-slot ] map ;
: finalize-slots ( specs base -- specs )
over length iota [ + ] with map [ >>offset ] 2map ;
: slot-named* ( name specs -- offset spec/f )
[ name>> = ] with find ;
: slot-named ( name specs -- spec/f )
slot-named* nip ;
! Predefine some slots, because there are change-* words in other vocabs
! that nondeterministically cause ambiguities when USEd alongside
! accessors
SLOT: at
SLOT: nth
SLOT: global