factor/core/slots/slots.factor

216 lines
5.8 KiB
Factor
Raw Normal View History

2008-01-28 19:15:21 -05:00
! Copyright (C) 2005, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: arrays byte-arrays kernel kernel.private math namespaces
make sequences strings words effects generic generic.standard
classes classes.algebra slots.private combinators accessors
words sequences.private assocs alien ;
2007-09-20 18:09:08 -04:00
IN: slots
TUPLE: slot-spec name offset class initial read-only ;
2007-09-20 18:09:08 -04:00
2008-07-25 03:07:45 -04:00
PREDICATE: reader < word "reader" word-prop ;
PREDICATE: writer < word "writer" word-prop ;
: <slot-spec> ( -- slot-spec )
slot-spec new
object bootstrap-word >>class ;
2007-09-20 18:09:08 -04:00
2008-06-29 22:37:57 -04:00
: define-typecheck ( class generic quot props -- )
[ dup define-simple-generic create-method ] 2dip
2008-07-25 03:07:45 -04:00
[ [ props>> ] [ drop ] [ ] tri* update ]
2008-06-29 22:37:57 -04:00
[ drop define ]
3bi ;
2007-09-20 18:09:08 -04:00
: create-accessor ( name effect -- word )
>r "accessors" create dup r>
"declared-effect" set-word-prop ;
2008-06-29 22:37:57 -04:00
: reader-quot ( slot-spec -- quot )
2007-09-20 18:09:08 -04:00
[
2008-06-29 22:37:57 -04:00
dup offset>> ,
2007-09-20 18:09:08 -04:00
\ slot ,
2008-06-29 22:37:57 -04:00
dup class>> object bootstrap-word eq?
[ drop ] [ class>> 1array , \ declare , ] if
2007-09-20 18:09:08 -04:00
] [ ] make ;
2008-03-20 16:30:59 -04:00
: reader-word ( name -- word )
2008-07-25 03:07:45 -04:00
">>" append (( object -- value )) create-accessor
dup t "reader" set-word-prop ;
2008-03-20 16:30:59 -04:00
2008-07-25 03:07:45 -04:00
: reader-props ( slot-spec -- assoc )
[
[ "reading" set ]
[ read-only>> [ t "foldable" set ] when ] bi
t "flushable" set
] H{ } make-assoc ;
2008-06-29 22:37:57 -04:00
: define-reader ( class slot-spec -- )
2008-06-29 22:37:57 -04:00
[ name>> reader-word ] [ reader-quot ] [ reader-props ] tri
define-typecheck ;
2008-03-20 16:30:59 -04:00
: writer-word ( name -- word )
2008-07-25 03:07:45 -04:00
"(>>" swap ")" 3append (( value object -- )) create-accessor
dup t "writer" set-word-prop ;
2008-03-20 16:30:59 -04:00
2008-06-30 02:44:58 -04:00
ERROR: bad-slot-value value class ;
2008-06-28 00:10:19 -04:00
2008-06-29 22:37:57 -04:00
: writer-quot/object ( slot-spec -- )
offset>> , \ set-slot , ;
2008-06-29 22:37:57 -04:00
: writer-quot/coerce ( slot-spec -- )
[ \ >r , class>> "coercer" word-prop % \ r> , ]
[ offset>> , \ set-slot , ]
bi ;
2008-06-29 22:37:57 -04:00
: writer-quot/check ( slot-spec -- )
[ offset>> , ]
[
\ pick ,
2008-06-30 02:44:58 -04:00
dup class>> "predicate" word-prop %
[ set-slot ] ,
class>> [ 2nip bad-slot-value ] curry [ ] like ,
\ if ,
2008-06-29 22:37:57 -04:00
]
bi ;
2008-06-29 22:37:57 -04:00
: writer-quot/fixnum ( slot-spec -- )
[ >r >fixnum r> ] % writer-quot/check ;
2008-06-29 22:37:57 -04:00
: writer-quot ( slot-spec -- quot )
2008-06-28 00:10:19 -04:00
[
{
2008-06-29 22:37:57 -04:00
{ [ dup class>> object bootstrap-word eq? ] [ writer-quot/object ] }
{ [ dup class>> "coercer" word-prop ] [ writer-quot/coerce ] }
{ [ dup class>> fixnum bootstrap-word class<= ] [ writer-quot/fixnum ] }
[ writer-quot/check ]
} cond
2008-06-28 00:10:19 -04:00
] [ ] make ;
2008-07-25 03:07:45 -04:00
: writer-props ( slot-spec -- assoc )
[ "writing" set ] H{ } make-assoc ;
: define-writer ( class slot-spec -- )
2008-07-25 03:07:45 -04:00
[ name>> writer-word ] [ writer-quot ] [ writer-props ] tri
define-typecheck ;
2008-03-20 16:30:59 -04:00
: setter-word ( name -- word )
2008-06-08 16:32:55 -04:00
">>" prepend (( object value -- object )) create-accessor ;
2008-03-20 16:30:59 -04:00
: define-setter ( name -- )
dup setter-word dup deferred? [
2008-03-20 16:30:59 -04:00
[ \ over , swap writer-word , ] [ ] make define-inline
] [ 2drop ] if ;
: changer-word ( name -- word )
2008-06-08 16:32:55 -04:00
"change-" prepend (( object quot -- object )) create-accessor ;
2008-03-20 16:30:59 -04:00
: define-changer ( name -- )
dup changer-word dup deferred? [
2008-03-20 16:30:59 -04:00
[
[ over >r >r ] %
over reader-word ,
[ r> call r> swap ] %
swap setter-word ,
] [ ] make define-inline
] [ 2drop ] if ;
2008-06-28 00:10:19 -04:00
: 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 ;
2008-03-20 16:30:59 -04:00
: define-accessors ( class specs -- )
2008-06-28 00:10:19 -04:00
[ define-slot-methods ] with each ;
: define-protocol-slot ( name -- )
{
[ reader-word define-simple-generic ]
[ writer-word define-simple-generic ]
[ define-setter ]
[ define-changer ]
} cleave ;
ERROR: no-initial-value class ;
2008-07-12 02:08:30 -04:00
GENERIC: initial-value* ( class -- object )
M: class initial-value* no-initial-value ;
: initial-value ( class -- object )
{
2008-06-29 22:37:57 -04:00
{ [ \ 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{ } ] }
{ [ simple-alien bootstrap-word over class<= ] [ <bad-alien> ] }
2008-07-12 02:08:30 -04:00
[ 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 ] }
2008-06-30 02:44:58 -04:00
{ 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>> [
2008-06-29 22:37:57 -04:00
[ ] [
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? not ] [ peel-off-attributes ] [ ] while drop
check-initial-value ;
2008-07-13 22:06:50 -04:00
M: slot-spec make-slot
check-initial-value ;
: make-slots ( slots -- specs )
[ make-slot ] map ;
: finalize-slots ( specs base -- specs )
over length [ + ] with map [ >>offset ] 2map ;
: slot-named ( name specs -- spec/f )
2008-07-13 22:06:50 -04:00
[ name>> = ] with find nip ;