1 ! Copyright (C) 2007, 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors alien arrays assocs classes
5 classes.tuple.private combinators.short-circuit fry hashtables
6 io kernel kernel.private locals.backend make math namespaces
7 prettyprint quotations sequences sequences.deep shuffle
8 slots.private vectors vocabs words words.alias ;
14 CONSTANT: manual-substitutions
16 { -rot [ swap [ swap ] dip ] }
17 { -rot [ swap swapd ] }
18 { rot [ [ swap ] dip swap ] }
19 { rot [ swapd swap ] }
22 { swapd [ [ swap ] dip ] }
24 { 2drop [ drop drop ] }
25 { 3drop [ drop drop drop ] }
28 { >boolean [ f = not ] }
31 CONSTANT: trivial-defs
33 [ ">" write ] [ "/>" write ] [ " " write ]
39 [ 2dup = ] [ 2dup eq? ]
42 [ with each ] [ with map ]
45 [ empty? ] [ empty? not ]
46 [ dup empty? ] [ dup empty? not ]
49 : lintable-word? ( word -- ? )
51 [ vocabulary>> "specialized-" head? ]
52 [ vocabulary>> "windows-messages" = ]
56 : lintable-words ( -- words )
57 all-words [ lintable-word? ] filter ;
59 : ignore-def? ( def -- ? )
65 [ trivial-defs member? ]
67 ! Remove curry only defs
68 [ [ \ curry = ] all? ]
70 ! Remove words with locals
71 [ [ \ load-locals = ] any? ]
73 ! Remove stuff with wrappers
77 [ [ { [ number? ] [ { + - / * /i /f >integer } member? ] } 1|| ] all? ]
79 ! Remove more trival defs
83 [ first2 [ word? ] either? ]
84 [ first2 [ { dip dup over swap drop } member? ] either? ]
88 ! Remove [ V{ } clone ] and related
92 [ first { [ sequence? ] [ assoc? ] } 1|| ]
93 [ second { clone clone-like like assoc-like make make-assoc } member? ]
97 ! Remove [ foo get ] and related
102 [ second { get get-global , % } member? ]
106 ! Remove [ first second ] and related
110 [ first { first second third } member? ]
111 [ second { first second third } member? ]
115 ! Remove [ [ trivial ] if ] and related
119 [ first { [ quotation? ] [ ignore-def? ] } 1&& ]
120 [ second { if if* unless unless* when when* curry } member? ]
124 ! Remove [ n - ] and related
128 [ first { [ number? ] [ boolean? ] } 1|| ]
129 [ second { + - / * < <= = >= > shift bitand bitor bitxor eq? } member? ]
133 ! Remove [ dup 0 > ] and related
137 [ first { dup over } member? ]
139 [ third { + - / * < <= = >= > } member? ]
143 ! Remove [ drop f f ] and related
147 [ first { drop 2drop 3drop nip 2nip } member? ]
148 [ rest-slice [ boolean? ] all? ]
152 ! Remove [ length 1 = ] and related
158 [ third { + - / * < <= = >= > } member? ]
162 ! Remove [ dup length 1 = ] and related
166 [ first { dup over } member? ]
167 [ second \ length = ]
169 [ fourth { + - / * < <= = >= > } member? ]
173 ! Remove numbers/t/f only defs
175 [ { [ number? ] [ boolean? ] } 1|| ] all?
178 ! Remove [ tag n eq? ]
182 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
186 ! Remove [ { foo } declare class ]
190 [ first { [ array? ] [ length 1 = ] } 1&& ]
191 [ second \ declare = ]
196 ! Remove [ m n shift ]
200 [ first2 [ number? ] both? ] [ third \ shift = ]
204 ! Remove [ layout-of n slot ]
208 [ first \ layout-of = ]
215 : all-callables ( def -- seq )
216 [ { [ callable? ] [ ignore-def? not ] } 1&& ] deep-filter ;
218 : (load-definitions) ( word def hash -- )
219 [ all-callables ] dip '[ _ push-at ] with each ;
221 : load-definitions ( words -- hash )
222 H{ } clone [ '[ dup def>> _ (load-definitions) ] each ] keep ;
224 SYMBOL: lint-definitions
225 SYMBOL: lint-definitions-keys
227 : reload-definitions ( -- )
228 ! Load lintable and non-ignored definitions
229 lintable-words load-definitions
231 ! Remove words that are their own definition
232 [ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
234 ! Add manual definitions
235 manual-substitutions over '[ _ push-at ] assoc-each
237 ! Set globals to new values
238 [ lint-definitions set-global ]
239 [ keys lint-definitions-keys set-global ] bi ;
241 : find-duplicates ( -- seq )
242 lint-definitions get-global [ nip length 1 > ] assoc-filter ;
244 GENERIC: lint ( obj -- seq )
246 M: object lint ( obj -- seq ) drop f ;
248 M: callable lint ( quot -- seq )
249 [ lint-definitions-keys get-global ] dip '[ _ subseq? ] filter ;
251 M: word lint ( word -- seq/f )
252 def>> [ callable? ] deep-filter [ lint ] map concat ;
254 : word-path. ( word -- )
255 [ vocabulary>> write ":" write ] [ . ] bi ;
257 : 4bl ( -- ) bl bl bl bl ;
259 : (lint.) ( pair -- )
260 first2 [ word-path. ] dip [
261 [ 4bl . "-----------------------------------" print ]
262 [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
265 : lint. ( alist -- ) [ (lint.) ] each ;
267 GENERIC: run-lint ( obj -- obj )
269 : (trim-self) ( val key -- obj ? )
270 lint-definitions get-global at*
271 [ dupd remove empty? not ] [ drop f ] if ;
273 : trim-self ( seq -- newseq )
274 [ [ (trim-self) ] filter ] assoc-map ;
276 : filter-symbols ( alist -- alist )
278 nip first dup lint-definitions get-global at
279 [ first ] bi@ literalize = not
282 M: sequence run-lint ( seq -- seq )
283 [ dup lint ] { } map>assoc trim-self
284 [ second empty? not ] filter filter-symbols ;
286 M: word run-lint ( word -- seq ) 1array run-lint ;
290 : lint-all ( -- seq )
291 all-words run-lint dup lint. ;
293 : lint-vocab ( vocab -- seq )
294 words run-lint dup lint. ;
296 : lint-vocabs ( prefix -- seq )
297 [ vocabs ] dip [ head? ] curry filter [ lint-vocab ] map ;
299 : lint-word ( word -- seq )
300 1array run-lint dup lint. ;