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