1 ! Copyright (C) 2007, 2010 Eduardo Cavazos, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs continuations definitions init
4 io io.files io.pathnames kernel make namespaces parser
5 sequences sets splitting strings vocabs words ;
10 SYMBOL: add-vocab-root-hook
18 } clone vocab-roots set-global
20 [ drop ] add-vocab-root-hook set-global
21 ] "vocabs.loader" add-startup-hook
23 : add-vocab-root ( root -- )
24 trim-tail-separators dup vocab-roots get ?adjoin
25 [ add-vocab-root-hook get-global call( root -- ) ] [ drop ] if ;
28 root-cache [ H{ } clone ] initialize
30 ERROR: not-found-in-roots path ;
34 : find-root-for ( path -- path/f )
35 vocab-roots get [ prepend-path exists? ] with find nip ;
37 M: string vocab-path ( string -- path/f )
38 dup find-root-for [ prepend-path ] [ not-found-in-roots ] if* ;
42 : vocab-dir ( vocab -- dir )
43 vocab-name H{ { CHAR: . CHAR: / } } substitute ;
45 : append-vocab-dir ( vocab str/f -- path )
46 [ vocab-name "." split ] dip
47 [ [ dup last ] dip append suffix ] when*
50 : find-vocab-root ( vocab -- path/f )
51 vocab-name root-cache get [
52 dup ".private" tail? [ drop f ] [
53 ".factor" append-vocab-dir find-root-for
57 : vocab-exists? ( name -- ? )
58 dup lookup-vocab [ ] [ find-vocab-root ] ?if ;
60 : vocab-append-path ( vocab path -- newpath )
61 swap find-vocab-root [ prepend-path ] [ drop f ] if* ;
63 : vocab-source-path ( vocab -- path/f )
64 dup ".factor" append-vocab-dir vocab-append-path ;
66 : vocab-docs-path ( vocab -- path/f )
67 dup "-docs.factor" append-vocab-dir vocab-append-path ;
71 ! Defined by vocabs.metadata
72 SYMBOL: check-vocab-hook
73 check-vocab-hook [ [ drop ] ] initialize
77 SYMBOL: require-when-vocabs
78 require-when-vocabs [ HS{ } clone ] initialize
80 SYMBOL: require-when-table
81 require-when-table [ V{ } clone ] initialize
83 : load-conditional-requires ( vocab -- )
84 vocab-name require-when-vocabs get in? [
85 require-when-table get [
86 [ [ lookup-vocab dup [ source-loaded?>> +done+ = ] when ] all? ] dip
87 [ require ] curry when
91 : load-source ( vocab -- )
92 dup check-vocab-hook get call( vocab -- )
94 +parsing+ >>source-loaded?
95 dup vocab-source-path [ parse-file ] [ [ ] ] if*
96 [ +parsing+ >>source-loaded? ] dip
97 [ % ] [ call( -- ) ] if-bootstrapping
98 +done+ >>source-loaded?
99 load-conditional-requires
100 ] [ ] [ f >>source-loaded? ] cleanup ;
102 : load-docs ( vocab -- )
105 +parsing+ >>docs-loaded?
106 dup vocab-docs-path [ ?run-file ] when*
107 +done+ >>docs-loaded?
108 ] [ ] [ f >>docs-loaded? ] cleanup
113 : require-when ( if then -- )
114 over [ lookup-vocab ] all? [
117 [ drop require-when-vocabs get adjoin-all ]
118 [ 2array require-when-table get push ] 2bi
123 [ [ load-source ] [ load-docs ] bi ]
128 dup load-vocab vocab-main [
131 "The " write vocab-name write
132 " vocabulary does not define an entry point." print
133 "To define one, refer to \\ MAIN: help" print
138 : require-all ( vocabs -- )
139 V{ } clone blacklist [ [ require ] each ] with-variable ;
143 : add-to-blacklist ( error vocab -- )
144 vocab-name blacklist get [ set-at ] [ 2drop ] if* ;
146 GENERIC: (require) ( name -- )
150 dup source-loaded?>> +parsing+ eq? [ drop ] [
151 dup source-loaded?>> [ dup load-source ] unless
152 dup docs-loaded?>> [ dup load-docs ] unless
155 ] [ [ swap add-to-blacklist ] keep rethrow ] recover ;
157 M: vocab-link (require)
158 vocab-name (require) ;
160 M: string (require) create-vocab (require) ;
165 dup vocab-name blacklist get at*
168 drop dup find-vocab-root
170 [ dup lookup-vocab [ drop ] [ no-vocab ] if ]
173 ] require-hook set-global
175 M: vocab-spec where vocab-source-path dup [ 1 2array ] when ;
177 ! put here to avoid circularity between vocabs.loader and source-files.errors
178 { "source-files.errors" "debugger" } "source-files.errors.debugger" require-when