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 continuations
6 fry hashtables io kernel kernel.private locals.backend make
7 math namespaces prettyprint quotations sequences sequences.deep
8 shuffle slots.private splitting stack-checker vectors vocabs
15 CONSTANT: manual-substitutions
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 ] }
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 ]
50 : lintable-word? ( word -- ? )
52 [ vocabulary>> "specialized-" head? ]
53 [ vocabulary>> "windows-messages" = ]
57 : lintable-words ( -- words )
58 all-words [ lintable-word? ] filter ;
60 : ignore-def? ( def -- ? )
66 [ trivial-defs member? ]
68 ! Remove curry only defs
69 [ [ \ curry = ] all? ]
71 ! Remove words with locals
72 [ [ \ load-locals = ] any? ]
74 ! Remove stuff with wrappers
78 [ [ { [ number? ] [ { + - / * /i /f >integer } member? ] } 1|| ] all? ]
80 ! Remove more trival defs
84 [ first2 [ word? ] either? ]
85 [ first2 [ { dip dup over swap drop } member? ] either? ]
89 ! Remove [ V{ } clone ] and related
93 [ first { [ sequence? ] [ assoc? ] } 1|| ]
94 [ second { clone clone-like like assoc-like make } member? ]
98 ! Remove [ foo get ] and related
103 [ second { get get-global , % } member? ]
107 ! Remove [ first second ] and related
111 [ first { first second third } member? ]
112 [ second { first second third } member? ]
116 ! Remove [ [ trivial ] if ] and related
120 [ first { [ quotation? ] [ ignore-def? ] } 1&& ]
121 [ second { if if* unless unless* when when* curry } member? ]
125 ! Remove [ n - ] and related
129 [ first { [ number? ] [ boolean? ] } 1|| ]
130 [ second { + - / * < <= = >= > shift bitand bitor bitxor eq? } member? ]
134 ! Remove [ dup 0 > ] and related
138 [ first { dup over } member? ]
140 [ third { + - / * < <= = >= > } member? ]
144 ! Remove [ drop f f ] and related
148 [ first { drop 2drop 3drop nip 2nip } member? ]
149 [ rest-slice [ boolean? ] all? ]
153 ! Remove [ length 1 = ] and related
159 [ third { + - / * < <= = >= > } member? ]
163 ! Remove [ dup length 1 = ] and related
167 [ first { dup over } member? ]
168 [ second \ length = ]
170 [ fourth { + - / * < <= = >= > } member? ]
174 ! Remove numbers/t/f only defs
176 [ { [ number? ] [ boolean? ] } 1|| ] all?
179 ! Remove [ tag n eq? ]
183 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
187 ! Remove [ { foo } declare class ]
191 [ first { [ array? ] [ length 1 = ] } 1&& ]
192 [ second \ declare = ]
197 ! Remove [ m n shift ]
201 [ first2 [ number? ] both? ] [ third \ shift = ]
205 ! Remove [ layout-of n slot ]
209 [ first \ layout-of = ]
216 : all-callables ( def -- seq )
217 [ { [ callable? ] [ ignore-def? not ] } 1&& ] deep-filter ;
219 : (load-definitions) ( word def hash -- )
220 [ all-callables ] dip '[ _ push-at ] with each ;
222 : load-definitions ( words -- hash )
223 H{ } clone [ '[ dup def>> _ (load-definitions) ] each ] keep ;
225 SYMBOL: lint-definitions
226 SYMBOL: lint-definitions-keys
228 : reload-definitions ( -- )
229 ! Load lintable and non-ignored definitions
230 lintable-words load-definitions
232 ! Remove words that are their own definition
233 [ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
235 ! Add manual definitions
236 manual-substitutions over '[ _ push-at ] assoc-each
238 ! Set globals to new values
239 [ lint-definitions set-global ]
240 [ keys lint-definitions-keys set-global ] bi ;
242 : find-duplicates ( -- seq )
243 lint-definitions get-global [ nip length 1 > ] assoc-filter ;
245 GENERIC: lint ( obj -- seq )
247 M: object lint ( obj -- seq ) drop f ;
249 M: callable lint ( quot -- seq )
250 [ lint-definitions-keys get-global ] dip '[ _ subseq? ] filter ;
252 M: word lint ( word -- seq/f )
253 def>> [ callable? ] deep-filter [ lint ] map concat ;
255 : word-path. ( word -- )
256 [ vocabulary>> write ":" write ] [ . ] bi ;
258 : 4bl ( -- ) bl bl bl bl ;
260 : (lint.) ( pair -- )
261 first2 [ word-path. ] dip [
262 [ 4bl . "-----------------------------------" print ]
263 [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
266 : lint. ( alist -- ) [ (lint.) ] each ;
268 GENERIC: run-lint ( obj -- obj )
270 : (trim-self) ( val key -- obj ? )
271 lint-definitions get-global at*
272 [ dupd remove empty? not ] [ drop f ] if ;
274 : trim-self ( seq -- newseq )
275 [ [ (trim-self) ] filter ] assoc-map ;
277 : filter-symbols ( alist -- alist )
279 nip first dup lint-definitions get-global at
280 [ first ] bi@ literalize = not
283 M: sequence run-lint ( seq -- seq )
284 [ dup lint ] { } map>assoc trim-self
285 [ second empty? not ] filter filter-symbols ;
287 M: word run-lint ( word -- seq ) 1array run-lint ;
291 : find-swap/swap ( word -- ? )
292 def>> [ callable? ] deep-filter
295 [ [ \ swap = ] count 2 >= ]
297 { swap } split rest but-last
298 [ [ infer ] [ 2drop ( -- ) ] recover ( x -- x ) = ] any?
303 : find-redundant-word-props ( -- seq )
306 [ { [ foldable? ] [ flushable? ] } 1|| ]
311 : lint-all ( -- seq )
312 all-words run-lint dup lint. ;
314 : lint-vocab ( vocab -- seq )
315 words run-lint dup lint. ;
317 : lint-vocabs ( prefix -- seq )
318 [ vocabs ] dip [ head? ] curry filter [ lint-vocab ] map ;
320 : lint-word ( word -- seq )
321 1array run-lint dup lint. ;