]> gitweb.factorcode.org Git - factor.git/blob - basis/hints/hints.factor
make first2, first3, first4 inline
[factor.git] / basis / hints / hints.factor
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.parser namespaces parser sbufs
7 sequences splitting splitting.private strings vectors words ;
8 IN: hints
9
10 GENERIC: specializer-predicate ( spec -- quot )
11
12 M: class specializer-predicate "predicate" word-prop ;
13
14 M: object specializer-predicate '[ _ eq? ] ;
15
16 GENERIC: specializer-declaration ( spec -- class )
17
18 M: class specializer-declaration ;
19
20 M: object specializer-declaration class ;
21
22 : specializer ( word -- specializer )
23     "specializer" word-prop ;
24
25 : make-specializer ( specs -- quot )
26     dup length <reversed>
27     [ (picker) 2array ] 2map
28     [ drop object eq? not ] assoc-filter
29     [ [ t ] ] [
30         [ swap specializer-predicate append ] { } assoc>map
31         [ ] [ swap [ f ] \ if 3array append [ ] like ] map-reduce
32     ] if-empty ;
33
34 : specializer-cases ( quot specializer -- alist )
35     dup [ array? ] all? [ 1array ] unless [
36         [ nip make-specializer ]
37         [ [ specializer-declaration ] map swap '[ _ declare @ ] ] 2bi
38     ] with { } map>assoc ;
39
40 : specialize-quot ( quot specializer -- quot' )
41     [ drop ] [ specializer-cases ] 2bi alist>quot ;
42
43 ! compiler.tree.propagation.inlining sets this to f
44 SYMBOL: specialize-method?
45
46 t specialize-method? set-global
47
48 : method-declaration ( method -- quot )
49     [ "method-generic" word-prop dispatch# object <array> ]
50     [ "method-class" word-prop ]
51     bi prefix [ declare ] curry [ ] like ;
52
53 : specialize-method ( quot method -- quot' )
54     [ specialize-method? get [ method-declaration prepend ] [ drop ] if ]
55     [ "method-generic" word-prop ] bi
56     specializer [ specialize-quot ] when* ;
57
58 : standard-method? ( method -- ? )
59     dup method-body? [
60         "method-generic" word-prop standard-generic?
61     ] [ drop f ] if ;
62
63 : specialized-def ( word -- quot )
64     [ def>> ] keep
65     dup generic? [ drop ] [
66         [ dup standard-method? [ specialize-method ] [ drop ] if ]
67         [ specializer [ specialize-quot ] when* ]
68         bi
69     ] if ;
70
71 : specialized-length ( specializer -- n )
72     dup [ array? ] all? [ first ] when length ;
73
74 SYNTAX: HINTS:
75     scan-object dup wrapper? [ wrapped>> ] when
76     [ changed-definition ]
77     [ subwords [ changed-definition ] each ]
78     [ parse-definition { } like "specializer" set-word-prop ] tri ;
79
80 ! Default specializers
81 { last pop* pop } [
82     { vector } "specializer" set-word-prop
83 ] each
84
85 \ push { { vector } { sbuf } } "specializer" set-word-prop
86
87 \ last { { vector } } "specializer" set-word-prop
88
89 \ set-last { { object vector } } "specializer" set-word-prop
90
91 \ push-all
92 { { string sbuf } { array vector } { byte-array byte-vector } }
93 "specializer" set-word-prop
94
95 \ append
96 { { string string } { array array } }
97 "specializer" set-word-prop
98
99 \ subseq
100 { { fixnum fixnum string } { fixnum fixnum array } }
101 "specializer" set-word-prop
102
103 \ reverse-here
104 { { string } { array } }
105 "specializer" set-word-prop
106
107 \ mismatch
108 { string string }
109 "specializer" set-word-prop
110
111 \ >string { sbuf } "specializer" set-word-prop
112
113 \ >array { { vector } } "specializer" set-word-prop
114
115 \ >vector { { array } { vector } } "specializer" set-word-prop
116
117 \ >sbuf { string } "specializer" set-word-prop
118
119 \ split, { string string } "specializer" set-word-prop
120
121 \ memq? { array } "specializer" set-word-prop
122
123 \ member? { array } "specializer" set-word-prop
124
125 \ assoc-stack { vector } "specializer" set-word-prop
126
127 \ >le { { fixnum fixnum } { bignum fixnum } } "specializer" set-word-prop
128
129 \ >be { { bignum fixnum } { fixnum fixnum } } "specializer" set-word-prop
130
131 \ base> { string fixnum } "specializer" set-word-prop
132
133 M\ hashtable at* { { fixnum object } { word object } } "specializer" set-word-prop
134
135 M\ hashtable set-at { { object fixnum object } { object word object } } "specializer" set-word-prop