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 fry hashtables
6 io kernel kernel.private locals.backend make math namespaces
7 prettyprint quotations sequences sequences.deep shuffle
8 slots.private vectors vocabs words words.alias ;
14 CONSTANT: manual-substitutions
16 { -rot [ swap [ swap ] dip ] }
17 { -rot [ swap swapd ] }
18 { rot [ [ swap ] dip swap ] }
19 { rot [ swapd swap ] }
22 { swapd [ [ swap ] dip ] }
24 { 2drop [ drop drop ] }
25 { 3drop [ drop drop drop ] }
28 { >boolean [ f = not ] }
31 CONSTANT: trivial-defs
34 [ 2drop t ] [ 2drop f ]
35 [ 3drop t ] [ 3drop f ]
36 [ ">" write ] [ "/>" write ]
37 [ length 1 - ] [ length 1 = ] [ length 1 > ]
38 [ drop f f ] [ drop f t ] [ drop t f ] [ drop t t ]
39 [ 2drop f f ] [ 2drop f t ] [ 2drop t f ] [ 2drop t t ]
43 [ dup 0 > ] [ dup 0 <= ] [ dup 0 < ]
44 [ over 0 > ] [ over 0 <= ] [ over 0 < ]
47 [ dup 1 + ] [ drop 1 + ]
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 numbers/t/f only defs
76 [ { [ number? ] [ t? ] [ f eq? ] } 1|| ] all?
79 ! Remove [ tag n eq? ]
83 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
87 ! Remove [ { foo } declare class ]
91 [ first { [ array? ] [ length 1 = ] } 1&& ]
92 [ second \ declare = ]
97 ! Remove [ m n shift ]
101 [ first2 [ number? ] both? ] [ third \ shift = ]
105 ! Remove [ layout-of n slot ]
109 [ first \ layout-of = ]
115 ! Remove [ ... \ cdecl ]
117 { [ length 3 = ] [ last \ cdecl = ] } 1&&
121 : all-callables ( def -- seq )
122 [ { [ callable? ] [ ignore-def? not ] } 1&& ] deep-filter ;
124 : (load-definitions) ( word def hash -- )
125 [ all-callables ] dip '[ _ push-at ] with each ;
127 : load-definitions ( words -- hash )
128 H{ } clone [ '[ dup def>> _ (load-definitions) ] each ] keep ;
130 SYMBOL: lint-definitions
131 SYMBOL: lint-definitions-keys
133 : reload-definitions ( -- )
134 ! Load lintable and non-ignored definitions
135 lintable-words load-definitions
137 ! Remove words that are their own definition
138 [ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
140 ! Add manual definitions
141 manual-substitutions over '[ _ push-at ] assoc-each
143 ! Set globals to new values
144 [ lint-definitions set-global ]
145 [ keys lint-definitions-keys set-global ] bi ;
147 : find-duplicates ( -- seq )
148 lint-definitions get-global [ nip length 1 > ] assoc-filter ;
150 GENERIC: lint ( obj -- seq )
152 M: object lint ( obj -- seq ) drop f ;
154 M: callable lint ( quot -- seq )
155 [ lint-definitions-keys get-global ] dip '[ _ subseq? ] filter ;
157 M: word lint ( word -- seq/f )
158 def>> all-callables [ lint ] map concat ;
160 : word-path. ( word -- )
161 [ vocabulary>> write ":" write ] [ . ] bi ;
163 : 4bl ( -- ) bl bl bl bl ;
165 : (lint.) ( pair -- )
166 first2 [ word-path. ] dip [
167 [ 4bl . "-----------------------------------" print ]
168 [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
171 : lint. ( alist -- ) [ (lint.) ] each ;
173 GENERIC: run-lint ( obj -- obj )
175 : (trim-self) ( val key -- obj ? )
176 lint-definitions get-global at*
177 [ dupd remove empty? not ] [ drop f ] if ;
179 : trim-self ( seq -- newseq )
180 [ [ (trim-self) ] filter ] assoc-map ;
182 : filter-symbols ( alist -- alist )
184 nip first dup lint-definitions get-global at
185 [ first ] bi@ literalize = not
188 M: sequence run-lint ( seq -- seq )
189 [ dup lint ] { } map>assoc trim-self
190 [ second empty? not ] filter filter-symbols ;
192 M: word run-lint ( word -- seq ) 1array run-lint ;
196 : lint-all ( -- seq )
197 all-words run-lint dup lint. ;
199 : lint-vocab ( vocab -- seq )
200 words run-lint dup lint. ;
202 : lint-vocabs ( prefix -- seq )
203 [ vocabs ] dip [ head? ] curry filter [ lint-vocab ] map ;
205 : lint-word ( word -- seq )
206 1array run-lint dup lint. ;