1 ! Copyright (C) 2007, 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien arrays assocs combinators.short-circuit
4 fry hashtables io kernel math namespaces prettyprint quotations
5 sequences sequences.deep shuffle slots.private vectors vocabs
6 words xml.data words.alias ;
9 SYMBOL: lint-definitions
10 SYMBOL: lint-definitions-keys
12 : set-hash-vector ( val key hash -- )
13 2dup at -rot [ ?push ] 2dip set-at ;
15 : manual-substitutions ( hash -- )
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 ] }
30 } swap '[ first2 _ set-hash-vector ] each ;
32 CONSTANT: trivial-defs
34 [ drop ] [ 2drop ] [ 2array ]
42 [ drop t ] [ drop f ] [ 2drop t ] [ 2drop f ]
44 [ first ] [ second ] [ third ] [ fourth ]
45 [ ">" write ] [ "/>" write ]
49 H{ } clone lint-definitions set-global
52 dup def>> dup callable?
53 [ lint-definitions get-global set-hash-vector ] [ drop ] if
56 ! ! Remove definitions
58 ! Remove empty word defs
59 lint-definitions get-global [ drop empty? not ] assoc-filter
61 ! Remove constants [ 1 ]
62 [ drop { [ length 1 = ] [ first number? ] } 1&& not ] assoc-filter
64 ! Remove words that are their own definition
65 [ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
68 [ nip [ vocabulary>> "specialized-" head? ] any? not ] assoc-filter
70 [ nip [ vocabulary>> "windows.messages" = ] any? not ] assoc-filter
72 [ nip [ alias? ] any? not ] assoc-filter
75 [ drop trivial-defs member? not ] assoc-filter
77 ! Remove numbers only defs
78 [ drop [ number? ] all? not ] assoc-filter
80 ! Remove curry only defs
81 [ drop [ \ curry = ] all? not ] assoc-filter
87 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
93 [ [ wrapper? ] deep-any? ]
94 [ [ hashtable? ] deep-any? ]
98 ! Remove n m shift defs
100 drop dup length 3 = [
101 [ first2 [ number? ] both? ]
102 [ third \ shift = ] bi and not
109 [ first2 [ number? ] [ \ slot = ] bi* and not ] [ drop t ] if
112 dup manual-substitutions
114 [ lint-definitions set-global ] [ keys lint-definitions-keys set-global ] bi
116 : find-duplicates ( -- seq )
117 lint-definitions get-global [ nip length 1 > ] assoc-filter ;
119 GENERIC: lint ( obj -- seq )
121 M: object lint ( obj -- seq ) drop f ;
123 : subseq/member? ( subseq/member seq -- ? )
124 { [ start ] [ member? ] } 2|| ;
126 M: callable lint ( quot -- seq )
127 [ lint-definitions-keys get-global ] dip '[ _ subseq/member? ] filter ;
129 M: word lint ( word -- seq )
130 def>> dup callable? [ lint ] [ drop f ] if ;
132 : word-path. ( word -- )
133 [ vocabulary>> ] [ name>> ] bi ":" glue print ;
135 : 4bl ( -- ) bl bl bl bl ;
137 : (lint.) ( pair -- )
138 first2 [ word-path. ] dip [
139 [ 4bl . "-----------------------------------" print ]
140 [ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
143 : lint. ( alist -- ) [ (lint.) ] each ;
145 GENERIC: run-lint ( obj -- obj )
147 : (trim-self) ( val key -- obj ? )
148 lint-definitions get-global at*
149 [ dupd remove empty? not ] [ drop f ] if ;
151 : trim-self ( seq -- newseq )
152 [ [ (trim-self) ] filter ] assoc-map ;
154 : filter-symbols ( alist -- alist )
156 nip first dup lint-definitions get-global at
157 [ first ] bi@ literalize = not
160 M: sequence run-lint ( seq -- seq )
161 [ dup lint ] { } map>assoc trim-self
162 [ second empty? not ] filter filter-symbols ;
164 M: word run-lint ( word -- seq ) 1array run-lint ;
166 : lint-all ( -- seq ) all-words run-lint dup lint. ;
168 : lint-vocab ( vocab -- seq ) words run-lint dup lint. ;
170 : lint-vocabs ( prefix -- seq )
171 [ vocabs ] dip [ head? ] curry filter [ lint-vocab ] map ;
173 : lint-word ( word -- seq ) 1array run-lint dup lint. ;