factor/basis/tools/vocabs/vocabs.factor

305 lines
8.2 KiB
Factor
Raw Normal View History

! Copyright (C) 2007, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2008-07-29 04:17:21 -04:00
USING: kernel io io.styles io.files io.encodings.utf8
vocabs.loader vocabs sequences namespaces make math.parser
arrays hashtables assocs memoize summary sorting splitting
combinators source-files debugger continuations compiler.errors
init checksums checksums.crc32 sets accessors generic
definitions words ;
IN: tools.vocabs
: vocab-xref ( vocab quot -- vocabs )
[ [ vocab-name ] [ words [ generic? not ] filter ] bi ] dip map
[
[ [ word? ] [ generic? not ] bi and ] filter [
dup method-body?
[ "method-generic" word-prop ] when
vocabulary>>
] map
] gather natural-sort remove sift ; inline
: vocabs. ( seq -- )
[ dup >vocab-link write-object nl ] each ;
: vocab-uses ( vocab -- vocabs ) [ uses ] vocab-xref ;
: vocab-uses. ( vocab -- ) vocab-uses vocabs. ;
: vocab-usage ( vocab -- vocabs ) [ usage ] vocab-xref ;
: vocab-usage. ( vocab -- ) vocab-usage vocabs. ;
2008-03-19 15:39:08 -04:00
: vocab-tests-file ( vocab -- path )
dup "-tests.factor" vocab-dir+ vocab-append-path dup
[ dup exists? [ drop f ] unless ] [ drop f ] if ;
2008-03-19 15:39:08 -04:00
: vocab-tests-dir ( vocab -- paths )
dup vocab-dir "tests" append-path vocab-append-path dup [
dup exists? [
2008-10-19 14:27:59 -04:00
dup directory-files [ ".factor" tail? ] filter
[ append-path ] with map
2008-03-19 15:39:08 -04:00
] [ drop f ] if
] [ drop f ] if ;
: vocab-tests ( vocab -- tests )
2008-03-19 15:39:08 -04:00
[
2008-04-09 01:19:56 -04:00
[ vocab-tests-file [ , ] when* ]
[ vocab-tests-dir [ % ] when* ] bi
2008-03-19 15:39:08 -04:00
] { } make ;
: vocab-files ( vocab -- seq )
2008-03-19 15:39:08 -04:00
[
2008-04-09 01:19:56 -04:00
[ vocab-source-path [ , ] when* ]
[ vocab-docs-path [ , ] when* ]
[ vocab-tests % ] tri
] { } make ;
: vocab-heading. ( vocab -- )
nl
"==== " write
2008-04-09 01:19:56 -04:00
[ vocab-name ] [ vocab write-object ] bi ":" print
nl ;
: load-error. ( triple -- )
2008-04-09 01:19:56 -04:00
[ first vocab-heading. ] [ second print-error ] bi ;
: load-failures. ( failures -- )
[ load-error. nl ] each ;
SYMBOL: failures
: require-all ( vocabs -- failures )
[
V{ } clone blacklist set
V{ } clone failures set
[
[ require ]
[ swap vocab-name failures get set-at ]
recover
] each
failures get
] with-compiler-errors ;
2008-04-09 20:30:54 -04:00
: source-modified? ( path -- ? )
dup source-files get at [
2008-08-30 13:35:14 -04:00
dup path>>
2008-04-09 20:30:54 -04:00
dup exists? [
2008-04-30 17:11:55 -04:00
utf8 file-lines crc32 checksum-lines
2008-08-30 13:35:14 -04:00
swap checksum>> = not
2008-04-09 20:30:54 -04:00
] [
2drop f
] if
] [
exists?
] ?if ;
SYMBOL: changed-vocabs
[ f changed-vocabs set-global ] "tools.vocabs" add-init-hook
: changed-vocab ( vocab -- )
2008-04-10 23:02:23 -04:00
dup vocab changed-vocabs get and
[ dup changed-vocabs get set-at ] [ drop ] if ;
2008-04-09 20:30:54 -04:00
: unchanged-vocab ( vocab -- )
2008-04-10 23:02:23 -04:00
changed-vocabs get delete-at ;
2008-04-09 20:30:54 -04:00
: unchanged-vocabs ( vocabs -- )
[ unchanged-vocab ] each ;
2008-04-11 08:15:26 -04:00
: changed-vocab? ( vocab -- ? )
changed-vocabs get dup [ key? ] [ 2drop t ] if ;
2008-04-09 20:30:54 -04:00
: filter-changed ( vocabs -- vocabs' )
[ changed-vocab? ] filter ;
2008-04-09 20:30:54 -04:00
SYMBOL: modified-sources
SYMBOL: modified-docs
: (to-refresh) ( vocab variable loaded? path -- )
dup [
swap [
2008-04-11 08:15:26 -04:00
pick changed-vocab? [
2008-04-09 20:30:54 -04:00
source-modified? [ get push ] [ 2drop ] if
] [ 3drop ] if
] [ drop get push ] if
] [ 2drop 2drop ] if ;
: to-refresh ( prefix -- modified-sources modified-docs unchanged )
[
V{ } clone modified-sources set
V{ } clone modified-docs set
child-vocabs [
[
[
[ modified-sources ]
[ vocab-source-loaded? ]
[ vocab-source-path ]
tri (to-refresh)
] [
[ modified-docs ]
[ vocab-docs-loaded? ]
[ vocab-docs-path ]
tri (to-refresh)
] bi
] each
modified-sources get
modified-docs get
]
[ modified-docs get modified-sources get append diff ] bi
2008-04-09 20:30:54 -04:00
] with-scope ;
: do-refresh ( modified-sources modified-docs unchanged -- )
unchanged-vocabs
2008-04-09 01:19:56 -04:00
[
[ [ f swap set-vocab-source-loaded? ] each ]
[ [ f swap set-vocab-docs-loaded? ] each ] bi*
]
2008-04-09 20:30:54 -04:00
[
append prune
[ unchanged-vocabs ]
[ require-all load-failures. ] bi
] 2bi ;
: refresh ( prefix -- ) to-refresh do-refresh ;
2008-04-09 01:19:56 -04:00
: refresh-all ( -- ) "" refresh ;
2008-04-09 01:19:56 -04:00
MEMO: vocab-file-contents ( vocab name -- seq )
vocab-append-path dup
[ dup exists? [ utf8 file-lines ] [ drop f ] if ] when ;
: set-vocab-file-contents ( seq vocab name -- )
dupd vocab-append-path [
utf8 set-file-lines
2008-04-09 01:19:56 -04:00
\ vocab-file-contents reset-memoized
] [
"The " swap vocab-name
" vocabulary was not loaded from the file system"
3append throw
] ?if ;
: vocab-summary-path ( vocab -- string )
vocab-dir "summary.txt" append-path ;
: vocab-summary ( vocab -- summary )
dup dup vocab-summary-path vocab-file-contents
2008-09-06 20:13:59 -04:00
[
vocab-name " vocabulary" append
] [
nip first
2008-09-06 20:13:59 -04:00
] if-empty ;
M: vocab summary
[
dup vocab-summary %
" (" %
2008-08-30 14:28:59 -04:00
words>> assoc-size #
" words)" %
] "" make ;
M: vocab-link summary vocab-summary ;
: set-vocab-summary ( string vocab -- )
>r 1array r>
dup vocab-summary-path
set-vocab-file-contents ;
: vocab-tags-path ( vocab -- string )
vocab-dir "tags.txt" append-path ;
: vocab-tags ( vocab -- tags )
2008-09-03 23:20:40 -04:00
dup vocab-tags-path vocab-file-contents harvest ;
: set-vocab-tags ( tags vocab -- )
dup vocab-tags-path set-vocab-file-contents ;
: add-vocab-tags ( tags vocab -- )
[ vocab-tags append prune ] keep set-vocab-tags ;
: vocab-authors-path ( vocab -- string )
vocab-dir "authors.txt" append-path ;
: vocab-authors ( vocab -- authors )
2008-09-03 23:20:40 -04:00
dup vocab-authors-path vocab-file-contents harvest ;
: set-vocab-authors ( authors vocab -- )
dup vocab-authors-path set-vocab-file-contents ;
: subdirs ( dir -- dirs )
2008-10-19 21:37:11 -04:00
[
2008-10-19 14:27:59 -04:00
[ link-info directory? ] filter
2008-10-19 21:37:11 -04:00
] with-directory-files natural-sort ;
: (all-child-vocabs) ( root name -- vocabs )
2008-09-06 20:13:59 -04:00
[
vocab-dir append-path dup exists?
[ subdirs ] [ drop { } ] if
] keep [
swap [ "." swap 3append ] with map
2008-09-06 20:13:59 -04:00
] unless-empty ;
: vocabs-in-dir ( root name -- )
dupd (all-child-vocabs) [
2008-03-19 15:39:08 -04:00
2dup vocab-dir? [ dup >vocab-link , ] when
vocabs-in-dir
] with each ;
: all-vocabs ( -- assoc )
vocab-roots get [
dup [ "" vocabs-in-dir ] { } make
] { } map>assoc ;
MEMO: all-vocabs-seq ( -- seq )
all-vocabs values concat ;
: unportable? ( name -- ? )
vocab-tags "unportable" swap member? ;
: filter-unportable ( seq -- seq' )
[ vocab-name unportable? not ] filter ;
: try-everything ( -- failures )
all-vocabs-seq
filter-unportable
require-all ;
: load-everything ( -- )
try-everything load-failures. ;
: unrooted-child-vocabs ( prefix -- seq )
dup empty? [ CHAR: . suffix ] unless
vocabs
[ find-vocab-root not ] filter
[
vocab-name swap ?head CHAR: . rot member? not and
] with filter
[ vocab ] map ;
: all-child-vocabs ( prefix -- assoc )
vocab-roots get [
2008-03-19 15:39:08 -04:00
dup pick (all-child-vocabs) [ >vocab-link ] map
] { } map>assoc
swap unrooted-child-vocabs f swap 2array suffix ;
2008-03-13 04:35:54 -04:00
: all-child-vocabs-seq ( prefix -- assoc )
vocab-roots get swap [
dupd (all-child-vocabs)
[ vocab-dir? ] with filter
2008-03-13 04:35:54 -04:00
] curry map concat ;
MEMO: all-tags ( -- seq )
all-vocabs-seq [ vocab-tags ] gather natural-sort ;
MEMO: all-authors ( -- seq )
all-vocabs-seq [ vocab-authors ] gather natural-sort ;
: reset-cache ( -- )
2008-03-19 15:39:08 -04:00
root-cache get-global clear-assoc
2008-04-09 01:19:56 -04:00
\ vocab-file-contents reset-memoized
\ all-vocabs-seq reset-memoized
\ all-authors reset-memoized
\ all-tags reset-memoized ;