1 ! Copyright (C) 2006, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors words kernel sequences namespaces make assocs
4 hashtables definitions kernel.private classes classes.private
5 classes.algebra quotations arrays vocabs effects combinators
7 FROM: namespaces => set ;
10 ! Method combination protocol
11 GENERIC: perform-combination ( word combination -- )
13 GENERIC: make-default-method ( generic combination -- method )
15 PREDICATE: generic < word
16 "combination" word-prop >boolean ;
18 M: generic definition drop f ;
20 : make-generic ( word -- )
21 [ { "unannotated-def" } reset-props ]
22 [ dup "combination" word-prop perform-combination ]
25 PREDICATE: method < word
26 "method-generic" word-prop >boolean ;
28 : method ( class generic -- method/f )
29 "methods" word-prop at ;
33 : interesting-class? ( class1 class2 -- ? )
35 ! Case 1: no intersection. Discard and keep going
36 { [ 2dup classes-intersect? not ] [ 2drop t ] }
37 ! Case 2: class1 contained in class2. Add to
38 ! interesting set and keep going.
39 { [ 2dup class<= ] [ nip , t ] }
40 ! Case 3: class1 and class2 are incomparable. Give up
44 : interesting-classes ( class classes -- interesting/f )
45 [ [ interesting-class? ] with all? ] { } make and ;
49 : method-classes ( generic -- classes )
50 "methods" word-prop keys ;
52 : order ( generic -- seq )
53 method-classes sort-classes ;
55 : nearest-class ( class generic -- class/f )
56 method-classes interesting-classes smallest-class ;
58 : method-for-class ( class generic -- method/f )
59 [ nip ] [ nearest-class ] 2bi dup [ swap method ] [ 2drop f ] if ;
61 GENERIC: effective-method ( generic -- method )
63 \ effective-method t "no-compile" set-word-prop
65 : next-method-class ( class generic -- class/f )
66 method-classes [ class< ] with filter smallest-class ;
68 : next-method ( class generic -- method/f )
69 [ next-method-class ] keep method ;
71 GENERIC: next-method-quot* ( class generic combination -- quot )
73 : next-method-quot ( method -- quot )
74 next-method-quot-cache get [
75 [ "method-class" word-prop ]
77 "method-generic" word-prop
78 dup "combination" word-prop
79 ] bi next-method-quot*
82 ERROR: no-next-method method ;
84 : (call-next-method) ( method -- )
85 dup next-method-quot [ call ] [ no-next-method ] ?if ;
87 TUPLE: check-method class generic ;
89 : check-method ( class generic -- class generic )
90 2dup [ class? ] [ generic? ] bi* and [
91 \ check-method boa throw
94 : remake-generic ( generic -- )
95 dup outdated-generics get set-in-unit ;
97 : remake-generics ( -- )
98 outdated-generics get keys [ generic? ] filter [ make-generic ] each ;
100 GENERIC: update-generic ( class generic -- )
102 : with-methods ( class generic quot -- )
103 [ "methods" word-prop ] prepose [ update-generic ] 2bi ; inline
105 : method-word-name ( class generic -- string )
106 [ name>> ] bi@ "=>" glue ;
108 M: method parent-word
109 "method-generic" word-prop ;
112 "forgotten" word-prop not ;
114 : method-word-props ( class generic -- assoc )
120 : <method> ( class generic -- method )
122 [ method-word-name f <word> ] [ method-word-props ] 2bi
125 : with-implementors ( class generic quot -- )
126 [ swap implementors-map get at ] dip call ; inline
128 : reveal-method ( method class generic -- )
129 [ [ conjoin ] with-implementors ]
130 [ [ set-at ] with-methods ]
133 : create-method ( class generic -- method )
134 2dup method dup [ 2nip dup reset-generic ] [
136 [ <method> dup ] 2keep
141 PREDICATE: default-method < word "default" word-prop ;
143 : <default-method> ( generic combination -- method )
144 [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
145 [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
147 : define-default-method ( generic combination -- )
148 dupd <default-method> "default-method" set-word-prop ;
150 ! Definition protocol
155 dup "forgotten" word-prop [ drop ] [
157 dup default-method? [ drop ] [
159 [ "method-class" word-prop ]
160 [ "method-generic" word-prop ] bi
164 [ [ delete-at ] with-methods ]
165 [ [ delete-at ] with-implementors ] 2bi
170 [ call-next-method ] bi
173 : define-generic ( word combination effect -- )
174 [ nip swap set-stack-effect ]
177 2dup [ "combination" word-prop ] dip = [ 2drop ] [
179 [ drop reset-generic ]
180 [ "combination" set-word-prop ]
181 [ drop H{ } clone "methods" set-word-prop ]
182 [ define-default-method ]
187 [ 2drop remake-generic ] 3tri ;
191 [ "default-method" word-prop , ]
192 [ "methods" word-prop values % ]
193 [ "engines" word-prop % ]
197 M: class forget-methods
198 [ implementors ] [ [ swap method ] curry ] bi map forget-all ;