]> gitweb.factorcode.org Git - factor.git/blob - basis/hints/hints.factor
Changing : foo ; parsing to SYNTAX: foo ;
[factor.git] / basis / hints / hints.factor
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
7 hashtables ;
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 : make-specializer ( specs -- quot )
23     dup length <reversed>
24     [ (picker) 2array ] 2map
25     [ drop object eq? not ] assoc-filter
26     [ [ t ] ] [
27         [ swap specializer-predicate append ] { } assoc>map
28         unclip [ swap [ f ] \ if 3array append [ ] like ] reduce
29     ] if-empty ;
30
31 : specializer-cases ( quot word -- default alist )
32     dup [ array? ] all? [ 1array ] unless [
33         [ make-specializer ] keep
34         [ specializer-declaration ] map '[ _ declare ] pick append
35     ] { } map>assoc ;
36
37 : method-declaration ( method -- quot )
38     [ "method-generic" word-prop dispatch# object <array> ]
39     [ "method-class" word-prop ]
40     bi prefix ;
41
42 : specialize-method ( quot method -- quot' )
43     method-declaration '[ _ declare ] prepend ;
44
45 : specialize-quot ( quot specializer -- quot' )
46     specializer-cases alist>quot ;
47
48 : standard-method? ( method -- ? )
49     dup method-body? [
50         "method-generic" word-prop standard-generic?
51     ] [ drop f ] if ;
52
53 : specialized-def ( word -- quot )
54     [ def>> ] keep
55     [ dup standard-method? [ specialize-method ] [ drop ] if ]
56     [ "specializer" word-prop [ specialize-quot ] when* ]
57     bi ;
58
59 : specialized-length ( specializer -- n )
60     dup [ array? ] all? [ first ] when length ;
61
62 SYNTAX: HINTS:
63     scan-object
64     dup method-spec? [ first2 method ] when
65     [ redefined ]
66     [ parse-definition "specializer" set-word-prop ] bi ;
67
68 ! Default specializers
69 { first first2 first3 first4 }
70 [ { array } "specializer" set-word-prop ] each
71
72 { peek pop* pop } [
73     { vector } "specializer" set-word-prop
74 ] each
75
76 \ push { { vector } { sbuf } } "specializer" set-word-prop
77
78 \ push-all
79 { { string sbuf } { array vector } { byte-array byte-vector } }
80 "specializer" set-word-prop
81
82 \ append
83 { { string string } { array array } }
84 "specializer" set-word-prop
85
86 \ subseq
87 { { fixnum fixnum string } { fixnum fixnum array } }
88 "specializer" set-word-prop
89
90 \ reverse-here
91 { { string } { array } }
92 "specializer" set-word-prop
93
94 \ mismatch
95 { string string }
96 "specializer" set-word-prop
97
98 \ >string { sbuf } "specializer" set-word-prop
99
100 \ >array { { vector } } "specializer" set-word-prop
101
102 \ >vector { { array } { vector } } "specializer" set-word-prop
103
104 \ >sbuf { string } "specializer" set-word-prop
105
106 \ split, { string string } "specializer" set-word-prop
107
108 \ memq? { array } "specializer" set-word-prop
109
110 \ member? { array } "specializer" set-word-prop
111
112 \ assoc-stack { vector } "specializer" set-word-prop
113
114 \ >le { { fixnum fixnum } { bignum fixnum } } "specializer" set-word-prop
115
116 \ >be { { bignum fixnum } { fixnum fixnum } } "specializer" set-word-prop
117
118 \ hashtable \ at* method { { fixnum hashtable } { word hashtable } } "specializer" set-word-prop
119
120 \ hashtable \ set-at method { { object fixnum object } { object word object } } "specializer" set-word-prop