]> gitweb.factorcode.org Git - factor.git/blob - extra/lint/lint.factor
remove bogus equality
[factor.git] / extra / lint / lint.factor
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
7 kernel.private ;
8 IN: lint
9
10 SYMBOL: def-hash
11 SYMBOL: def-hash-keys
12
13 : set-hash-vector ( val key hash -- )
14     2dup at -rot [ ?push ] 2dip set-at ;
15
16 : more-defs ( hash -- )
17     {
18         { -rot [ swap >r swap r> ] }
19         { -rot [ swap swapd ] }
20         { rot [ >r swap r> swap ] }
21         { rot [ swapd swap ] }
22         { over [ dup swap ] }
23         { tuck [ dup -rot ] }
24         { swapd [ >r swap r> ] }
25         { 2nip [ nip nip ] }
26         { 2drop [ drop drop ] }
27         { 3drop [ drop drop drop ] }
28         { pop* [ pop drop ] }
29         { when [ [ ] if ] }
30         { >boolean [ f = not ] }
31     } swap '[ first2 _ set-hash-vector ] each ;
32
33 : accessor-words ( -- seq )
34 {
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
43 } ;
44
45 : trivial-defs
46     {
47         [ . ]
48         [ get ]
49         [ t ] [ f ]
50         [ { } ]
51         [ drop ] ! because of declare
52         [ drop f ]
53         [ "cdecl" ]
54         [ first ] [ second ] [ third ] [ fourth ]
55         [ ">" write-html ] [ "/>" write-html ]
56     } ;
57
58 ! ! Add definitions
59 H{ } clone def-hash set-global
60
61 all-words [
62     dup def>> dup callable?
63     [ def-hash get-global set-hash-vector ] [ drop ] if
64 ] each
65
66 ! ! Remove definitions
67
68 ! Remove empty word defs
69 def-hash get-global [ drop empty? not ] assoc-filter
70
71 ! Remove constants [ 1 ]
72 [ drop { [ length 1 = ] [ first number? ] } 1&& not ] assoc-filter
73
74 ! Remove words that are their own definition
75 [ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
76
77 ! Remove set-alien-cell, etc.
78 [ drop [ accessor-words diff ] keep [ length ] bi@ = ] assoc-filter
79
80 ! Remove trivial defs
81 [ drop trivial-defs member? not ] assoc-filter
82
83 ! Remove tag defs
84 [
85     drop {
86             [ length 3 = ]
87             [ first \ tag = ] [ second number? ] [ third \ eq? = ]
88     } 1&& not
89 ] assoc-filter
90
91 [
92     drop {
93         [ [ wrapper? ] deep-contains? ]
94         [ [ hashtable? ] deep-contains? ]
95     } 1|| not
96 ] assoc-filter
97
98 ! Remove n m shift defs
99 [
100     drop dup length 3 = [
101         [ first2 [ number? ] both? ]
102         [ third \ shift = ] bi and not
103     ] [ drop t ] if
104 ] assoc-filter 
105
106 ! Remove [ n slot ]
107 [
108     drop dup length 2 =
109     [ first2 [ number? ] [ \ slot = ] bi* and not ] [ drop t ] if
110 ] assoc-filter
111
112
113 dup more-defs
114
115 [ def-hash set-global ] [ keys def-hash-keys set-global ] bi
116
117 : find-duplicates ( -- seq )
118     def-hash get-global [ nip length 1 > ] assoc-filter ;
119
120 GENERIC: lint ( obj -- seq )
121
122 M: object lint ( obj -- seq ) drop f ;
123
124 : subseq/member? ( subseq/member seq -- ? )
125     { [ start ] [ member? ] } 2|| ;
126
127 M: callable lint ( quot -- seq )
128     [ def-hash-keys get-global ] dip '[ _ subseq/member? ] filter ;
129
130 M: word lint ( word -- seq )
131     def>> dup callable? [ lint ] [ drop f ] if ;
132
133 : word-path. ( word -- )
134     [ vocabulary>> ] [ unparse ] bi ":" glue print ;
135
136 : 4bl ( -- ) bl bl bl bl ;
137
138 : (lint.) ( pair -- )
139     first2 [ word-path. ] dip [
140         [ 4bl .  "-----------------------------------" print ]
141         [ def-hash get-global at [ 4bl word-path. ] each nl ] bi
142     ] each nl nl ;
143
144 : lint. ( alist -- ) [ (lint.) ] each ;
145
146 GENERIC: run-lint ( obj -- obj )
147
148 : (trim-self) ( val key -- obj ? )
149     def-hash get-global at*
150     [ dupd remove empty? not ] [ drop f ] if ;
151
152 : trim-self ( seq -- newseq )
153     [ [ (trim-self) ] filter ] assoc-map ;
154
155 : filter-symbols ( alist -- alist )
156     [
157         nip first dup def-hash get-global at
158         [ first ] bi@ literalize = not
159     ] assoc-filter ;
160
161 M: sequence run-lint ( seq -- seq )
162     [ dup lint ] { } map>assoc trim-self
163     [ second empty? not ] filter filter-symbols ;
164
165 M: word run-lint ( word -- seq ) 1array run-lint ;
166
167 : lint-all ( -- seq ) all-words run-lint dup lint. ;
168
169 : lint-vocab ( vocab -- seq ) words run-lint dup lint. ;
170
171 : lint-word ( word -- seq ) 1array run-lint dup lint. ;