1 ! Copyright (C) 2007, 2008, 2011 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes classes.tuple.private
4 combinators.short-circuit continuations fry io kernel
5 kernel.private locals.backend make math math.private namespaces
6 prettyprint quotations sequences sequences.deep slots.private
7 splitting stack-checker vocabs words words.alias ;
12 CONSTANT: manual-substitutions
14 { -rot [ swap [ swap ] dip ] }
15 { -rot [ swap swapd ] }
16 { rot [ [ swap ] dip swap ] }
17 { rot [ swapd swap ] }
19 { swapd [ [ swap ] dip ] }
21 { 2drop [ drop drop ] }
22 { 3drop [ drop drop drop ] }
25 { >boolean [ f = not ] }
28 CONSTANT: trivial-defs
30 [ ">" write ] [ "/>" write ] [ " " write ]
36 [ 2dup = ] [ 2dup eq? ]
39 [ with each ] [ with map ]
42 [ empty? ] [ empty? not ]
43 [ dup empty? ] [ dup empty? not ]
44 [ 2dup both-fixnums? ]
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 } 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-of ]
190 [ first { [ array? ] [ length 1 = ] } 1&& ]
191 [ second \ declare = ]
192 [ third \ class-of = ]
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 = ] reject ] 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 [ subseq? ] with 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? ] reject filter-symbols ;
286 M: word run-lint ( word -- seq ) 1array run-lint ;
290 : find-swap/swap ( word -- ? )
291 def>> [ callable? ] deep-filter
294 [ [ \ swap = ] count 2 >= ]
296 { swap } split rest but-last
297 [ [ infer ] [ 2drop ( -- ) ] recover ( x -- x ) = ] any?
302 : find-redundant-word-props ( -- seq )
305 [ { [ foldable? ] [ flushable? ] } 1|| ]
310 : lint-all ( -- seq )
311 all-words run-lint dup lint. ;
313 : lint-vocab ( vocab -- seq )
314 vocab-words run-lint dup lint. ;
316 : lint-vocabs ( prefix -- seq )
317 [ loaded-vocab-names ] dip [ head? ] curry filter [ lint-vocab ] map ;
319 : lint-word ( word -- seq )
320 1array run-lint dup lint. ;