factor/core/generic/generic.factor

180 lines
4.4 KiB
Factor
Raw Normal View History

2008-02-04 20:38:31 -05:00
! Copyright (C) 2006, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: words kernel sequences namespaces assocs hashtables
definitions kernel.private classes classes.private
2008-03-24 20:52:21 -04:00
classes.algebra quotations arrays vocabs effects ;
2007-09-20 18:09:08 -04:00
IN: generic
! Method combination protocol
GENERIC: perform-combination ( word combination -- )
2007-10-09 17:41:35 -04:00
GENERIC: make-default-method ( generic combination -- method )
2008-03-26 19:23:19 -04:00
PREDICATE: generic < word
"combination" word-prop >boolean ;
M: generic definition drop f ;
2007-09-20 18:09:08 -04:00
: make-generic ( word -- )
2008-04-02 19:50:21 -04:00
[ { "unannotated-def" } reset-props ]
[ dup "combination" word-prop perform-combination ]
bi ;
2007-09-20 18:09:08 -04:00
: method ( class generic -- method/f )
"methods" word-prop at ;
2008-03-26 19:23:19 -04:00
PREDICATE: method-spec < pair
2007-09-20 18:09:08 -04:00
first2 generic? swap class? and ;
: order ( generic -- seq )
"methods" word-prop keys sort-classes ;
2008-04-18 17:51:09 -04:00
: specific-method ( class word -- class )
order min-class ;
GENERIC: effective-method ( ... generic -- method )
: next-method-class ( class generic -- class/f )
order [ class< ] with subset reverse dup length 1 =
[ drop f ] [ second ] if ;
: next-method ( class generic -- class/f )
[ next-method-class ] keep method ;
2008-04-05 21:07:30 -04:00
GENERIC: next-method-quot* ( class generic -- quot )
: next-method-quot ( class generic -- quot )
dup "combination" word-prop next-method-quot* ;
: (call-next-method) ( class generic -- )
next-method-quot call ;
2007-09-20 18:09:08 -04:00
TUPLE: check-method class generic ;
: check-method ( class generic -- class generic )
over class? over generic? and [
\ check-method boa throw
2008-03-16 03:43:00 -04:00
] unless ; inline
2007-09-20 18:09:08 -04:00
2008-03-16 03:43:00 -04:00
: with-methods ( generic quot -- )
swap [ "methods" word-prop swap call ] keep make-generic ;
2007-09-20 18:09:08 -04:00
inline
: method-word-name ( class word -- string )
word-name "/" rot word-name 3append ;
2008-03-26 19:23:19 -04:00
PREDICATE: method-body < word
2008-03-16 03:43:00 -04:00
"method-generic" word-prop >boolean ;
2008-02-04 20:38:31 -05:00
M: method-body stack-effect
"method-generic" word-prop stack-effect ;
2008-02-04 20:38:31 -05:00
2008-04-02 19:50:21 -04:00
M: method-body crossref?
drop t ;
2008-03-16 03:43:00 -04:00
: method-word-props ( class generic -- assoc )
[
"method-generic" set
"method-class" set
] H{ } make-assoc ;
2008-03-16 03:43:00 -04:00
: <method> ( class generic -- method )
check-method
[ method-word-props ] 2keep
method-word-name f <word>
2008-03-16 03:43:00 -04:00
[ set-word-props ] keep ;
: reveal-method ( method class generic -- )
[ set-at ] with-methods ;
: create-method ( class generic -- method )
2dup method dup [
2nip
] [
2008-03-16 03:43:00 -04:00
drop [ <method> dup ] 2keep reveal-method
] if ;
2007-09-20 18:09:08 -04:00
2008-03-16 03:43:00 -04:00
: <default-method> ( generic combination -- method )
object bootstrap-word pick <method>
[ -rot make-default-method define ] keep ;
: define-default-method ( generic combination -- )
2008-03-16 03:43:00 -04:00
dupd <default-method> "default-method" set-word-prop ;
2007-09-20 18:09:08 -04:00
! Definition protocol
M: method-spec where
dup first2 method [ ] [ second ] ?if where ;
2007-09-20 18:09:08 -04:00
2008-02-23 23:29:29 -05:00
M: method-spec set-where
first2 method set-where ;
2007-09-20 18:09:08 -04:00
2008-02-23 23:29:29 -05:00
M: method-spec definer
2008-03-16 03:43:00 -04:00
first2 method definer ;
2007-09-20 18:09:08 -04:00
2008-02-03 15:19:07 -05:00
M: method-spec definition
2008-03-16 03:43:00 -04:00
first2 method definition ;
2007-09-20 18:09:08 -04:00
2008-02-23 23:29:29 -05:00
M: method-spec forget*
2008-03-18 22:43:29 -04:00
first2 method forget* ;
2008-02-23 23:29:29 -05:00
M: method-body definer
drop \ M: \ ; ;
M: method-body forget*
2008-03-18 22:43:29 -04:00
dup "forgotten" word-prop [ drop ] [
2008-04-02 19:50:21 -04:00
[
2008-04-03 06:57:20 -04:00
[ "method-class" word-prop ]
2008-04-02 19:50:21 -04:00
[ "method-generic" word-prop ] bi
2008-04-03 06:57:20 -04:00
dup generic? [
[ delete-at* ] with-methods
[ call-next-method ] [ drop ] if
] [ 2drop ] if
2008-04-02 19:50:21 -04:00
]
[ t "forgotten" set-word-prop ] bi
2008-03-18 22:43:29 -04:00
] if ;
2007-09-20 18:09:08 -04:00
: implementors* ( classes -- words )
all-words [
"methods" word-prop keys
swap [ key? ] curry contains?
2008-01-09 17:36:30 -05:00
] with subset ;
2007-09-20 18:09:08 -04:00
: implementors ( class -- seq )
dup associate implementors* ;
: forget-methods ( class -- )
2008-04-02 19:50:21 -04:00
[ implementors ] [ [ swap 2array ] curry ] bi map forget-all ;
2007-09-20 18:09:08 -04:00
M: class forget* ( class -- )
2008-04-02 19:50:21 -04:00
[ forget-methods ]
[ update-map- ]
2008-04-03 06:57:20 -04:00
[ call-next-method ]
2008-04-02 19:50:21 -04:00
tri ;
2007-09-20 18:09:08 -04:00
2008-01-31 01:49:18 -05:00
M: assoc update-methods ( assoc -- )
implementors* [ make-generic ] each ;
: define-generic ( word combination -- )
2008-02-04 20:38:31 -05:00
over "combination" word-prop over = [
2drop
] [
2dup "combination" set-word-prop
over H{ } clone "methods" set-word-prop
dupd define-default-method
make-generic
] if ;
2008-02-04 20:43:10 -05:00
2008-02-05 00:30:59 -05:00
M: generic subwords
2008-04-02 19:50:21 -04:00
[
[ "default-method" word-prop , ]
[ "methods" word-prop values % ]
[ "engines" word-prop % ]
tri
] { } make ;
2008-02-04 20:43:10 -05:00
2008-04-03 06:57:20 -04:00
M: generic forget*
[ subwords forget-all ] [ call-next-method ] bi ;
2008-02-06 13:09:42 -05:00
2008-02-04 20:43:10 -05:00
: xref-generics ( -- )
2008-02-05 00:30:59 -05:00
all-words [ subwords [ xref ] each ] each ;