2019-10-18 09:05:08 -04:00
|
|
|
! Copyright (C) 2006, 2007 Slava Pestov.
|
2006-08-02 02:49:23 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
|
IN: generic
|
2019-10-18 09:05:08 -04:00
|
|
|
USING: arrays definitions errors assocs help kernel
|
|
|
|
|
sequences words namespaces quotations ;
|
2006-08-02 02:49:23 -04:00
|
|
|
|
2006-11-11 00:43:24 -05:00
|
|
|
TUPLE: method loc def ;
|
2006-08-02 03:49:13 -04:00
|
|
|
|
|
|
|
|
M: f method-def ;
|
|
|
|
|
M: f method-loc ;
|
|
|
|
|
M: quotation method-def ;
|
|
|
|
|
M: quotation method-loc drop f ;
|
|
|
|
|
|
2006-08-16 21:55:53 -04:00
|
|
|
: method ( class generic -- method/f )
|
2019-10-18 09:05:08 -04:00
|
|
|
"methods" word-prop at ;
|
2006-08-02 03:10:09 -04:00
|
|
|
|
2019-10-18 09:05:04 -04:00
|
|
|
PREDICATE: pair method-spec
|
|
|
|
|
first2 dup generic? [ method >boolean ] [ 2drop f ] if ;
|
|
|
|
|
|
2006-08-16 21:55:53 -04:00
|
|
|
: order ( generic -- seq )
|
2019-10-18 09:05:08 -04:00
|
|
|
"methods" word-prop keys sort-classes ;
|
|
|
|
|
|
|
|
|
|
: methods ( generic -- assoc )
|
|
|
|
|
dup "methods" word-prop swap order [
|
|
|
|
|
dup rot at method-def 2array
|
|
|
|
|
] map-with ;
|
2006-08-02 02:49:23 -04:00
|
|
|
|
|
|
|
|
TUPLE: check-method class generic ;
|
|
|
|
|
|
|
|
|
|
: check-method ( class generic -- class generic )
|
|
|
|
|
dup generic? [ <check-method> throw ] unless
|
|
|
|
|
over class? [ <check-method> throw ] unless ;
|
|
|
|
|
|
2006-08-15 16:29:35 -04:00
|
|
|
: with-methods ( word quot -- )
|
2006-08-02 02:49:23 -04:00
|
|
|
swap [ "methods" word-prop swap call ] keep ?make-generic ;
|
|
|
|
|
inline
|
|
|
|
|
|
2006-08-02 03:49:13 -04:00
|
|
|
: define-method ( method class generic -- )
|
2006-08-02 02:49:23 -04:00
|
|
|
>r bootstrap-word r> check-method
|
2019-10-18 09:05:08 -04:00
|
|
|
[ set-at ] with-methods ;
|
|
|
|
|
|
|
|
|
|
! Definition protocol
|
|
|
|
|
M: method-spec where
|
|
|
|
|
dup first2 method method-loc [ ] [ second where ] ?if ;
|
|
|
|
|
|
|
|
|
|
M: method-spec forget
|
|
|
|
|
first2 [ delete-at ] with-methods ;
|
|
|
|
|
|
|
|
|
|
M: method-spec definer drop \ M: \ ; ;
|
|
|
|
|
|
|
|
|
|
M: method-spec definition first2 method method-def ;
|
2006-08-02 02:49:23 -04:00
|
|
|
|
2006-08-16 21:55:53 -04:00
|
|
|
: implementors ( class -- seq )
|
2019-10-18 09:05:04 -04:00
|
|
|
all-words
|
|
|
|
|
[ generic? ] subset
|
2019-10-18 09:05:08 -04:00
|
|
|
[ "methods" word-prop key? ] subset-with ;
|
2006-08-02 15:17:13 -04:00
|
|
|
|
2019-10-18 09:05:08 -04:00
|
|
|
: forget-methods ( class -- )
|
|
|
|
|
dup implementors [ 2array forget ] each-with ;
|
2006-08-02 16:53:26 -04:00
|
|
|
|
2019-10-18 09:05:08 -04:00
|
|
|
: forget-predicate ( class -- )
|
|
|
|
|
"predicate" word-prop [ forget ] each ;
|
2006-08-02 15:17:13 -04:00
|
|
|
|
2019-10-18 09:05:08 -04:00
|
|
|
: forget-class ( class -- )
|
|
|
|
|
dup forget-methods
|
|
|
|
|
dup forget-predicate
|
|
|
|
|
dup uncache-class
|
|
|
|
|
forget-word ;
|
2006-08-02 15:17:13 -04:00
|
|
|
|
2019-10-18 09:05:08 -04:00
|
|
|
M: class forget forget-class ;
|