1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: parser words definitions kernel sequences assocs arrays
4 kernel.private fry combinators accessors vectors strings sbufs
5 byte-arrays byte-vectors io.binary io.streams.string splitting
6 math generic generic.standard generic.standard.engines classes ;
9 GENERIC: specializer-predicate ( spec -- quot )
11 M: class specializer-predicate "predicate" word-prop ;
13 M: object specializer-predicate '[ _ eq? ] ;
15 GENERIC: specializer-declaration ( spec -- class )
17 M: class specializer-declaration ;
19 M: object specializer-declaration class ;
21 : make-specializer ( specs -- quot )
23 [ (picker) 2array ] 2map
24 [ drop object eq? not ] assoc-filter
26 [ swap specializer-predicate append ] { } assoc>map
27 unclip [ swap [ f ] \ if 3array append [ ] like ] reduce
30 : specializer-cases ( quot word -- default alist )
31 dup [ array? ] all? [ 1array ] unless [
32 [ make-specializer ] keep
33 [ specializer-declaration ] map '[ _ declare ] pick append
36 : method-declaration ( method -- quot )
37 [ "method-generic" word-prop dispatch# object <array> ]
38 [ "method-class" word-prop ]
41 : specialize-method ( quot method -- quot' )
42 method-declaration '[ _ declare ] prepend ;
44 : specialize-quot ( quot specializer -- quot' )
45 specializer-cases alist>quot ;
47 : standard-method? ( method -- ? )
49 "method-generic" word-prop standard-generic?
52 : specialized-def ( word -- quot )
55 [ dup "specializer" word-prop ]
56 [ "specializer" word-prop specialize-quot ]
58 { [ dup standard-method? ] [ specialize-method ] }
62 : specialized-length ( specializer -- n )
63 dup [ array? ] all? [ first ] when length ;
67 dup method-spec? [ first2 method ] when
69 [ parse-definition "specializer" set-word-prop ] bi ;
72 ! Default specializers
73 { first first2 first3 first4 }
74 [ { array } "specializer" set-word-prop ] each
77 { vector } "specializer" set-word-prop
80 \ push { { vector } { sbuf } } "specializer" set-word-prop
83 { { string sbuf } { array vector } { byte-array byte-vector } }
84 "specializer" set-word-prop
87 { { string string } { array array } }
88 "specializer" set-word-prop
91 { { fixnum fixnum string } { fixnum fixnum array } }
92 "specializer" set-word-prop
95 { { string } { array } }
96 "specializer" set-word-prop
100 "specializer" set-word-prop
102 \ find-last-sep { string sbuf } "specializer" set-word-prop
104 \ >string { sbuf } "specializer" set-word-prop
106 \ >array { { vector } } "specializer" set-word-prop
108 \ >vector { { array } { vector } } "specializer" set-word-prop
110 \ >sbuf { string } "specializer" set-word-prop
112 \ split, { string string } "specializer" set-word-prop
114 \ memq? { array } "specializer" set-word-prop
116 \ member? { array } "specializer" set-word-prop
118 \ assoc-stack { vector } "specializer" set-word-prop
120 \ >le { { fixnum fixnum } { bignum fixnum } } "specializer" set-word-prop
122 \ >be { { bignum fixnum } { fixnum fixnum } } "specializer" set-word-prop