1 ! Copyright (C) 2007, 2010 Eduardo Cavazos, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces make sequences io io.files io.pathnames kernel
4 assocs words vocabs definitions parser continuations hashtables
5 sorting source-files arrays combinators strings system
6 math.parser splitting init accessors sets ;
16 } clone vocab-roots set-global
18 : add-vocab-root ( root -- )
19 vocab-roots get adjoin ;
23 root-cache [ H{ } clone ] initialize
25 ERROR: not-found-in-roots path ;
29 : find-root-for ( path -- path/f )
30 vocab-roots get [ prepend-path exists? ] with find nip ;
32 M: string vocab-path ( string -- path/f )
33 dup find-root-for [ prepend-path ] [ not-found-in-roots ] if* ;
37 : vocab-dir ( vocab -- dir )
38 vocab-name { { CHAR: . CHAR: / } } substitute ;
40 : vocab-dir+ ( vocab str/f -- path )
41 [ vocab-name "." split ] dip
42 [ [ dup last ] dip append suffix ] when*
45 : find-vocab-root ( vocab -- path/f )
46 vocab-name dup root-cache get at
47 [ ] [ ".factor" vocab-dir+ find-root-for ] ?if ;
49 : vocab-append-path ( vocab path -- newpath )
50 swap find-vocab-root dup [ prepend-path ] [ 2drop f ] if ;
52 : vocab-source-path ( vocab -- path/f )
53 dup ".factor" vocab-dir+ vocab-append-path ;
55 : vocab-docs-path ( vocab -- path/f )
56 dup "-docs.factor" vocab-dir+ vocab-append-path ;
60 ! Defined by vocabs.metadata
61 SYMBOL: check-vocab-hook
63 check-vocab-hook [ [ drop ] ] initialize
69 SYMBOL: require-when-vocabs
70 require-when-vocabs [ HS{ } clone ] initialize
72 SYMBOL: require-when-table
73 require-when-table [ V{ } clone ] initialize
75 : load-conditional-requires ( vocab -- )
76 vocab-name require-when-vocabs get in? [
77 require-when-table get [
78 [ [ vocab ] all? ] dip
79 [ require ] curry when
83 : load-source ( vocab -- )
84 dup check-vocab-hook get call( vocab -- )
86 +parsing+ >>source-loaded?
87 dup vocab-source-path [ parse-file ] [ [ ] ] if*
88 [ +parsing+ >>source-loaded? ] dip
89 [ % ] [ call( -- ) ] if-bootstrapping
90 +done+ >>source-loaded?
91 load-conditional-requires
92 ] [ ] [ f >>source-loaded? ] cleanup ;
94 : load-docs ( vocab -- )
97 +parsing+ >>docs-loaded?
98 [ vocab-docs-path [ ?run-file ] when* ] keep
100 ] [ ] [ f >>docs-loaded? ] cleanup
105 : require ( vocab -- )
108 : require-when ( if then -- )
109 over [ vocab ] all? [
112 [ drop [ require-when-vocabs get adjoin ] each ]
113 [ 2array require-when-table get push ] 2bi
118 [ [ load-source ] [ load-docs ] bi ]
123 dup load-vocab vocab-main [
126 "The " write vocab-name write
127 " vocabulary does not define an entry point." print
128 "To define one, refer to \\ MAIN: help" print
135 : add-to-blacklist ( error vocab -- )
136 vocab-name blacklist get dup [ set-at ] [ 3drop ] if ;
138 GENERIC: (load-vocab) ( name -- vocab )
140 M: vocab (load-vocab)
142 dup source-loaded?>> +parsing+ eq? [
143 dup source-loaded?>> [ dup load-source ] unless
144 dup docs-loaded?>> [ dup load-docs ] unless
146 ] [ [ swap add-to-blacklist ] keep rethrow ] recover ;
148 M: vocab-link (load-vocab)
149 vocab-name (load-vocab) ;
151 M: string (load-vocab) create-vocab (load-vocab) ;
156 dup vocab-name blacklist get at* [ rethrow ] [
157 drop dup find-vocab-root
158 [ (load-vocab) ] [ dup vocab [ ] [ no-vocab ] ?if ] if
160 ] load-vocab-hook set-global
162 M: vocab-spec where vocab-source-path dup [ 1 2array ] when ;