1 ! Copyright (C) 2007, 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien arrays assocs combinators.short-circuit
4 fry hashtables io kernel math namespaces prettyprint quotations
5 sequences sequences.deep shuffle slots.private vectors vocabs
6 words xml.data words.alias ;
9 SYMBOL: lint-definitions
10 SYMBOL: lint-definitions-keys
12 : set-hash-vector ( val key hash -- )
13 2dup at -rot [ ?push ] 2dip set-at ;
15 : manual-substitutions ( hash -- )
17 { -rot [ swap [ swap ] dip ] }
18 { -rot [ swap swapd ] }
19 { rot [ [ swap ] dip swap ] }
20 { rot [ swapd swap ] }
23 { swapd [ [ swap ] dip ] }
25 { 2drop [ drop drop ] }
26 { 3drop [ drop drop drop ] }
29 { >boolean [ f = not ] }
30 } swap '[ first2 _ set-hash-vector ] each ;
32 CONSTANT: trivial-defs
40 [ drop f ] [ 2drop ] [ 2drop t ]
42 [ first ] [ second ] [ third ] [ fourth ]
43 [ ">" write ] [ "/>" write ]
47 H{ } clone lint-definitions set-global
50 dup def>> dup callable?
51 [ lint-definitions get-global set-hash-vector ] [ drop ] if
54 ! ! Remove definitions
56 ! Remove empty word defs
57 lint-definitions get-global [ drop empty? not ] assoc-filter
59 ! Remove constants [ 1 ]
60 [ drop { [ length 1 = ] [ first number? ] } 1&& not ] assoc-filter
62 ! Remove words that are their own definition
63 [ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
66 [ nip [ vocabulary>> "specialized-" head? ] any? not ] assoc-filter
68 [ nip [ vocabulary>> "windows.messages" = ] any? not ] assoc-filter
70 [ nip [ alias? ] any? not ] assoc-filter
73 [ drop trivial-defs member? not ] assoc-filter
75 ! Remove numbers only defs
76 [ drop [ number? ] all? not ] assoc-filter
78 ! Remove curry only defs
79 [ drop [ \ curry = ] all? not ] assoc-filter
85 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
91 [ [ wrapper? ] deep-any? ]
92 [ [ hashtable? ] deep-any? ]
96 ! Remove n m shift defs
99 [ first2 [ number? ] both? ]
100 [ third \ shift = ] bi and not
107 [ first2 [ number? ] [ \ slot = ] bi* and not ] [ drop t ] if
110 dup manual-substitutions
112 [ lint-definitions set-global ] [ keys lint-definitions-keys set-global ] bi
114 : find-duplicates ( -- seq )
115 lint-definitions get-global [ nip length 1 > ] assoc-filter ;
117 GENERIC: lint ( obj -- seq )
119 M: object lint ( obj -- seq ) drop f ;
121 : subseq/member? ( subseq/member seq -- ? )
122 { [ start ] [ member? ] } 2|| ;
124 M: callable lint ( quot -- seq )
125 [ lint-definitions-keys get-global ] dip '[ _ subseq/member? ] filter ;
127 M: word lint ( word -- seq )
128 def>> dup callable? [ lint ] [ drop f ] if ;
130 : word-path. ( word -- )
131 [ vocabulary>> ] [ name>> ] bi ":" glue print ;
133 : 4bl ( -- ) bl bl bl bl ;
135 : (lint.) ( pair -- )
136 first2 [ word-path. ] dip [
137 [ 4bl . "-----------------------------------" print ]
138 [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
141 : lint. ( alist -- ) [ (lint.) ] each ;
143 GENERIC: run-lint ( obj -- obj )
145 : (trim-self) ( val key -- obj ? )
146 lint-definitions get-global at*
147 [ dupd remove empty? not ] [ drop f ] if ;
149 : trim-self ( seq -- newseq )
150 [ [ (trim-self) ] filter ] assoc-map ;
152 : filter-symbols ( alist -- alist )
154 nip first dup lint-definitions get-global at
155 [ first ] bi@ literalize = not
158 M: sequence run-lint ( seq -- seq )
159 [ dup lint ] { } map>assoc trim-self
160 [ second empty? not ] filter filter-symbols ;
162 M: word run-lint ( word -- seq ) 1array run-lint ;
164 : lint-all ( -- seq ) all-words run-lint dup lint. ;
166 : lint-vocab ( vocab -- seq ) words run-lint dup lint. ;
168 : lint-word ( word -- seq ) 1array run-lint dup lint. ;