1 ! Copyright (C) 2007, 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.accessors arrays assocs
4 combinators.short-circuit fry hashtables html.elements io
5 kernel math namespaces prettyprint quotations sequences
6 sequences.deep sets slots.private vectors vocabs words
13 : set-hash-vector ( val key hash -- )
14 2dup at -rot [ ?push ] 2dip set-at ;
16 : more-defs ( hash -- )
18 { -rot [ swap >r swap r> ] }
19 { -rot [ swap swapd ] }
20 { rot [ >r swap r> swap ] }
21 { rot [ swapd swap ] }
24 { swapd [ >r swap r> ] }
26 { 2drop [ drop drop ] }
27 { 3drop [ drop drop drop ] }
30 { >boolean [ f = not ] }
31 } swap '[ first2 _ set-hash-vector ] each ;
33 : accessor-words ( -- seq )
35 alien-signed-1 alien-signed-2 alien-signed-4 alien-signed-8
36 alien-unsigned-1 alien-unsigned-2 alien-unsigned-4 alien-unsigned-8
37 <displaced-alien> alien-unsigned-cell set-alien-signed-cell
38 set-alien-unsigned-1 set-alien-signed-1 set-alien-unsigned-2
39 set-alien-signed-2 set-alien-unsigned-4 set-alien-signed-4
40 set-alien-unsigned-8 set-alien-signed-8
41 alien-cell alien-signed-cell set-alien-cell set-alien-unsigned-cell
42 set-alien-float alien-float
56 [ first ] [ second ] [ third ] [ fourth ]
57 [ ">" write-html ] [ "/>" write-html ]
61 H{ } clone def-hash set-global
64 dup def>> dup callable?
65 [ def-hash get-global set-hash-vector ] [ drop ] if
68 ! ! Remove definitions
70 ! Remove empty word defs
71 def-hash get-global [ drop empty? not ] assoc-filter
73 ! Remove constants [ 1 ]
74 [ drop { [ length 1 = ] [ first number? ] } 1&& not ] assoc-filter
76 ! Remove words that are their own definition
77 [ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
79 ! Remove set-alien-cell, etc.
80 [ drop [ accessor-words diff ] keep [ length ] bi@ = ] assoc-filter
83 [ drop trivial-defs member? not ] assoc-filter
85 ! Remove numbers only defs
86 [ drop [ number? ] all? not ] assoc-filter
88 ! Remove curry only defs
89 [ drop [ \ curry = ] all? not ] assoc-filter
95 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
101 [ [ wrapper? ] deep-contains? ]
102 [ [ hashtable? ] deep-contains? ]
106 ! Remove n m shift defs
108 drop dup length 3 = [
109 [ first2 [ number? ] both? ]
110 [ third \ shift = ] bi and not
117 [ first2 [ number? ] [ \ slot = ] bi* and not ] [ drop t ] if
123 [ def-hash set-global ] [ keys def-hash-keys set-global ] bi
125 : find-duplicates ( -- seq )
126 def-hash get-global [ nip length 1 > ] assoc-filter ;
128 GENERIC: lint ( obj -- seq )
130 M: object lint ( obj -- seq ) drop f ;
132 : subseq/member? ( subseq/member seq -- ? )
133 { [ start ] [ member? ] } 2|| ;
135 M: callable lint ( quot -- seq )
136 [ def-hash-keys get-global ] dip '[ _ subseq/member? ] filter ;
138 M: word lint ( word -- seq )
139 def>> dup callable? [ lint ] [ drop f ] if ;
141 : word-path. ( word -- )
142 [ vocabulary>> ] [ unparse ] bi ":" glue print ;
144 : 4bl ( -- ) bl bl bl bl ;
146 : (lint.) ( pair -- )
147 first2 [ word-path. ] dip [
148 [ 4bl . "-----------------------------------" print ]
149 [ def-hash get-global at [ 4bl word-path. ] each nl ] bi
152 : lint. ( alist -- ) [ (lint.) ] each ;
154 GENERIC: run-lint ( obj -- obj )
156 : (trim-self) ( val key -- obj ? )
157 def-hash get-global at*
158 [ dupd remove empty? not ] [ drop f ] if ;
160 : trim-self ( seq -- newseq )
161 [ [ (trim-self) ] filter ] assoc-map ;
163 : filter-symbols ( alist -- alist )
165 nip first dup def-hash get-global at
166 [ first ] bi@ literalize = not
169 M: sequence run-lint ( seq -- seq )
170 [ dup lint ] { } map>assoc trim-self
171 [ second empty? not ] filter filter-symbols ;
173 M: word run-lint ( word -- seq ) 1array run-lint ;
175 : lint-all ( -- seq ) all-words run-lint dup lint. ;
177 : lint-vocab ( vocab -- seq ) words run-lint dup lint. ;
179 : lint-word ( word -- seq ) 1array run-lint dup lint. ;