1 ! Copyright (C) 2007, 2009 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 )
39 os windows? { { CHAR: . CHAR: \\ } } { { CHAR: . CHAR: / } } ?
42 : vocab-dir+ ( vocab str/f -- path )
43 [ vocab-name "." split ] dip
44 [ [ dup last ] dip append suffix ] when*
47 : find-vocab-root ( vocab -- path/f )
48 vocab-name dup root-cache get at
49 [ ] [ ".factor" vocab-dir+ find-root-for ] ?if ;
51 : vocab-append-path ( vocab path -- newpath )
52 swap find-vocab-root dup [ prepend-path ] [ 2drop f ] if ;
54 : vocab-source-path ( vocab -- path/f )
55 dup ".factor" vocab-dir+ vocab-append-path ;
57 : vocab-docs-path ( vocab -- path/f )
58 dup "-docs.factor" vocab-dir+ vocab-append-path ;
64 : load-source ( vocab -- )
66 +parsing+ >>source-loaded?
67 dup vocab-source-path [ parse-file ] [ [ ] ] if*
68 [ +parsing+ >>source-loaded? ] dip
69 [ % ] [ call( -- ) ] if-bootstrapping
70 +done+ >>source-loaded? drop
71 ] [ ] [ f >>source-loaded? ] cleanup ;
73 : load-docs ( vocab -- )
76 +parsing+ >>docs-loaded?
77 [ vocab-docs-path [ ?run-file ] when* ] keep
79 ] [ ] [ f >>docs-loaded? ] cleanup
84 : require ( vocab -- )
89 [ [ load-source ] [ load-docs ] bi ]
94 dup load-vocab vocab-main [
97 "The " write vocab-name write
98 " vocabulary does not define an entry point." print
99 "To define one, refer to \\ MAIN: help" print
106 : add-to-blacklist ( error vocab -- )
107 vocab-name blacklist get dup [ set-at ] [ 3drop ] if ;
109 GENERIC: (load-vocab) ( name -- vocab )
111 M: vocab (load-vocab)
113 dup source-loaded?>> +parsing+ eq? [
114 dup source-loaded?>> [ dup load-source ] unless
115 dup docs-loaded?>> [ dup load-docs ] unless
117 ] [ [ swap add-to-blacklist ] keep rethrow ] recover ;
119 M: vocab-link (load-vocab)
120 vocab-name create-vocab (load-vocab) ;
122 M: string (load-vocab)
123 create-vocab (load-vocab) ;
128 dup vocab-name blacklist get at* [ rethrow ] [
129 drop dup find-vocab-root
130 [ (load-vocab) ] [ dup vocab [ ] [ no-vocab ] ?if ] if
132 ] load-vocab-hook set-global
134 M: vocab-spec where vocab-source-path dup [ 1 2array ] when ;