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 io kernel kernel.private
5 locals.backend make math math.private namespaces prettyprint
6 quotations sequences sequences.deep shuffle 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 ] }
20 { 2swap [ -roll -roll ] }
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 ]
47 [ 2dup both-fixnums? ]
52 : lintable-word? ( word -- ? )
54 [ vocabulary>> "specialized-" head? ]
55 [ vocabulary>> "windows-messages" = ]
59 : lintable-words ( -- words )
60 all-words [ lintable-word? ] filter ;
62 : ignore-def? ( def -- ? )
68 [ trivial-defs member? ]
70 ! Remove curry only defs
71 [ [ \ curry = ] all? ]
73 ! Remove words with locals
74 [ [ \ load-locals = ] any? ]
76 ! Remove stuff with wrappers
80 [ [ { [ number? ] [ { + - / * /i /f >integer } member? ] } 1|| ] all? ]
82 ! Remove more trival defs
86 [ first2 [ word? ] either? ]
87 [ first2 [ { dip dup over swap drop } member? ] either? ]
91 ! Remove [ V{ } clone ] and related
95 [ first { [ sequence? ] [ assoc? ] } 1|| ]
96 [ second { clone clone-like like assoc-like make } member? ]
100 ! Remove [ foo get ] and related
105 [ second { get get-global , % } member? ]
109 ! Remove [ first second ] and related
113 [ first { first second third } member? ]
114 [ second { first second third } member? ]
118 ! Remove [ [ trivial ] if ] and related
122 [ first { [ quotation? ] [ ignore-def? ] } 1&& ]
123 [ second { if if* unless unless* when when* curry } member? ]
127 ! Remove [ n - ] and related
131 [ first { [ number? ] [ boolean? ] } 1|| ]
132 [ second { + - / * < <= = >= > shift bitand bitor bitxor eq? } member? ]
136 ! Remove [ dup 0 > ] and related
140 [ first { dup over } member? ]
142 [ third { + - / * < <= = >= > } member? ]
146 ! Remove [ drop f f ] and related
150 [ first { drop 2drop 3drop nip 2nip 3nip 4nip } member? ]
151 [ rest-slice [ boolean? ] all? ]
155 ! Remove [ length 1 = ] and related
161 [ third { + - / * < <= = >= > } member? ]
165 ! Remove [ dup length 1 = ] and related
169 [ first { dup over } member? ]
170 [ second \ length = ]
172 [ fourth { + - / * < <= = >= > } member? ]
176 ! Remove numbers/t/f only defs
178 [ { [ number? ] [ boolean? ] } 1|| ] all?
181 ! Remove [ tag n eq? ]
185 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
189 ! Remove [ { foo } declare class-of ]
193 [ first { [ array? ] [ length 1 = ] } 1&& ]
194 [ second \ declare = ]
195 [ third \ class-of = ]
199 ! Remove [ m n shift ]
203 [ first2 [ number? ] both? ] [ third \ shift = ]
207 ! Remove [ layout-of n slot ]
211 [ first \ layout-of = ]
218 : all-callables ( def -- seq )
219 [ { [ callable? ] [ ignore-def? not ] } 1&& ] deep-filter ;
221 : (load-definitions) ( word def hash -- )
222 [ all-callables ] dip '[ _ push-at ] with each ;
224 : load-definitions ( words -- hash )
225 H{ } clone [ '[ dup def>> _ (load-definitions) ] each ] keep ;
227 SYMBOL: lint-definitions
228 SYMBOL: lint-definitions-keys
230 : reload-definitions ( -- )
231 ! Load lintable and non-ignored definitions
232 lintable-words load-definitions
234 ! Remove words that are their own definition
235 [ [ [ def>> ] [ 1quotation ] bi = ] reject ] assoc-map
237 ! Add manual definitions
238 manual-substitutions over '[ _ push-at ] assoc-each
240 ! Set globals to new values
241 [ lint-definitions set-global ]
242 [ keys lint-definitions-keys set-global ] bi ;
244 : find-duplicates ( -- seq )
245 lint-definitions get-global [ nip length 1 > ] assoc-filter ;
247 GENERIC: lint ( obj -- seq )
249 M: object lint ( obj -- seq ) drop f ;
251 M: callable lint ( quot -- seq )
252 lint-definitions-keys get-global [ find-subseq? ] with filter ;
254 M: word lint ( word -- seq/f )
255 def>> [ callable? ] deep-filter [ lint ] map concat ;
257 : word-path. ( word -- )
258 [ vocabulary>> write ":" write ] [ . ] bi ;
260 : 4bl ( -- ) bl bl bl bl ;
262 : (lint.) ( pair -- )
263 first2 [ word-path. ] dip [
264 [ 4bl . "-----------------------------------" print ]
265 [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
268 : lint. ( alist -- ) [ (lint.) ] each ;
270 GENERIC: run-lint ( obj -- obj )
272 : (trim-self) ( val key -- obj ? )
273 lint-definitions get-global at*
274 [ dupd remove empty? not ] [ drop f ] if ;
276 : trim-self ( seq -- newseq )
277 [ [ (trim-self) ] filter ] assoc-map ;
279 : filter-symbols ( alist -- alist )
281 nip first dup lint-definitions get-global at
282 [ first ] bi@ literalize = not
285 M: sequence run-lint ( seq -- seq )
286 [ lint ] zip-with trim-self
287 [ second empty? ] reject filter-symbols ;
289 M: word run-lint ( word -- seq ) 1array run-lint ;
293 : find-swap/swap ( word -- ? )
294 def>> [ callable? ] deep-filter
297 [ [ \ swap = ] count 2 >= ]
299 { swap } split rest but-last
300 [ [ infer ] [ 2drop ( -- ) ] recover ( x -- x ) = ] any?
305 : find-redundant-word-props ( -- seq )
308 [ { [ foldable? ] [ flushable? ] } 1|| ]
313 : lint-all ( -- seq )
314 all-words run-lint dup lint. ;
316 : lint-vocab ( vocab -- seq )
317 vocab-words run-lint dup lint. ;
319 : lint-vocabs ( prefix -- seq )
320 [ loaded-vocab-names ] dip [ head? ] curry filter [ lint-vocab ] map ;
322 : lint-word ( word -- seq )
323 1array run-lint dup lint. ;