factor/basis/vocabs/hierarchy/hierarchy.factor

107 lines
3.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2007, 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2009-07-06 05:23:26 -04:00
USING: accessors arrays assocs combinators.short-circuit fry
io.directories io.files io.files.info io.pathnames kernel make
memoize namespaces sequences sorting splitting vocabs sets
vocabs.loader vocabs.metadata vocabs.errors ;
2009-07-06 05:23:26 -04:00
RENAME: child-vocabs vocabs => vocabs:child-vocabs
IN: vocabs.hierarchy
2009-07-06 05:23:26 -04:00
TUPLE: vocab-prefix name ;
C: <vocab-prefix> vocab-prefix
M: vocab-prefix vocab-name name>> ;
<PRIVATE
: vocab-subdirs ( dir -- dirs )
[
[
{ [ link-info directory? ] [ "." head? not ] } 1&&
] filter
] with-directory-files natural-sort ;
: vocab-dir? ( root name -- ? )
over
[ ".factor" vocab-dir+ append-path exists? ]
[ 2drop f ]
if ;
2009-07-06 05:23:26 -04:00
: (child-vocabs) ( root prefix -- vocabs )
[ vocab-dir append-path dup exists? [ vocab-subdirs ] [ drop { } ] if ]
[ nip [ '[ [ _ "." ] dip 3append ] map ] unless-empty ]
[ drop '[ _ over vocab-dir? [ >vocab-link ] [ <vocab-prefix> ] if ] map ]
2tri ;
2009-07-06 05:23:26 -04:00
: ((child-vocabs-recursive)) ( root name -- )
dupd vocab-name (child-vocabs)
[ dup , ((child-vocabs-recursive)) ] with each ;
2009-07-06 05:23:26 -04:00
: (child-vocabs-recursive) ( root name -- seq )
[ ((child-vocabs-recursive)) ] { } make ;
2009-07-06 05:23:26 -04:00
: no-rooted ( seq -- seq' ) [ find-vocab-root not ] filter ;
2009-07-06 05:23:26 -04:00
: one-level-only? ( name prefix -- ? )
?head [ "." split1 nip not ] dip and ;
: unrooted-child-vocabs ( prefix -- seq )
2009-07-06 05:23:26 -04:00
[ vocabs no-rooted ] dip
dup empty? [ CHAR: . suffix ] unless
2009-07-06 05:23:26 -04:00
'[ vocab-name _ one-level-only? ] filter ;
: unrooted-child-vocabs-recursive ( prefix -- seq )
vocabs:child-vocabs no-rooted ;
PRIVATE>
2009-07-06 05:23:26 -04:00
: no-prefixes ( seq -- seq' ) [ vocab-prefix? not ] filter ;
: no-roots ( assoc -- seq ) values concat ;
2009-07-06 05:23:26 -04:00
: child-vocabs ( prefix -- assoc )
[ [ vocab-roots get ] dip '[ dup _ (child-vocabs) ] { } map>assoc ]
[ unrooted-child-vocabs [ vocab ] map f swap 2array ]
bi suffix ;
: all-vocabs ( -- assoc )
"" child-vocabs ;
: child-vocabs-recursive ( prefix -- assoc )
[ [ vocab-roots get ] dip '[ dup _ (child-vocabs-recursive) ] { } map>assoc ]
[ unrooted-child-vocabs-recursive [ vocab ] map f swap 2array ]
bi suffix ;
MEMO: all-vocabs-recursive ( -- assoc )
"" child-vocabs-recursive ;
: all-vocab-names ( -- seq )
all-vocabs-recursive no-roots no-prefixes [ vocab-name ] map ;
<PRIVATE
: filter-unportable ( seq -- seq' )
[ vocab-name unportable? not ] filter ;
2009-07-06 05:23:26 -04:00
: collect-vocabs ( quot -- seq )
[ all-vocabs-recursive no-roots no-prefixes ] dip
gather natural-sort ; inline
PRIVATE>
: (load) ( prefix -- failures )
2009-07-06 05:23:26 -04:00
child-vocabs-recursive
filter-unportable
require-all ;
: load ( prefix -- )
(load) load-failures. ;
: load-all ( -- )
"" load ;
2009-07-06 05:23:26 -04:00
MEMO: all-tags ( -- seq ) [ vocab-tags ] collect-vocabs ;
2009-07-06 05:23:26 -04:00
MEMO: all-authors ( -- seq ) [ vocab-authors ] collect-vocabs ;