factor/extra/lint/lint.factor

174 lines
4.4 KiB
Factor
Raw Normal View History

2008-12-07 02:55:19 -05:00
! Copyright (C) 2007, 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
2011-10-14 04:39:07 -04:00
USING: accessors alien arrays assocs combinators.short-circuit
fry hashtables io kernel math namespaces prettyprint quotations
sequences sequences.deep shuffle slots.private vectors vocabs
words xml.data words.alias ;
2008-12-07 02:55:19 -05:00
IN: lint
2011-10-14 04:39:07 -04:00
SYMBOL: lint-definitions
SYMBOL: lint-definitions-keys
2008-12-07 02:55:19 -05:00
: set-hash-vector ( val key hash -- )
2dup at -rot [ ?push ] 2dip set-at ;
2011-10-14 04:39:07 -04:00
: manual-substitutions ( hash -- )
2008-12-07 02:55:19 -05:00
{
2008-12-18 01:42:12 -05:00
{ -rot [ swap [ swap ] dip ] }
2008-12-07 02:55:19 -05:00
{ -rot [ swap swapd ] }
2008-12-18 01:42:12 -05:00
{ rot [ [ swap ] dip swap ] }
2008-12-07 02:55:19 -05:00
{ rot [ swapd swap ] }
{ over [ dup swap ] }
{ tuck [ dup -rot ] }
2008-12-18 01:42:12 -05:00
{ swapd [ [ swap ] dip ] }
2008-12-07 02:55:19 -05:00
{ 2nip [ nip nip ] }
{ 2drop [ drop drop ] }
{ 3drop [ drop drop drop ] }
{ pop* [ pop drop ] }
{ when [ [ ] if ] }
{ >boolean [ f = not ] }
} swap '[ first2 _ set-hash-vector ] each ;
2011-10-14 04:39:07 -04:00
CONSTANT: trivial-defs
2008-12-07 02:55:19 -05:00
{
2011-10-14 13:23:52 -04:00
[ drop ] [ 2drop ] [ 2array ]
[ bitand ]
2008-12-07 02:55:19 -05:00
[ . ]
2011-10-14 13:23:52 -04:00
[ new ]
2008-12-07 02:55:19 -05:00
[ get ]
2011-10-14 15:31:06 -04:00
[ "" ]
2008-12-07 02:55:19 -05:00
[ t ] [ f ]
[ { } ]
2011-10-14 13:23:52 -04:00
[ drop t ] [ drop f ] [ 2drop t ] [ 2drop f ]
2010-03-31 22:20:35 -04:00
[ cdecl ]
2008-12-07 02:55:19 -05:00
[ first ] [ second ] [ third ] [ fourth ]
2009-02-01 00:11:07 -05:00
[ ">" write ] [ "/>" write ]
2011-10-14 04:39:07 -04:00
}
2008-12-07 02:55:19 -05:00
! ! Add definitions
2011-10-14 04:39:07 -04:00
H{ } clone lint-definitions set-global
2008-12-07 02:55:19 -05:00
all-words [
dup def>> dup callable?
2011-10-14 04:39:07 -04:00
[ lint-definitions get-global set-hash-vector ] [ drop ] if
2008-12-07 02:55:19 -05:00
] each
! ! Remove definitions
! Remove empty word defs
2011-10-14 04:39:07 -04:00
lint-definitions get-global [ drop empty? not ] assoc-filter
2008-12-07 02:55:19 -05:00
! Remove constants [ 1 ]
[ drop { [ length 1 = ] [ first number? ] } 1&& not ] assoc-filter
! Remove words that are their own definition
[ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
2011-10-14 04:39:07 -04:00
! Remove specialized*
[ nip [ vocabulary>> "specialized-" head? ] any? not ] assoc-filter
[ nip [ vocabulary>> "windows.messages" = ] any? not ] assoc-filter
2008-12-07 02:55:19 -05:00
[ nip [ alias? ] any? not ] assoc-filter
2008-12-07 02:55:19 -05:00
! Remove trivial defs
[ drop trivial-defs member? not ] assoc-filter
! Remove numbers only defs
[ drop [ number? ] all? not ] assoc-filter
! Remove curry only defs
[ drop [ \ curry = ] all? not ] assoc-filter
2008-12-07 02:55:19 -05:00
! Remove tag defs
[
drop {
[ length 3 = ]
[ first \ tag = ] [ second number? ] [ third \ eq? = ]
} 1&& not
] assoc-filter
[
drop {
[ [ wrapper? ] deep-any? ]
[ [ hashtable? ] deep-any? ]
2008-12-07 02:55:19 -05:00
} 1|| not
] assoc-filter
! Remove n m shift defs
[
drop dup length 3 = [
[ first2 [ number? ] both? ]
[ third \ shift = ] bi and not
] [ drop t ] if
] assoc-filter
! Remove [ n slot ]
[
drop dup length 2 =
[ first2 [ number? ] [ \ slot = ] bi* and not ] [ drop t ] if
] assoc-filter
2011-10-14 04:39:07 -04:00
dup manual-substitutions
2008-12-07 02:55:19 -05:00
2011-10-14 04:39:07 -04:00
[ lint-definitions set-global ] [ keys lint-definitions-keys set-global ] bi
2008-12-07 02:55:19 -05:00
: find-duplicates ( -- seq )
2011-10-14 04:39:07 -04:00
lint-definitions get-global [ nip length 1 > ] assoc-filter ;
2008-12-07 02:55:19 -05:00
GENERIC: lint ( obj -- seq )
M: object lint ( obj -- seq ) drop f ;
: subseq/member? ( subseq/member seq -- ? )
{ [ start ] [ member? ] } 2|| ;
M: callable lint ( quot -- seq )
2011-10-14 04:39:07 -04:00
[ lint-definitions-keys get-global ] dip '[ _ subseq/member? ] filter ;
2008-12-07 02:55:19 -05:00
M: word lint ( word -- seq )
def>> dup callable? [ lint ] [ drop f ] if ;
: word-path. ( word -- )
2011-10-14 04:39:07 -04:00
[ vocabulary>> ] [ name>> ] bi ":" glue print ;
2008-12-07 02:55:19 -05:00
: 4bl ( -- ) bl bl bl bl ;
: (lint.) ( pair -- )
first2 [ word-path. ] dip [
[ 4bl . "-----------------------------------" print ]
2011-10-14 04:39:07 -04:00
[ lint-definitions get-global at [ 4bl word-path. ] each nl ] bi
2008-12-07 02:55:19 -05:00
] each nl nl ;
: lint. ( alist -- ) [ (lint.) ] each ;
GENERIC: run-lint ( obj -- obj )
: (trim-self) ( val key -- obj ? )
2011-10-14 04:39:07 -04:00
lint-definitions get-global at*
2008-12-07 02:55:19 -05:00
[ dupd remove empty? not ] [ drop f ] if ;
: trim-self ( seq -- newseq )
[ [ (trim-self) ] filter ] assoc-map ;
: filter-symbols ( alist -- alist )
[
2011-10-14 04:39:07 -04:00
nip first dup lint-definitions get-global at
2008-12-07 02:55:19 -05:00
[ first ] bi@ literalize = not
] assoc-filter ;
M: sequence run-lint ( seq -- seq )
[ dup lint ] { } map>assoc trim-self
[ second empty? not ] filter filter-symbols ;
M: word run-lint ( word -- seq ) 1array run-lint ;
: lint-all ( -- seq ) all-words run-lint dup lint. ;
: lint-vocab ( vocab -- seq ) words run-lint dup lint. ;
2011-10-14 13:23:52 -04:00
: lint-vocabs ( prefix -- seq )
[ vocabs ] dip [ head? ] curry filter [ lint-vocab ] map ;
2008-12-07 02:55:19 -05:00
: lint-word ( word -- seq ) 1array run-lint dup lint. ;