1 ! Copyright (C) 2008, 2010 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.encodings
6 io.streams.string kernel kernel.private math
7 math.integers.private math.parser namespaces parser sbufs
8 sequences splitting splitting.private strings vectors words ;
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 )
27 dup length iota <reversed>
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 : method-declaration ( method -- quot )
45 [ "method-generic" word-prop dispatch# object <array> ]
46 [ "method-class" word-prop ]
47 bi prefix [ declare ] curry [ ] like ;
49 : specialize-method ( quot method -- quot' )
50 [ method-declaration prepend ]
51 [ "method-generic" word-prop ] bi
52 specializer [ specialize-quot ] when* ;
54 : standard-method? ( method -- ? )
56 "method-generic" word-prop standard-generic?
59 : specialized-def ( word -- quot )
61 dup generic? [ drop ] [
62 [ dup standard-method? [ specialize-method ] [ drop ] if ]
63 [ specializer [ specialize-quot ] when* ]
67 : specialized-length ( specializer -- n )
68 dup [ array? ] all? [ first ] when length ;
71 scan-object dup wrapper? [ wrapped>> ] when
72 [ changed-definition ]
73 [ subwords [ changed-definition ] each ]
74 [ parse-definition { } like "specializer" set-word-prop ] tri ;
76 ! Default specializers
78 { vector } "specializer" set-word-prop
81 \ push { { vector } { sbuf } } "specializer" set-word-prop
83 \ last { { vector } } "specializer" set-word-prop
85 \ set-last { { object vector } } "specializer" set-word-prop
88 { { string sbuf } { array vector } { byte-array byte-vector } }
89 "specializer" set-word-prop
92 { { string string } { array array } }
93 "specializer" set-word-prop
96 { { fixnum fixnum string } { fixnum fixnum array } }
97 "specializer" set-word-prop
100 { { string } { array } }
101 "specializer" set-word-prop
105 "specializer" set-word-prop
107 \ >string { sbuf } "specializer" set-word-prop
109 \ >array { { vector } } "specializer" set-word-prop
111 \ >vector { { array } { vector } } "specializer" set-word-prop
113 \ >sbuf { string } "specializer" set-word-prop
115 \ split, { string string } "specializer" set-word-prop
117 \ member-eq? { array } "specializer" set-word-prop
119 \ member? { array } "specializer" set-word-prop
121 \ assoc-stack { vector } "specializer" set-word-prop
123 \ >le { { fixnum fixnum } { bignum fixnum } } "specializer" set-word-prop
125 \ >be { { bignum fixnum } { fixnum fixnum } } "specializer" set-word-prop
127 \ base> { string fixnum } "specializer" set-word-prop
129 M\ hashtable at* { { fixnum object } { word object } } "specializer" set-word-prop
131 M\ hashtable set-at { { object fixnum object } { object word object } } "specializer" set-word-prop
133 \ bignum/f { { bignum bignum } { bignum fixnum } { fixnum bignum } { fixnum fixnum } } "specializer" set-word-prop
135 \ encode-string { string object object } "specializer" set-word-prop