1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: words kernel sequences namespaces assocs hashtables
4 definitions kernel.private classes classes.private
5 classes.algebra quotations arrays vocabs effects combinators ;
8 ! Method combination protocol
9 GENERIC: perform-combination ( word combination -- )
11 GENERIC: make-default-method ( generic combination -- method )
13 PREDICATE: generic < word
14 "combination" word-prop >boolean ;
16 M: generic definition drop f ;
18 : make-generic ( word -- )
19 [ { "unannotated-def" } reset-props ]
20 [ dup "combination" word-prop perform-combination ]
23 : method ( class generic -- method/f )
24 "methods" word-prop at ;
26 PREDICATE: method-spec < pair
27 first2 generic? swap class? and ;
29 : order ( generic -- seq )
30 "methods" word-prop keys sort-classes ;
32 : specific-method ( class word -- class )
35 GENERIC: effective-method ( ... generic -- method )
37 : next-method-class ( class generic -- class/f )
38 order [ class<= ] with filter reverse dup length 1 =
39 [ drop f ] [ second ] if ;
41 : next-method ( class generic -- class/f )
42 [ next-method-class ] keep method ;
44 GENERIC: next-method-quot* ( class generic -- quot )
46 : next-method-quot ( class generic -- quot )
47 dup "combination" word-prop next-method-quot* ;
49 : (call-next-method) ( class generic -- )
50 next-method-quot call ;
52 TUPLE: check-method class generic ;
54 : check-method ( class generic -- class generic )
55 over class? over generic? and [
56 \ check-method boa throw
59 : with-methods ( generic quot -- )
60 swap [ "methods" word-prop swap call ] keep make-generic ;
63 : method-word-name ( class word -- string )
64 word-name "/" rot word-name 3append ;
66 PREDICATE: method-body < word
67 "method-generic" word-prop >boolean ;
69 M: method-body stack-effect
70 "method-generic" word-prop stack-effect ;
72 M: method-body crossref?
75 : method-word-props ( class generic -- assoc )
81 : <method> ( class generic -- method )
83 [ method-word-props ] 2keep
84 method-word-name f <word>
85 [ set-word-props ] keep ;
87 : reveal-method ( method class generic -- )
88 [ set-at ] with-methods ;
90 : create-method ( class generic -- method )
94 drop [ <method> dup ] 2keep reveal-method
97 : <default-method> ( generic combination -- method )
98 object bootstrap-word pick <method>
99 [ -rot make-default-method define ] keep ;
101 : define-default-method ( generic combination -- )
102 dupd <default-method> "default-method" set-word-prop ;
104 ! Definition protocol
106 dup first2 method [ ] [ second ] ?if where ;
108 M: method-spec set-where
109 first2 method set-where ;
111 M: method-spec definer
112 first2 method definer ;
114 M: method-spec definition
115 first2 method definition ;
117 M: method-spec forget*
118 first2 method forget* ;
120 M: method-spec smart-usage
123 M: method-body definer
126 M: method-body forget*
127 dup "forgotten" word-prop [ drop ] [
130 [ "method-class" word-prop ]
131 [ "method-generic" word-prop ] tri
133 [ delete-at ] with-methods
137 [ t "forgotten" set-word-prop ] bi
140 M: method-body smart-usage
141 "method-generic" word-prop smart-usage ;
143 : implementors* ( classes -- words )
145 "methods" word-prop keys
146 swap [ key? ] curry contains?
149 : implementors ( class -- seq )
150 dup associate implementors* ;
152 : forget-methods ( class -- )
153 [ implementors ] [ [ swap 2array ] curry ] bi map forget-all ;
155 M: class forget* ( class -- )
165 [ call-next-method ] bi ;
167 M: assoc update-methods ( assoc -- )
168 implementors* [ make-generic ] each ;
170 : define-generic ( word combination -- )
171 over "combination" word-prop over = [
174 2dup "combination" set-word-prop
175 over H{ } clone "methods" set-word-prop
176 dupd define-default-method
182 [ "default-method" word-prop , ]
183 [ "methods" word-prop values % ]
184 [ "engines" word-prop % ]
189 [ subwords forget-all ] [ call-next-method ] bi ;
191 : xref-generics ( -- )
192 all-words [ subwords [ xref ] each ] each ;