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 "." split ] dip
49 [ [ dup last ] dip append suffix ] when*
52 : find-vocab-root ( vocab -- path/f )
53 vocab-name root-cache get [
54 dup ".private" tail? [ drop f ] [
55 ".factor" append-vocab-dir find-root-for
59 : vocab-append-path ( vocab path -- newpath )
60 swap find-vocab-root dup [ prepend-path ] [ 2drop f ] if ;
62 : vocab-source-path ( vocab -- path/f )
63 dup ".factor" append-vocab-dir vocab-append-path ;
65 : vocab-docs-path ( vocab -- path/f )
66 dup "-docs.factor" append-vocab-dir vocab-append-path ;
70 ! Defined by vocabs.metadata
71 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 ] each ]
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
140 : add-to-blacklist ( error vocab -- )
141 vocab-name blacklist get dup [ set-at ] [ 3drop ] if ;
143 GENERIC: (require) ( name -- )
147 dup source-loaded?>> +parsing+ eq? [ drop ] [
148 dup source-loaded?>> [ dup load-source ] unless
149 dup docs-loaded?>> [ dup load-docs ] unless
152 ] [ [ swap add-to-blacklist ] keep rethrow ] recover ;
154 M: vocab-link (require)
155 vocab-name (require) ;
157 M: string (require) create-vocab (require) ;
162 dup vocab-name blacklist get at*
165 drop dup find-vocab-root
167 [ dup lookup-vocab [ drop ] [ no-vocab ] if ]
170 ] require-hook set-global
172 M: vocab-spec where vocab-source-path dup [ 1 2array ] when ;
174 ! put here to avoid circularity between vocabs.loader and source-files.errors
175 { "source-files.errors" "debugger" } "source-files.errors.debugger" require-when