1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs byte-arrays byte-vectors classes
4 combinators definitions effects fry generic generic.single
5 generic.standard hashtables io.binary io.streams.string kernel
6 kernel.private math math.parser namespaces parser sbufs
7 sequences splitting splitting.private strings vectors words ;
10 GENERIC: specializer-predicate ( spec -- quot )
12 M: class specializer-predicate "predicate" word-prop ;
14 M: object specializer-predicate '[ _ eq? ] ;
16 GENERIC: specializer-declaration ( spec -- class )
18 M: class specializer-declaration ;
20 M: object specializer-declaration class ;
22 : specialized? ( types -- ? )
23 [ object = ] all? not ;
25 : specializer ( word -- specializer )
26 [ "specializer" word-prop ]
27 [ stack-effect effect-in-types ] bi
28 dup specialized? [ suffix ] [ drop ] if ;
30 : make-specializer ( specs -- quot )
32 [ (picker) 2array ] 2map
33 [ drop object eq? not ] assoc-filter
35 [ swap specializer-predicate append ] { } assoc>map
36 [ ] [ swap [ f ] \ if 3array append [ ] like ] map-reduce
39 ERROR: type-mismatch-error word expected-types ;
41 : fallback-def ( word -- quot )
42 dup stack-effect effect-in-types dup specialized?
43 [ [ type-mismatch-error ] 2curry ]
46 : specializer-cases ( quot specializer -- alist )
47 dup [ array? ] all? [ 1array ] unless [
48 [ nip make-specializer ]
49 [ [ specializer-declaration ] map swap '[ _ declare @ ] ] 2bi
50 ] with { } map>assoc ;
52 : specialize-quot ( quot word specializer -- quot' )
53 [ drop nip fallback-def ] [ nip specializer-cases ] 3bi alist>quot ;
55 ! compiler.tree.propagation.inlining sets this to f
56 SYMBOL: specialize-method?
58 t specialize-method? set-global
60 : method-declaration ( method -- quot )
61 [ "method-generic" word-prop dispatch# object <array> ]
62 [ "method-class" word-prop ]
63 bi prefix [ declare ] curry [ ] like ;
65 : specialize-method ( quot method -- quot' )
66 [ specialize-method? get [ method-declaration prepend ] [ drop ] if ]
67 [ dup "method-generic" word-prop specializer ] bi
68 [ specialize-quot ] [ nip ] if* ;
70 : standard-method? ( method -- ? )
72 "method-generic" word-prop standard-generic?
75 : specialized-def ( word -- quot )
77 dup generic? [ drop ] [
78 [ dup standard-method? [ specialize-method ] [ drop ] if ]
79 [ dup specializer [ specialize-quot ] [ drop ] if* ]
83 : specialized-length ( specializer -- n )
84 dup [ array? ] all? [ first ] when length ;
87 scan-object dup wrapper? [ wrapped>> ] when
88 [ changed-definition ]
89 [ subwords [ changed-definition ] each ]
90 [ parse-definition { } like "specializer" set-word-prop ] tri ;
92 ! Default specializers
93 { first first2 first3 first4 }
94 [ { array } "specializer" set-word-prop ] each
97 { vector } "specializer" set-word-prop
100 \ push { { vector } { sbuf } } "specializer" set-word-prop
102 \ last { { vector } } "specializer" set-word-prop
104 \ set-last { { object vector } } "specializer" set-word-prop
107 { { string sbuf } { array vector } { byte-array byte-vector } }
108 "specializer" set-word-prop
111 { { string string } { array array } }
112 "specializer" set-word-prop
115 { { fixnum fixnum string } { fixnum fixnum array } }
116 "specializer" set-word-prop
119 { { string } { array } }
120 "specializer" set-word-prop
124 "specializer" set-word-prop
126 \ >string { sbuf } "specializer" set-word-prop
128 \ >array { { vector } } "specializer" set-word-prop
130 \ >vector { { array } { vector } } "specializer" set-word-prop
132 \ >sbuf { string } "specializer" set-word-prop
134 \ split, { string string } "specializer" set-word-prop
136 \ memq? { array } "specializer" set-word-prop
138 \ member? { array } "specializer" set-word-prop
140 \ assoc-stack { vector } "specializer" set-word-prop
142 \ >le { { fixnum fixnum } { bignum fixnum } } "specializer" set-word-prop
144 \ >be { { bignum fixnum } { fixnum fixnum } } "specializer" set-word-prop
146 \ base> { string fixnum } "specializer" set-word-prop
148 M\ hashtable at* { { fixnum object } { word object } } "specializer" set-word-prop
150 M\ hashtable set-at { { object fixnum object } { object word object } } "specializer" set-word-prop