factor/core/classes/classes.factor

258 lines
6.6 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2010 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs combinators definitions graphs kernel
make namespaces quotations sequences sets words words.symbol ;
2007-09-20 18:09:08 -04:00
IN: classes
2008-03-24 20:52:21 -04:00
ERROR: bad-inheritance class superclass ;
PREDICATE: class < word "class" word-prop ;
PREDICATE: defining-class < word "defining-class" word-prop ;
MIXIN: classoid
INSTANCE: class classoid
INSTANCE: defining-class classoid
<PRIVATE
2008-05-02 03:51:38 -04:00
SYMBOL: class<=-cache
2008-03-24 20:52:21 -04:00
SYMBOL: class-not-cache
SYMBOL: classes-intersect-cache
SYMBOL: class-and-cache
SYMBOL: class-or-cache
2008-10-01 09:20:49 -04:00
SYMBOL: next-method-quot-cache
2008-03-24 20:52:21 -04:00
: init-caches ( -- )
H{ } clone class<=-cache namespaces:set
H{ } clone class-not-cache namespaces:set
H{ } clone classes-intersect-cache namespaces:set
H{ } clone class-and-cache namespaces:set
H{ } clone class-or-cache namespaces:set
H{ } clone next-method-quot-cache namespaces:set ;
2008-03-24 20:52:21 -04:00
: reset-caches ( -- )
2008-05-02 03:51:38 -04:00
class<=-cache get clear-assoc
2008-03-24 20:52:21 -04:00
class-not-cache get clear-assoc
classes-intersect-cache get clear-assoc
class-and-cache get clear-assoc
2008-10-01 09:20:49 -04:00
class-or-cache get clear-assoc
next-method-quot-cache get clear-assoc ;
2007-09-20 18:09:08 -04:00
SYMBOL: update-map
2008-04-02 19:50:21 -04:00
SYMBOL: implementors-map
GENERIC: class-name ( class -- string )
M: class class-name name>> ;
GENERIC: rank-class ( class -- n )
GENERIC: reset-class ( class -- )
M: class reset-class
{
"defining-class"
"class"
"metaclass"
"superclass"
"members"
"participants"
"predicate"
} remove-word-props ;
M: word reset-class drop ;
PRIVATE>
2008-04-02 19:50:21 -04:00
: classes ( -- seq ) implementors-map get keys ;
2007-09-20 18:09:08 -04:00
PREDICATE: predicate < word "predicating" word-prop >boolean ;
: create-predicate-word ( word -- predicate )
[ name>> "?" append ] [ vocabulary>> ] bi create-word
dup predicate? [ dup reset-generic ] unless ;
2007-09-20 18:09:08 -04:00
GENERIC: class-of ( object -- class )
GENERIC: instance? ( object class -- ? ) flushable
GENERIC: predicate-def ( obj -- quot )
M: word predicate-def
"predicate" word-prop ;
M: object predicate-def
[ instance? ] curry ;
: predicate-word ( word -- predicate )
predicate-def first ;
M: predicate flushable? drop t ;
M: predicate forget*
[ call-next-method ] [ f "predicating" set-word-prop ] bi ;
M: predicate reset-word
[ call-next-method ] [ f "predicating" set-word-prop ] bi ;
: define-predicate ( class quot -- )
[ predicate-word ] dip ( object -- ? ) define-declared ;
2007-09-20 18:09:08 -04:00
: superclass-of ( class -- super )
! Output f for non-classes to work with algebra code
2008-03-24 20:52:21 -04:00
dup class? [ "superclass" word-prop ] [ drop f ] if ;
2007-09-20 18:09:08 -04:00
: superclasses-of ( class -- supers )
[ superclass-of ] follow reverse! ;
2008-03-27 02:42:13 -04:00
: superclass-of? ( class superclass -- ? )
superclasses-of member-eq? ;
: subclass-of? ( class superclass -- ? )
swap superclass-of? ;
: class-members ( class -- seq )
2015-09-08 19:15:10 -04:00
! Output f for non-classes to work with algebra code
2008-03-24 20:52:21 -04:00
dup class? [ "members" word-prop ] [ drop f ] if ;
2007-09-20 18:09:08 -04:00
: class-participants ( class -- seq )
2015-09-08 19:15:10 -04:00
! Output f for non-classes to work with algebra code
2008-05-10 19:09:05 -04:00
dup class? [ "participants" word-prop ] [ drop f ] if ;
GENERIC: contained-classes ( obj -- members )
M: object contained-classes
"members" word-prop ;
: all-contained-classes ( members -- members' )
dup dup [ contained-classes ] map concat sift append
2dup set= [ drop members ] [ nip all-contained-classes ] if ;
GENERIC: implementors ( class/classes -- seq )
2007-09-20 18:09:08 -04:00
! update-map
: class-uses ( class -- seq )
2008-05-10 19:09:05 -04:00
[
[ class-members % ]
[ class-participants % ]
[ superclass-of [ , ] when* ]
2008-05-10 19:09:05 -04:00
tri
] { } make ;
2007-09-20 18:09:08 -04:00
: class-usage ( class -- seq )
update-map get at members ;
2008-07-04 02:32:11 -04:00
: class-usages ( class -- seq )
[ class-usage ] closure members ;
2007-09-20 18:09:08 -04:00
M: class implementors implementors-map get at members ;
M: sequence implementors [ implementors ] gather ;
<PRIVATE
2007-09-20 18:09:08 -04:00
: update-map+ ( class -- )
dup class-uses update-map get add-vertex ;
2007-09-20 18:09:08 -04:00
: update-map- ( class -- )
dup class-uses update-map get remove-vertex ;
2007-09-20 18:09:08 -04:00
: implementors-map+ ( class -- )
[ HS{ } clone ] dip implementors-map get set-at ;
: implementors-map- ( class -- )
implementors-map get delete-at ;
2008-05-10 19:09:05 -04:00
: make-class-props ( superclass members participants metaclass -- assoc )
2007-09-20 18:09:08 -04:00
[
2008-05-10 19:09:05 -04:00
{
2012-07-19 12:50:09 -04:00
[ dup [ bootstrap-word ] when "superclass" ,, ]
[ [ bootstrap-word ] map "members" ,, ]
[ [ bootstrap-word ] map "participants" ,, ]
[ "metaclass" ,, ]
2008-05-10 19:09:05 -04:00
} spread
2012-07-19 12:50:09 -04:00
] H{ } make ;
2007-09-20 18:09:08 -04:00
GENERIC: metaclass-changed ( use class -- )
: ?metaclass-changed ( class usages/f -- )
[ [ metaclass-changed ] with each ] [ drop ] if* ;
: check-metaclass ( class metaclass -- usages/f )
over class? [
over "metaclass" word-prop eq?
[ drop f ] [ class-usage ] if
] [ 2drop f ] if ;
2009-03-22 21:16:31 -04:00
: ?define-symbol ( word -- )
dup deferred? [ define-symbol ] [ drop ] if ;
2007-09-20 18:09:08 -04:00
: (define-class) ( word props -- )
reset-caches
2dup "metaclass" of check-metaclass
{
[ 2drop update-map- ]
[ 2drop dup class? [ reset-class ] [ implementors-map+ ] if ]
[ 2drop ?define-symbol ]
[ drop [ assoc-union ] curry change-props drop ]
[
2drop
dup create-predicate-word
[ 1quotation "predicate" set-word-prop ]
[ swap "predicating" set-word-prop ]
2bi
]
[ 2drop t "class" set-word-prop ]
[ 2drop f "defining-class" set-word-prop ]
[ 2drop update-map+ ]
[ nip ?metaclass-changed ]
} 3cleave ;
2008-03-28 23:59:48 -04:00
GENERIC: update-class ( class -- )
2008-01-31 01:49:18 -05:00
M: class update-class drop ;
2008-01-31 01:49:18 -05:00
2008-06-11 18:40:33 -04:00
GENERIC: update-methods ( class seq -- )
2008-04-02 03:44:10 -04:00
: update-classes ( class -- )
dup class-usages
2008-06-11 18:40:33 -04:00
[ nip [ update-class ] each ] [ update-methods ] 2bi ;
2008-04-02 03:44:10 -04:00
: check-inheritance ( subclass superclass -- )
2dup superclass-of? [ bad-inheritance ] [ 2drop ] if ;
2008-05-10 19:09:05 -04:00
: define-class ( word superclass members participants metaclass -- )
[ 2dup check-inheritance ] 3dip
make-class-props [ (define-class) ] [ drop changed-definition ] 2bi ;
2007-09-20 18:09:08 -04:00
2008-06-11 21:46:53 -04:00
: forget-predicate ( class -- )
dup predicate-def
2008-06-11 21:46:53 -04:00
dup length 1 = [
first
2009-01-23 19:20:47 -05:00
[ nip ] [ "predicating" word-prop = ] 2bi
2008-06-11 21:46:53 -04:00
[ forget ] [ drop ] if
] [ 2drop ] if ;
2009-04-06 05:16:39 -04:00
GENERIC: forget-methods ( class -- )
PRIVATE>
2008-07-04 02:32:11 -04:00
: forget-class ( class -- )
dup f check-metaclass {
[ drop forget-predicate ]
[ drop forget-methods ]
[ drop implementors-map- ]
[ drop update-map- ]
[ drop reset-class ]
[ 2drop reset-caches ]
[ ?metaclass-changed ]
} 2cleave ;
M: class metaclass-changed
swap class? [ drop ] [ forget-class ] if ;
M: class forget* ( class -- )
[ call-next-method ] [ forget-class ] bi ;