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