2009-01-21 20:55:47 -05:00
|
|
|
! Copyright (C) 2005, 2009 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-07-02 03:03:30 -04:00
|
|
|
USING: arrays byte-arrays kernel kernel.private math namespaces
|
2008-09-10 21:07:00 -04:00
|
|
|
make sequences strings words effects generic generic.standard
|
|
|
|
classes classes.algebra slots.private combinators accessors
|
2009-01-21 20:55:47 -05:00
|
|
|
words sequences.private assocs alien quotations hashtables ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: slots
|
|
|
|
|
2008-09-03 19:47:52 -04:00
|
|
|
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 ;
|
|
|
|
|
2009-03-07 00:33:03 -05:00
|
|
|
PREDICATE: reader-method < method-body "reading" word-prop ;
|
|
|
|
|
2008-07-25 03:07:45 -04:00
|
|
|
PREDICATE: writer < word "writer" word-prop ;
|
|
|
|
|
2009-03-07 00:33:03 -05:00
|
|
|
PREDICATE: writer-method < method-body "writing" word-prop ;
|
|
|
|
|
2008-06-28 03:36:20 -04:00
|
|
|
: <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
|
|
|
|
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-12-15 20:44:56 -05:00
|
|
|
">>" append "accessors" create
|
|
|
|
dup (( object -- value )) "declared-effect" set-word-prop
|
2008-07-25 03:07:45 -04:00
|
|
|
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
|
|
|
|
2008-06-28 03:36:20 -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-12-15 20:44:56 -05:00
|
|
|
"(>>" ")" surround "accessors" create
|
|
|
|
dup (( value object -- )) "declared-effect" set-word-prop
|
2008-07-25 03:07:45 -04:00
|
|
|
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 03:12:44 -04:00
|
|
|
|
2008-06-29 22:37:57 -04:00
|
|
|
: writer-quot/coerce ( slot-spec -- )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ class>> "coercer" word-prop [ dip ] curry % ]
|
2008-06-29 22:37:57 -04:00
|
|
|
[ offset>> , \ set-slot , ]
|
|
|
|
bi ;
|
2008-06-29 03:12:44 -04:00
|
|
|
|
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 03:12:44 -04:00
|
|
|
|
2008-06-29 22:37:57 -04:00
|
|
|
: writer-quot/fixnum ( slot-spec -- )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ [ >fixnum ] dip ] % writer-quot/check ;
|
2008-06-29 03:12:44 -04:00
|
|
|
|
2008-06-29 22:37:57 -04:00
|
|
|
: writer-quot ( slot-spec -- quot )
|
2008-06-28 00:10:19 -04:00
|
|
|
[
|
2008-06-29 03:12:44 -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 ] }
|
2008-06-29 03:12:44 -04:00
|
|
|
[ 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 )
|
2009-01-21 20:55:47 -05:00
|
|
|
"writing" associate ;
|
2008-07-25 03:07:45 -04:00
|
|
|
|
2008-06-28 03:36:20 -04:00
|
|
|
: 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-12-15 20:44:56 -05:00
|
|
|
">>" prepend "accessors" create ;
|
2008-03-20 16:30:59 -04:00
|
|
|
|
2008-11-14 01:39:28 -05:00
|
|
|
: define-setter ( name -- )
|
|
|
|
dup setter-word dup deferred? [
|
2008-12-15 20:44:56 -05:00
|
|
|
[ \ over , swap writer-word , ] [ ] make
|
|
|
|
(( object value -- object )) define-inline
|
2008-03-20 16:30:59 -04:00
|
|
|
] [ 2drop ] if ;
|
|
|
|
|
|
|
|
: changer-word ( name -- word )
|
2008-12-15 20:44:56 -05:00
|
|
|
"change-" prepend "accessors" create ;
|
2008-03-20 16:30:59 -04:00
|
|
|
|
2008-11-14 01:39:28 -05:00
|
|
|
: define-changer ( name -- )
|
|
|
|
dup changer-word dup deferred? [
|
2008-03-20 16:30:59 -04:00
|
|
|
[
|
2008-11-23 03:44:56 -05:00
|
|
|
\ over ,
|
|
|
|
over reader-word 1quotation
|
|
|
|
[ dip call ] curry [ dip swap ] curry %
|
2008-03-20 16:30:59 -04:00
|
|
|
swap setter-word ,
|
2008-12-15 20:44:56 -05:00
|
|
|
] [ ] make (( object quot -- object )) define-inline
|
2008-03-20 16:30:59 -04:00
|
|
|
] [ 2drop ] if ;
|
|
|
|
|
2008-06-28 00:10:19 -04:00
|
|
|
: define-slot-methods ( class slot-spec -- )
|
2008-06-28 03:36:20 -04:00
|
|
|
[ define-reader ]
|
|
|
|
[
|
|
|
|
dup read-only>> [ 2drop ] [
|
2008-11-14 01:39:28 -05:00
|
|
|
[ name>> define-setter drop ]
|
|
|
|
[ name>> define-changer drop ]
|
2008-06-28 03:36:20 -04:00
|
|
|
[ 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 ;
|
2008-03-31 02:19:34 -04:00
|
|
|
|
2008-06-28 03:36:20 -04:00
|
|
|
: define-protocol-slot ( name -- )
|
|
|
|
{
|
2008-11-14 01:39:28 -05:00
|
|
|
[ reader-word define-simple-generic ]
|
|
|
|
[ writer-word define-simple-generic ]
|
|
|
|
[ define-setter ]
|
|
|
|
[ define-changer ]
|
2008-06-28 03:36:20 -04:00
|
|
|
} cleave ;
|
|
|
|
|
2008-06-29 03:12:44 -04:00
|
|
|
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 ;
|
|
|
|
|
2008-06-29 03:12:44 -04:00
|
|
|
: 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{ } ] }
|
2008-07-01 17:33:45 -04:00
|
|
|
{ [ simple-alien bootstrap-word over class<= ] [ <bad-alien> ] }
|
2009-01-28 02:57:14 -05:00
|
|
|
{ [ quotation bootstrap-word over class<= ] [ [ ] ] }
|
2008-07-12 02:08:30 -04:00
|
|
|
[ dup initial-value* ]
|
2008-06-29 03:12:44 -04:00
|
|
|
} cond nip ;
|
|
|
|
|
2008-06-28 03:36:20 -04:00
|
|
|
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? [
|
2008-06-29 03:12:44 -04:00
|
|
|
dup first class? [
|
2008-06-28 03:36:20 -04:00
|
|
|
[ first >>class ] [ rest ] bi
|
|
|
|
] when
|
|
|
|
] unless ;
|
|
|
|
|
2008-06-29 03:12:44 -04:00
|
|
|
ERROR: bad-slot-attribute key ;
|
|
|
|
|
2008-06-28 03:36:20 -04:00
|
|
|
: 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 ] }
|
2008-06-29 03:12:44 -04:00
|
|
|
[ bad-slot-attribute ]
|
2008-06-28 03:36:20 -04:00
|
|
|
} case
|
|
|
|
] unless ;
|
|
|
|
|
2008-06-29 03:12:44 -04:00
|
|
|
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
|
2008-06-29 03:12:44 -04:00
|
|
|
] [
|
|
|
|
dup class>> initial-value >>initial
|
|
|
|
] if ;
|
|
|
|
|
2008-06-28 03:36:20 -04:00
|
|
|
M: array make-slot
|
|
|
|
<slot-spec>
|
|
|
|
swap
|
|
|
|
peel-off-name
|
|
|
|
peel-off-class
|
2009-02-17 20:19:49 -05:00
|
|
|
[ dup empty? ] [ peel-off-attributes ] until drop
|
2008-06-29 03:12:44 -04:00
|
|
|
check-initial-value ;
|
2008-06-28 03:36:20 -04:00
|
|
|
|
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 ;
|
2008-06-28 03:36:20 -04:00
|
|
|
|
2008-03-31 02:19:34 -04:00
|
|
|
: slot-named ( name specs -- spec/f )
|
2008-07-13 22:06:50 -04:00
|
|
|
[ name>> = ] with find nip ;
|