2005-02-20 19:03:37 -05:00
|
|
|
! Copyright (C) 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
|
|
|
|
|
|
|
! Some code for defining slot accessors and mutators. Used to
|
|
|
|
! implement tuples, as well as builtin types.
|
|
|
|
IN: generic
|
2005-09-11 20:46:55 -04:00
|
|
|
USING: arrays kernel kernel-internals lists math namespaces
|
|
|
|
parser sequences strings words ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
2005-08-02 00:25:05 -04:00
|
|
|
: define-typecheck ( class generic def -- )
|
2005-02-20 19:03:37 -05:00
|
|
|
#! Just like:
|
|
|
|
#! GENERIC: generic
|
|
|
|
#! M: class generic def ;
|
2005-09-16 02:39:33 -04:00
|
|
|
over define-generic -rot define-method ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
|
|
|
: define-slot-word ( class slot word quot -- )
|
|
|
|
over [
|
2005-08-02 00:25:05 -04:00
|
|
|
>r swap >fixnum r> cons define-typecheck
|
2005-02-20 19:03:37 -05:00
|
|
|
] [
|
|
|
|
2drop 2drop
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: define-reader ( class slot reader -- )
|
|
|
|
[ slot ] define-slot-word ;
|
|
|
|
|
|
|
|
: define-writer ( class slot writer -- )
|
|
|
|
[ set-slot ] define-slot-word ;
|
|
|
|
|
|
|
|
: define-slot ( class slot reader writer -- )
|
|
|
|
>r >r 2dup r> define-reader r> define-writer ;
|
|
|
|
|
2005-08-21 23:35:50 -04:00
|
|
|
: ?create ( { name vocab }/f -- word )
|
2005-09-02 23:44:23 -04:00
|
|
|
dup [ first2 create ] when ;
|
2005-08-21 23:35:50 -04:00
|
|
|
|
2005-02-20 19:03:37 -05:00
|
|
|
: intern-slots ( spec -- spec )
|
2005-09-16 20:49:24 -04:00
|
|
|
[ first3 [ ?create ] 2apply 3array ] map ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
|
|
|
: define-slots ( class spec -- )
|
|
|
|
#! Define a collection of slot readers and writers for the
|
|
|
|
#! given class. The spec is a list of lists of length 3 of
|
|
|
|
#! the form [ slot reader writer ]. slot is an integer,
|
|
|
|
#! reader and writer are either words, strings or f.
|
2005-09-02 23:44:23 -04:00
|
|
|
[ first3 define-slot ] each-with ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
|
|
|
: reader-word ( class name -- word )
|
2005-09-11 20:46:55 -04:00
|
|
|
>r word-name "-" r> append3 "in" get 2array ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
|
|
|
: writer-word ( class name -- word )
|
2005-08-25 15:27:38 -04:00
|
|
|
[ swap "set-" % word-name % "-" % % ] "" make
|
2005-09-11 20:46:55 -04:00
|
|
|
"in" get 2array ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
2005-07-25 17:13:35 -04:00
|
|
|
: simple-slot ( class name -- reader writer )
|
|
|
|
[ reader-word ] 2keep writer-word ;
|
2005-02-20 19:03:37 -05:00
|
|
|
|
2005-07-25 17:13:35 -04:00
|
|
|
: simple-slots ( class slots base -- spec )
|
2005-02-20 19:03:37 -05:00
|
|
|
#! Takes a list of slot names, and for each slot name
|
|
|
|
#! defines a pair of words <class>-<slot> and
|
|
|
|
#! set-<class>-<slot>. Slot numbering is consecutive and
|
|
|
|
#! begins at base.
|
2005-07-25 17:13:35 -04:00
|
|
|
over length [ + ] map-with
|
2005-09-11 20:46:55 -04:00
|
|
|
[ >r dupd simple-slot r> -rot 3array ] 2map nip
|
2005-08-19 21:46:12 -04:00
|
|
|
intern-slots ;
|