1 ! Copyright (C) 2007, 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien arrays assocs classes.tuple.private
4 combinators.short-circuit fry hashtables io kernel
5 locals.backend make math namespaces prettyprint quotations
6 sequences sequences.deep shuffle slots.private vectors vocabs
7 words xml.data words.alias ;
12 CONSTANT: manual-substitutions
14 { -rot [ swap [ swap ] dip ] }
15 { -rot [ swap swapd ] }
16 { rot [ [ swap ] dip swap ] }
17 { rot [ swapd swap ] }
20 { swapd [ [ swap ] dip ] }
22 { 2drop [ drop drop ] }
23 { 3drop [ drop drop drop ] }
26 { >boolean [ f = not ] }
29 CONSTANT: trivial-defs
32 [ 2drop t ] [ 2drop f ]
33 [ 3drop t ] [ 3drop f ]
34 [ ">" write ] [ "/>" write ]
35 [ length 1 - ] [ length 1 = ] [ length 1 > ]
36 [ drop f f ] [ 2drop f f ]
40 [ dup 0 > ] [ dup 0 <= ]
46 : lintable-word? ( word -- ? )
48 [ vocabulary>> "specialized-" head? ]
49 [ vocabulary>> "windows-messages" = ]
53 : lintable-words ( -- words )
54 all-words [ lintable-word? ] filter ;
56 : ignore-def? ( def -- ? )
62 [ trivial-defs member? ]
64 ! Remove curry only defs
65 [ [ \ curry = ] all? ]
67 ! Remove words with locals
68 [ [ \ load-locals = ] any? ]
70 ! Remove numbers/t/f only defs
72 [ { [ number? ] [ t? ] [ f eq? ] } 1|| ] all?
79 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
83 ! Remove [ m n shift ]
87 [ first2 [ number? ] both? ] [ third \ shift = ]
91 ! Remove [ layout-of n slot ]
95 [ first \ layout-of = ]
102 : all-callables ( def -- seq )
103 [ callable? ] deep-filter ;
105 : (load-definitions) ( word def hash -- )
106 [ all-callables ] dip '[ _ push-at ] with each ;
108 : load-definitions ( words -- hash )
109 H{ } clone [ '[ dup def>> _ (load-definitions) ] each ] keep ;
111 SYMBOL: lint-definitions
112 SYMBOL: lint-definitions-keys
114 : reload-definitions ( -- )
115 ! Load lintable and non-ignored definitions
116 lintable-words load-definitions
117 [ drop ignore-def? not ] assoc-filter
119 ! Remove words that are their own definition
120 [ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
122 ! Add manual definitions
123 manual-substitutions over '[ _ push-at ] assoc-each
125 ! Set globals to new values
126 [ lint-definitions set-global ]
127 [ keys lint-definitions-keys set-global ] bi ;
129 : find-duplicates ( -- seq )
130 lint-definitions get-global [ nip length 1 > ] assoc-filter ;
132 GENERIC: lint ( obj -- seq )
134 M: object lint ( obj -- seq ) drop f ;
136 M: callable lint ( quot -- seq )
137 [ lint-definitions-keys get-global ] dip '[ _ subseq? ] filter ;
139 M: word lint ( word -- seq/f )
140 def>> all-callables [ lint ] map concat ;
142 : word-path. ( word -- )
143 [ vocabulary>> write ":" write ] [ . ] bi ;
145 : 4bl ( -- ) bl bl bl bl ;
147 : (lint.) ( pair -- )
148 first2 [ word-path. ] dip [
149 [ 4bl . "-----------------------------------" print ]
150 [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
153 : lint. ( alist -- ) [ (lint.) ] each ;
155 GENERIC: run-lint ( obj -- obj )
157 : (trim-self) ( val key -- obj ? )
158 lint-definitions get-global at*
159 [ dupd remove empty? not ] [ drop f ] if ;
161 : trim-self ( seq -- newseq )
162 [ [ (trim-self) ] filter ] assoc-map ;
164 : filter-symbols ( alist -- alist )
166 nip first dup lint-definitions get-global at
167 [ first ] bi@ literalize = not
170 M: sequence run-lint ( seq -- seq )
171 [ dup lint ] { } map>assoc trim-self
172 [ second empty? not ] filter filter-symbols ;
174 M: word run-lint ( word -- seq ) 1array run-lint ;
178 : lint-all ( -- seq )
179 all-words run-lint dup lint. ;
181 : lint-vocab ( vocab -- seq )
182 words run-lint dup lint. ;
184 : lint-vocabs ( prefix -- seq )
185 [ vocabs ] dip [ head? ] curry filter [ lint-vocab ] map ;
187 : lint-word ( word -- seq )
188 1array run-lint dup lint. ;