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 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 : make-specializer ( specs -- quot )
24 [ (picker) 2array ] 2map
25 [ drop object eq? not ] assoc-filter
27 [ swap specializer-predicate append ] { } assoc>map
28 [ ] [ swap [ f ] \ if 3array append [ ] like ] map-reduce
31 : specializer-cases ( quot word -- default alist )
32 dup [ array? ] all? [ 1array ] unless [
33 [ make-specializer ] keep
34 [ specializer-declaration ] map '[ _ declare ] pick append
37 : specialize-quot ( quot specializer -- quot' )
38 specializer-cases alist>quot ;
40 ! compiler.tree.propagation.inlining sets this to f
41 SYMBOL: specialize-method?
43 t specialize-method? set-global
45 : method-declaration ( method -- quot )
46 [ "method-generic" word-prop dispatch# object <array> ]
47 [ "method-class" word-prop ]
48 bi prefix [ declare ] curry [ ] like ;
50 : specialize-method ( quot method -- quot' )
51 [ specialize-method? get [ method-declaration prepend ] [ drop ] if ]
52 [ "method-generic" word-prop "specializer" word-prop ] bi
53 [ specialize-quot ] when* ;
55 : standard-method? ( method -- ? )
57 "method-generic" word-prop standard-generic?
60 : specialized-def ( word -- quot )
62 dup generic? [ drop ] [
63 [ dup standard-method? [ specialize-method ] [ drop ] if ]
64 [ "specializer" word-prop [ specialize-quot ] when* ]
68 : specialized-length ( specializer -- n )
69 dup [ array? ] all? [ first ] when length ;
72 scan-object dup wrapper? [ wrapped>> ] when
73 [ changed-definition ]
74 [ parse-definition { } like "specializer" set-word-prop ] bi ;
76 ! Default specializers
77 { first first2 first3 first4 }
78 [ { array } "specializer" set-word-prop ] each
81 { vector } "specializer" set-word-prop
84 \ push { { vector } { sbuf } } "specializer" set-word-prop
86 \ last { { vector } } "specializer" set-word-prop
88 \ set-last { { object vector } } "specializer" set-word-prop
91 { { string sbuf } { array vector } { byte-array byte-vector } }
92 "specializer" set-word-prop
95 { { string string } { array array } }
96 "specializer" set-word-prop
99 { { fixnum fixnum string } { fixnum fixnum array } }
100 "specializer" set-word-prop
103 { { string } { array } }
104 "specializer" set-word-prop
108 "specializer" set-word-prop
110 \ >string { sbuf } "specializer" set-word-prop
112 \ >array { { vector } } "specializer" set-word-prop
114 \ >vector { { array } { vector } } "specializer" set-word-prop
116 \ >sbuf { string } "specializer" set-word-prop
118 \ split, { string string } "specializer" set-word-prop
120 \ memq? { array } "specializer" set-word-prop
122 \ member? { array } "specializer" set-word-prop
124 \ assoc-stack { vector } "specializer" set-word-prop
126 \ >le { { fixnum fixnum } { bignum fixnum } } "specializer" set-word-prop
128 \ >be { { bignum fixnum } { fixnum fixnum } } "specializer" set-word-prop
130 \ base> { string fixnum } "specializer" set-word-prop
132 M\ hashtable at* { { fixnum object } { word object } } "specializer" set-word-prop
134 M\ hashtable set-at { { object fixnum object } { object word object } } "specializer" set-word-prop