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 ;
30 root-cache [ H{ } clone ] initialize
32 ERROR: not-found-in-roots path ;
36 : find-root-for ( path -- path/f )
37 vocab-roots get [ prepend-path exists? ] with find nip ;
39 M: string vocab-path ( string -- path/f )
40 dup find-root-for [ prepend-path ] [ not-found-in-roots ] if* ;
44 : vocab-dir ( vocab -- dir )
45 vocab-name H{ { CHAR: . CHAR: / } } substitute ;
47 : append-vocab-dir ( vocab str/f -- path )
48 [ vocab-name ".private" ?tail drop "." split ] dip
49 [ [ dup last ] dip append suffix ] when*
52 : find-vocab-root ( vocab -- path/f )
53 vocab-name root-cache get
54 [ ".factor" append-vocab-dir find-root-for ] cache ;
56 : vocab-append-path ( vocab path -- newpath )
57 swap find-vocab-root dup [ prepend-path ] [ 2drop f ] if ;
59 : vocab-source-path ( vocab -- path/f )
60 dup ".factor" append-vocab-dir vocab-append-path ;
62 : vocab-docs-path ( vocab -- path/f )
63 dup "-docs.factor" append-vocab-dir vocab-append-path ;
67 ! Defined by vocabs.metadata
68 SYMBOL: check-vocab-hook
70 check-vocab-hook [ [ drop ] ] initialize
74 SYMBOL: require-when-vocabs
75 require-when-vocabs [ HS{ } clone ] initialize
77 SYMBOL: require-when-table
78 require-when-table [ V{ } clone ] initialize
80 : load-conditional-requires ( vocab -- )
81 vocab-name require-when-vocabs get in? [
82 require-when-table get [
83 [ [ lookup-vocab dup [ source-loaded?>> +done+ = ] when ] all? ] dip
84 [ require ] curry when
88 : load-source ( vocab -- )
89 dup check-vocab-hook get call( vocab -- )
91 +parsing+ >>source-loaded?
92 dup vocab-source-path [ parse-file ] [ [ ] ] if*
93 [ +parsing+ >>source-loaded? ] dip
94 [ % ] [ call( -- ) ] if-bootstrapping
95 +done+ >>source-loaded?
96 load-conditional-requires
97 ] [ ] [ f >>source-loaded? ] cleanup ;
99 : load-docs ( vocab -- )
102 +parsing+ >>docs-loaded?
103 [ vocab-docs-path [ ?run-file ] when* ] keep
104 +done+ >>docs-loaded?
105 ] [ ] [ f >>docs-loaded? ] cleanup
110 : require-when ( if then -- )
111 over [ lookup-vocab ] all? [
114 [ drop [ require-when-vocabs get adjoin ] each ]
115 [ 2array require-when-table get push ] 2bi
120 [ [ load-source ] [ load-docs ] bi ]
125 dup load-vocab vocab-main [
128 "The " write vocab-name write
129 " vocabulary does not define an entry point." print
130 "To define one, refer to \\ MAIN: help" print
137 : add-to-blacklist ( error vocab -- )
138 vocab-name blacklist get dup [ set-at ] [ 3drop ] if ;
140 GENERIC: (require) ( name -- )
144 dup source-loaded?>> +parsing+ eq? [ drop ] [
145 dup source-loaded?>> [ dup load-source ] unless
146 dup docs-loaded?>> [ dup load-docs ] unless
149 ] [ [ swap add-to-blacklist ] keep rethrow ] recover ;
151 M: vocab-link (require)
152 vocab-name (require) ;
154 M: string (require) create-vocab (require) ;
159 dup vocab-name blacklist get at*
162 drop dup find-vocab-root
164 [ dup lookup-vocab [ drop ] [ no-vocab ] if ]
167 ] require-hook set-global
169 M: vocab-spec where vocab-source-path dup [ 1 2array ] when ;
171 ! put here to avoid circularity between vocabs.loader and source-files.errors
172 { "source-files.errors" "debugger" } "source-files.errors.debugger" require-when