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 -- )
25 [ vocab-roots get adjoin ]
26 [ add-vocab-root-hook get-global call( root -- ) ] bi ;
29 root-cache [ H{ } clone ] initialize
31 ERROR: not-found-in-roots path ;
35 : find-root-for ( path -- path/f )
36 vocab-roots get [ prepend-path exists? ] with find nip ;
38 M: string vocab-path ( string -- path/f )
39 dup find-root-for [ prepend-path ] [ not-found-in-roots ] if* ;
43 : vocab-dir ( vocab -- dir )
44 vocab-name H{ { CHAR: . CHAR: / } } substitute ;
46 : append-vocab-dir ( vocab str/f -- path )
47 [ vocab-name "." split ] dip
48 [ [ dup last ] dip append suffix ] when*
51 : find-vocab-root ( vocab -- path/f )
52 vocab-name root-cache get [
53 dup ".private" tail? [ drop f ] [
54 ".factor" append-vocab-dir find-root-for
58 : vocab-append-path ( vocab path -- newpath )
59 swap find-vocab-root [ prepend-path ] [ drop f ] if* ;
61 : vocab-source-path ( vocab -- path/f )
62 dup ".factor" append-vocab-dir vocab-append-path ;
64 : vocab-docs-path ( vocab -- path/f )
65 dup "-docs.factor" append-vocab-dir vocab-append-path ;
69 ! Defined by vocabs.metadata
70 SYMBOL: check-vocab-hook
71 check-vocab-hook [ [ drop ] ] initialize
75 SYMBOL: require-when-vocabs
76 require-when-vocabs [ HS{ } clone ] initialize
78 SYMBOL: require-when-table
79 require-when-table [ V{ } clone ] initialize
81 : load-conditional-requires ( vocab -- )
82 vocab-name require-when-vocabs get in? [
83 require-when-table get [
84 [ [ lookup-vocab dup [ source-loaded?>> +done+ = ] when ] all? ] dip
85 [ require ] curry when
89 : load-source ( vocab -- )
90 dup check-vocab-hook get call( vocab -- )
92 +parsing+ >>source-loaded?
93 dup vocab-source-path [ parse-file ] [ [ ] ] if*
94 [ +parsing+ >>source-loaded? ] dip
95 [ % ] [ call( -- ) ] if-bootstrapping
96 +done+ >>source-loaded?
97 load-conditional-requires
98 ] [ ] [ f >>source-loaded? ] cleanup ;
100 : load-docs ( vocab -- )
103 +parsing+ >>docs-loaded?
104 dup vocab-docs-path [ ?run-file ] when*
105 +done+ >>docs-loaded?
106 ] [ ] [ f >>docs-loaded? ] cleanup
111 : require-when ( if then -- )
112 over [ lookup-vocab ] all? [
115 [ drop require-when-vocabs get adjoin-all ]
116 [ 2array require-when-table get push ] 2bi
121 [ [ load-source ] [ load-docs ] bi ]
126 dup load-vocab vocab-main [
129 "The " write vocab-name write
130 " vocabulary does not define an entry point." print
131 "To define one, refer to \\ MAIN: help" print
136 : require-all ( vocabs -- )
137 V{ } clone blacklist [ [ require ] each ] with-variable ;
141 : add-to-blacklist ( error vocab -- )
142 vocab-name blacklist get [ set-at ] [ 2drop ] if* ;
144 GENERIC: (require) ( name -- )
148 dup source-loaded?>> +parsing+ eq? [ drop ] [
149 dup source-loaded?>> [ dup load-source ] unless
150 dup docs-loaded?>> [ dup load-docs ] unless
153 ] [ [ swap add-to-blacklist ] keep rethrow ] recover ;
155 M: vocab-link (require)
156 vocab-name (require) ;
158 M: string (require) create-vocab (require) ;
163 dup vocab-name blacklist get at*
166 drop dup find-vocab-root
168 [ dup lookup-vocab [ drop ] [ no-vocab ] if ]
171 ] require-hook set-global
173 M: vocab-spec where vocab-source-path dup [ 1 2array ] when ;
175 ! put here to avoid circularity between vocabs.loader and source-files.errors
176 { "source-files.errors" "debugger" } "source-files.errors.debugger" require-when