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 ;
9 : (make-specializer) ( class picker -- quot )
10 swap "predicate" word-prop append ;
12 : make-specializer ( classes -- quot )
14 [ (picker) 2array ] 2map
15 [ drop object eq? not ] assoc-filter
17 [ (make-specializer) ] { } assoc>map
18 unclip [ swap [ f ] \ if 3array append [ ] like ] reduce
21 : specializer-cases ( quot word -- default alist )
22 dup [ array? ] all? [ 1array ] unless [
23 [ make-specializer ] keep
24 '[ _ declare ] pick append
27 : method-declaration ( method -- quot )
28 [ "method-generic" word-prop dispatch# object <array> ]
29 [ "method-class" word-prop ]
32 : specialize-method ( quot method -- quot' )
33 method-declaration '[ _ declare ] prepend ;
35 : specialize-quot ( quot specializer -- quot' )
36 specializer-cases alist>quot ;
38 : standard-method? ( method -- ? )
40 "method-generic" word-prop standard-generic?
43 : specialized-def ( word -- quot )
45 { [ dup standard-method? ] [ specialize-method ] }
47 [ dup "specializer" word-prop ]
48 [ "specializer" word-prop specialize-quot ]
53 : specialized-length ( specializer -- n )
54 dup [ array? ] all? [ first ] when length ;
59 [ parse-definition "specializer" set-word-prop ] bi ;
62 ! Default specializers
63 { first first2 first3 first4 }
64 [ { array } "specializer" set-word-prop ] each
66 { peek pop* pop push } [
67 { vector } "specializer" set-word-prop
71 { { string sbuf } { array vector } { byte-array byte-vector } }
72 "specializer" set-word-prop
75 { { string string } { array array } }
76 "specializer" set-word-prop
79 { { fixnum fixnum string } { fixnum fixnum array } }
80 "specializer" set-word-prop
83 { { string } { array } }
84 "specializer" set-word-prop
88 "specializer" set-word-prop
90 \ find-last-sep { string sbuf } "specializer" set-word-prop
92 \ >string { sbuf } "specializer" set-word-prop
94 \ >array { { string } { vector } } "specializer" set-word-prop
96 \ >vector { { array } { vector } } "specializer" set-word-prop
98 \ >sbuf { string } "specializer" set-word-prop
100 \ split, { string string } "specializer" set-word-prop
102 \ memq? { array } "specializer" set-word-prop
104 \ member? { fixnum string } "specializer" set-word-prop
106 \ assoc-stack { vector } "specializer" set-word-prop
108 \ >le { { fixnum fixnum } { bignum fixnum } } "specializer" set-word-prop
110 \ >be { { bignum fixnum } { fixnum fixnum } } "specializer" set-word-prop