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
51 [ drop ] ! because of declare
54 [ first ] [ second ] [ third ] [ fourth ]
55 [ ">" write-html ] [ "/>" write-html ]
59 H{ } clone def-hash set-global
62 dup def>> dup callable?
63 [ def-hash get-global set-hash-vector ] [ drop ] if
66 ! ! Remove definitions
68 ! Remove empty word defs
69 def-hash get-global [ drop empty? not ] assoc-filter
71 ! Remove constants [ 1 ]
72 [ drop { [ length 1 = ] [ first number? ] } 1&& not ] assoc-filter
74 ! Remove words that are their own definition
75 [ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
77 ! Remove set-alien-cell, etc.
78 [ drop [ accessor-words diff ] keep [ length ] bi@ = ] assoc-filter
81 [ drop trivial-defs member? not ] assoc-filter
87 [ first \ tag = ] [ second number? ] [ third \ eq? = ]
93 [ [ wrapper? ] deep-contains? ]
94 [ [ hashtable? ] deep-contains? ]
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
115 [ def-hash set-global ] [ keys def-hash-keys set-global ] bi
117 : find-duplicates ( -- seq )
118 def-hash get-global [ nip length 1 > ] assoc-filter ;
120 GENERIC: lint ( obj -- seq )
122 M: object lint ( obj -- seq ) drop f ;
124 : subseq/member? ( subseq/member seq -- ? )
125 { [ start ] [ member? ] } 2|| ;
127 M: callable lint ( quot -- seq )
128 [ def-hash-keys get-global ] dip '[ _ subseq/member? ] filter ;
130 M: word lint ( word -- seq )
131 def>> dup callable? [ lint ] [ drop f ] if ;
133 : word-path. ( word -- )
134 [ vocabulary>> ] [ unparse ] bi ":" glue print ;
136 : 4bl ( -- ) bl bl bl bl ;
138 : (lint.) ( pair -- )
139 first2 [ word-path. ] dip [
140 [ 4bl . "-----------------------------------" print ]
141 [ def-hash get-global at [ 4bl word-path. ] each nl ] bi
144 : lint. ( alist -- ) [ (lint.) ] each ;
146 GENERIC: run-lint ( obj -- obj )
148 : (trim-self) ( val key -- obj ? )
149 def-hash get-global at*
150 [ dupd remove empty? not ] [ drop f ] if ;
152 : trim-self ( seq -- newseq )
153 [ [ (trim-self) ] filter ] assoc-map ;
155 : filter-symbols ( alist -- alist )
157 nip first dup def-hash get-global at
158 [ first ] bi@ literalize = not
161 M: sequence run-lint ( seq -- seq )
162 [ dup lint ] { } map>assoc trim-self
163 [ second empty? not ] filter filter-symbols ;
165 M: word run-lint ( word -- seq ) 1array run-lint ;
167 : lint-all ( -- seq ) all-words run-lint dup lint. ;
169 : lint-vocab ( vocab -- seq ) words run-lint dup lint. ;
171 : lint-word ( word -- seq ) 1array run-lint dup lint. ;