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.integers.private math.parser math.parser.private
7 namespaces parser sbufs sequences splitting splitting.private strings
11 GENERIC: specializer-predicate ( spec -- quot )
13 M: class specializer-predicate "predicate" word-prop ;
15 M: object specializer-predicate '[ _ eq? ] ;
17 GENERIC: specializer-declaration ( spec -- class )
19 M: class specializer-declaration ;
21 M: object specializer-declaration class ;
23 : specializer ( word -- specializer )
24 "specializer" word-prop ;
26 : make-specializer ( specs -- quot )
28 [ (picker) 2array ] 2map
29 [ drop object eq? not ] assoc-filter
31 [ swap specializer-predicate append ] { } assoc>map
32 [ ] [ swap [ f ] \ if 3array append [ ] like ] map-reduce
35 : specializer-cases ( quot specializer -- alist )
36 dup [ array? ] all? [ 1array ] unless [
37 [ nip make-specializer ]
38 [ [ specializer-declaration ] map swap '[ _ declare @ ] ] 2bi
39 ] with { } map>assoc ;
41 : specialize-quot ( quot specializer -- quot' )
42 [ drop ] [ specializer-cases ] 2bi alist>quot ;
44 ! compiler.tree.propagation.inlining sets this to f
45 SYMBOL: specialize-method?
47 t specialize-method? set-global
49 : method-declaration ( method -- quot )
50 [ "method-generic" word-prop dispatch# object <array> ]
51 [ "method-class" word-prop ]
52 bi prefix [ declare ] curry [ ] like ;
54 : specialize-method ( quot method -- quot' )
55 [ specialize-method? get [ method-declaration prepend ] [ drop ] if ]
56 [ "method-generic" word-prop ] bi
57 specializer [ specialize-quot ] when* ;
59 : standard-method? ( method -- ? )
61 "method-generic" word-prop standard-generic?
64 : specialized-def ( word -- quot )
66 dup generic? [ drop ] [
67 [ dup standard-method? [ specialize-method ] [ drop ] if ]
68 [ specializer [ specialize-quot ] when* ]
72 : specialized-length ( specializer -- n )
73 dup [ array? ] all? [ first ] when length ;
76 scan-object dup wrapper? [ wrapped>> ] when
77 [ changed-definition ]
78 [ subwords [ changed-definition ] each ]
79 [ parse-definition { } like "specializer" set-word-prop ] tri ;
81 ! Default specializers
83 { vector } "specializer" set-word-prop
86 \ push { { vector } { sbuf } } "specializer" set-word-prop
88 \ last { { vector } } "specializer" set-word-prop
90 \ set-last { { object vector } } "specializer" set-word-prop
93 { { string sbuf } { array vector } { byte-array byte-vector } }
94 "specializer" set-word-prop
97 { { string string } { array array } }
98 "specializer" set-word-prop
101 { { fixnum fixnum string } { fixnum fixnum array } }
102 "specializer" set-word-prop
105 { { string } { array } }
106 "specializer" set-word-prop
110 "specializer" set-word-prop
112 \ >string { sbuf } "specializer" set-word-prop
114 \ >array { { vector } } "specializer" set-word-prop
116 \ >vector { { array } { vector } } "specializer" set-word-prop
118 \ >sbuf { string } "specializer" set-word-prop
120 \ split, { string string } "specializer" set-word-prop
122 \ member-eq? { array } "specializer" set-word-prop
124 \ member? { array } "specializer" set-word-prop
126 \ assoc-stack { vector } "specializer" set-word-prop
128 \ >le { { fixnum fixnum } { bignum fixnum } } "specializer" set-word-prop
130 \ >be { { bignum fixnum } { fixnum fixnum } } "specializer" set-word-prop
132 \ base> { string fixnum } "specializer" set-word-prop
134 M\ hashtable at* { { fixnum object } { word object } } "specializer" set-word-prop
136 M\ hashtable set-at { { object fixnum object } { object word object } } "specializer" set-word-prop
138 \ dec>float { string } "specializer" set-word-prop
140 \ hex>float { string } "specializer" set-word-prop
142 \ string>integer { string fixnum } "specializer" set-word-prop
144 \ bignum/f { { bignum bignum } { bignum fixnum } { fixnum bignum } { fixnum fixnum } } "specializer" set-word-prop