1 ! Copyright (C) 2007, 2008, 2011 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs assocs.extras classes
4 classes.tuple.private combinators.short-circuit continuations io
5 kernel kernel.private locals.backend make math math.private
6 namespaces prettyprint quotations sequences sequences.deep
7 shuffle slots.private splitting stack-checker vocabs words
13 CONSTANT: manual-substitutions
15 { -rot [ swap [ swap ] dip ] }
16 { -rot [ swap swapd ] }
17 { rot [ [ swap ] dip swap ] }
18 { rot [ swapd swap ] }
20 { swapd [ [ swap ] dip ] }
21 { 2swap [ -roll -roll ] }
25 { 2drop [ drop drop ] }
26 { 3drop [ drop drop drop ] }
29 { >boolean [ f = not ] }
32 CONSTANT: trivial-defs
34 [ ">" write ] [ "/>" write ] [ " " write ]
40 [ 2dup = ] [ 2dup eq? ]
43 [ with each ] [ with map ]
46 [ empty? ] [ empty? not ]
47 [ dup empty? ] [ dup empty? not ]
48 [ 2dup both-fixnums? ]
53 : lintable-word? ( word -- ? )
55 [ vocabulary>> "specialized-" head? ]
56 [ vocabulary>> "windows-messages" = ]
60 : lintable-words ( -- words )
61 all-words [ lintable-word? ] filter ;
63 : ignore-def? ( def -- ? )
69 [ trivial-defs member? ]
71 ! Remove curry only defs
72 [ [ \ curry = ] all? ]
74 ! Remove words with locals
75 [ [ \ load-locals = ] any? ]
77 ! Remove stuff with wrappers
81 [ [ { [ number? ] [ { + - / * /i /f >integer } member? ] } 1|| ] all? ]
83 ! Remove more trival defs
87 [ first2 [ word? ] either? ]
88 [ first2 [ { dip dup over swap drop } member? ] either? ]
92 ! Remove [ V{ } clone ] and related
96 [ first { [ sequence? ] [ assoc? ] } 1|| ]
97 [ second { clone clone-like like assoc-like make } member? ]
101 ! Remove [ foo get ] and related
106 [ second { get get-global , % } member? ]
110 ! Remove [ first second ] and related
114 [ first { first second third } member? ]
115 [ second { first second third } member? ]
119 ! Remove [ [ trivial ] if ] and related
123 [ first { [ quotation? ] [ ignore-def? ] } 1&& ]
124 [ second { if if* unless unless* when when* curry } member? ]
128 ! Remove [ n - ] and related
132 [ first { [ number? ] [ boolean? ] } 1|| ]
133 [ second { + - / * < <= = >= > shift bitand bitor bitxor eq? } member? ]
137 ! Remove [ dup 0 > ] and related
141 [ first { dup over } member? ]
143 [ third { + - / * < <= = >= > } member? ]
147 ! Remove [ drop f f ] and related
151 [ first { drop 2drop 3drop nip 2nip 3nip 4nip } member? ]
152 [ rest-slice [ boolean? ] all? ]
156 ! Remove [ length 1 = ] and related
162 [ third { + - / * < <= = >= > } member? ]
166 ! Remove [ dup length 1 = ] and related
170 [ first { dup over } member? ]
171 [ second \ length = ]
173 [ fourth { + - / * < <= = >= > } member? ]
177 ! Remove numbers/t/f only defs
179 [ { [ number? ] [ boolean? ] } 1|| ] all?
182 ! Remove [ tag n eq? ]
186 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
190 ! Remove [ { foo } declare class-of ]
194 [ first { [ array? ] [ length 1 = ] } 1&& ]
195 [ second \ declare = ]
196 [ third \ class-of = ]
200 ! Remove [ m n shift ]
204 [ first2 [ number? ] both? ] [ third \ shift = ]
208 ! Remove [ layout-of n slot ]
212 [ first \ layout-of = ]
219 : all-callables ( def -- seq )
220 [ { [ callable? ] [ ignore-def? not ] } 1&& ] deep-filter ;
222 : (load-definitions) ( word def hash -- )
223 [ all-callables ] dip push-at-each ;
225 : load-definitions ( words -- hash )
226 H{ } clone [ '[ dup def>> _ (load-definitions) ] each ] keep ;
228 SYMBOL: lint-definitions
229 SYMBOL: lint-definitions-keys
231 : reload-definitions ( -- )
232 ! Load lintable and non-ignored definitions
233 lintable-words load-definitions
235 ! Remove words that are their own definition
236 [ [ [ def>> ] [ 1quotation ] bi = ] reject ] assoc-map
238 ! Add manual definitions
239 manual-substitutions over '[ _ push-at ] assoc-each
241 ! Set globals to new values
242 [ lint-definitions set-global ]
243 [ keys lint-definitions-keys set-global ] bi ;
245 : find-duplicates ( -- seq )
246 lint-definitions get-global [ nip length 1 > ] assoc-filter ;
248 GENERIC: lint ( obj -- seq )
250 M: object lint ( obj -- seq ) drop f ;
252 M: callable lint ( quot -- seq )
253 lint-definitions-keys get-global [ subseq-of? ] with filter ;
255 M: word lint ( word -- seq/f )
256 def>> [ callable? ] deep-filter [ lint ] map concat ;
258 : word-path. ( word -- )
259 [ vocabulary>> write ":" write ] [ . ] bi ;
261 : 4bl ( -- ) bl bl bl bl ;
263 : (lint.) ( pair -- )
264 first2 [ word-path. ] dip [
265 [ 4bl . "-----------------------------------" print ]
266 [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
269 : lint. ( alist -- ) [ (lint.) ] each ;
271 GENERIC: run-lint ( obj -- obj )
273 : (trim-self) ( val key -- obj ? )
274 lint-definitions get-global at*
275 [ dupd remove empty? not ] [ drop f ] if ;
277 : trim-self ( seq -- newseq )
278 [ [ (trim-self) ] filter ] assoc-map ;
280 : filter-symbols ( alist -- alist )
282 nip first dup lint-definitions get-global at
283 [ first ] bi@ literalize = not
286 M: sequence run-lint ( seq -- seq )
287 [ lint ] zip-with trim-self
288 [ second empty? ] reject filter-symbols ;
290 M: word run-lint ( word -- seq ) 1array run-lint ;
294 : find-swap/swap ( word -- ? )
295 def>> [ callable? ] deep-filter
298 [ [ \ swap = ] count 2 >= ]
300 { swap } split rest but-last
301 [ [ infer ] [ 2drop ( -- ) ] recover ( x -- x ) = ] any?
306 : find-redundant-word-props ( -- seq )
309 [ { [ foldable? ] [ flushable? ] } 1|| ]
314 : lint-all ( -- seq )
315 all-words run-lint dup lint. ;
317 : lint-vocab ( vocab -- seq )
318 vocab-words run-lint dup lint. ;
320 : lint-vocabs ( prefix -- seq )
321 [ loaded-vocab-names ] dip [ head? ] curry filter [ lint-vocab ] map ;
323 : lint-word ( word -- seq )
324 1array run-lint dup lint. ;