1 ! Copyright (C) 2007, 2008, 2011 Doug Coleman.
2 ! See https://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 { dupd [ [ dup ] dip ] }
22 { 2dup [ over over ] }
23 { 2swap [ -roll -roll ] }
27 { 2drop [ drop drop ] }
28 { 3drop [ drop drop drop ] }
29 { 4drop [ drop drop drop drop ] }
33 { >boolean [ f = not ] }
34 { keep [ over [ call ] dip ] }
38 CONSTANT: trivial-defs
40 [ ">" write ] [ "/>" write ] [ " " write ]
46 [ 2dup = ] [ 2dup eq? ]
49 [ with each ] [ with map ]
52 [ empty? ] [ empty? not ]
53 [ dup empty? ] [ dup empty? not ]
54 [ 2dup both-fixnums? ]
59 : lintable-word? ( word -- ? )
61 [ vocabulary>> "specialized-" head? ]
62 [ vocabulary>> "windows-messages" = ]
66 : lintable-words ( -- words )
67 all-words [ lintable-word? ] filter ;
69 : ignore-def? ( def -- ? )
75 [ trivial-defs member? ]
77 ! Remove curry only defs
78 [ [ \ curry = ] all? ]
80 ! Remove words with locals
81 [ [ \ load-locals = ] any? ]
83 ! Remove stuff with wrappers
87 [ [ { [ number? ] [ { + - / * /i /f >integer } member? ] } 1|| ] all? ]
89 ! Remove more trival defs
93 [ first2 [ word? ] either? ]
94 [ first2 [ { dip dup over swap drop } member? ] either? ]
98 ! Remove [ V{ } clone ] and related
102 [ first { [ sequence? ] [ assoc? ] } 1|| ]
103 [ second { clone clone-like like assoc-like make } member? ]
107 ! Remove [ foo get ] and related
112 [ second { get get-global , % } member? ]
116 ! Remove [ first second ] and related
120 [ first { first second third } member? ]
121 [ second { first second third } member? ]
125 ! Remove [ [ trivial ] if ] and related
129 [ first { [ quotation? ] [ ignore-def? ] } 1&& ]
130 [ second { if if* unless unless* when when* curry } member? ]
134 ! Remove [ n - ] and related
138 [ first { [ number? ] [ boolean? ] } 1|| ]
139 [ second { + - / * < <= = >= > shift bitand bitor bitxor eq? } member? ]
143 ! Remove [ dup 0 > ] and related
147 [ first { dup over } member? ]
149 [ third { + - / * < <= = >= > } member? ]
153 ! Remove [ drop f f ] and related
157 [ first { drop 2drop 3drop nip 2nip 3nip 4nip } member? ]
158 [ rest-slice [ boolean? ] all? ]
162 ! Remove [ length 1 = ] and related
168 [ third { + - / * < <= = >= > } member? ]
172 ! Remove [ dup length 1 = ] and related
176 [ first { dup over } member? ]
177 [ second \ length = ]
179 [ fourth { + - / * < <= = >= > } member? ]
183 ! Remove numbers/t/f only defs
185 [ { [ number? ] [ boolean? ] } 1|| ] all?
188 ! Remove [ tag n eq? ]
192 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
196 ! Remove [ { foo } declare class-of ]
200 [ first { [ array? ] [ length 1 = ] } 1&& ]
201 [ second \ declare = ]
202 [ third \ class-of = ]
206 ! Remove [ m n shift ]
210 [ first2 [ number? ] both? ] [ third \ shift = ]
214 ! Remove [ layout-of n slot ]
218 [ first \ layout-of = ]
225 : all-callables ( def -- seq )
226 [ { [ callable? ] [ ignore-def? not ] } 1&& ] deep-filter ;
228 : (load-definitions) ( word def hash -- )
229 [ all-callables ] dip push-at-each ;
231 : load-definitions ( words -- hash )
232 H{ } clone [ '[ dup def>> _ (load-definitions) ] each ] keep ;
234 SYMBOL: lint-definitions
235 SYMBOL: lint-definitions-keys
237 : reload-definitions ( -- )
238 ! Load lintable and non-ignored definitions
239 lintable-words load-definitions
241 ! Remove words that are their own definition
242 [ [ [ def>> ] [ 1quotation ] bi = ] reject ] assoc-map
244 ! Add manual definitions
245 manual-substitutions over '[ _ push-at ] assoc-each
247 ! Set globals to new values
248 [ lint-definitions set-global ]
249 [ keys lint-definitions-keys set-global ] bi ;
251 : find-duplicates ( -- seq )
252 lint-definitions get-global [ length 1 > ] filter-values ;
254 GENERIC: lint ( obj -- seq )
256 M: object lint ( obj -- seq ) drop f ;
258 M: callable lint ( quot -- seq )
259 lint-definitions-keys get-global [ subseq-of? ] with filter ;
261 M: word lint ( word -- seq/f )
262 def>> [ callable? ] deep-filter [ lint ] map concat ;
264 : word-path. ( word -- )
265 [ vocabulary>> write ":" write ] [ . ] bi ;
267 : 4bl ( -- ) bl bl bl bl ;
269 : (lint.) ( pair -- )
270 first2 [ word-path. ] dip [
271 [ 4bl . "-----------------------------------" print ]
272 [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
275 : lint. ( alist -- ) [ (lint.) ] each ;
277 GENERIC: run-lint ( obj -- obj )
279 : (trim-self) ( val key -- obj ? )
280 lint-definitions get-global at*
281 [ dupd remove empty? not ] [ drop f ] if ;
283 : trim-self ( seq -- newseq )
284 [ [ (trim-self) ] filter ] assoc-map ;
286 : filter-symbols ( alist -- alist )
288 first dup lint-definitions get-global at
289 [ first ] bi@ literalize = not
292 M: sequence run-lint ( seq -- seq )
293 [ lint ] zip-with trim-self
294 [ second empty? ] reject filter-symbols ;
296 M: word run-lint ( word -- seq ) 1array run-lint ;
300 : find-swap/swap ( word -- ? )
301 def>> [ callable? ] deep-filter
304 [ [ \ swap = ] count 2 >= ]
306 { swap } split rest but-last
307 [ [ infer ] [ 2drop ( -- ) ] recover ( x -- x ) = ] any?
312 : find-redundant-word-props ( -- seq )
315 [ { [ foldable? ] [ flushable? ] } 1|| ]
320 : lint-all ( -- seq )
321 all-words run-lint dup lint. ;
323 : lint-vocab ( vocab -- seq )
324 vocab-words run-lint dup lint. ;
326 : lint-vocabs ( prefix -- seq )
327 [ loaded-vocab-names ] dip [ head? ] curry filter [ lint-vocab ] map ;
329 : lint-word ( word -- seq )
330 1array run-lint dup lint. ;