1 ! Copyright (C) 2007, 2008 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 compiler.errors splitting init accessors sets ;
16 } clone vocab-roots set-global
18 : add-vocab-root ( root -- )
19 vocab-roots get adjoin ;
21 : vocab-dir ( vocab -- dir )
22 vocab-name { { CHAR: . CHAR: / } } substitute ;
24 : vocab-dir+ ( vocab str/f -- path )
25 [ vocab-name "." split ] dip
26 [ [ dup peek ] dip append suffix ] when*
29 : vocab-dir? ( root name -- ? )
31 [ ".factor" vocab-dir+ append-path exists? ]
37 H{ } clone root-cache set-global
41 : (find-vocab-root) ( name -- path/f )
42 vocab-roots get swap [ vocab-dir? ] curry find nip ;
46 : find-vocab-root ( vocab -- path/f )
47 vocab-name dup root-cache get at [ ] [ (find-vocab-root) ] ?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 ;
62 : load-source ( vocab -- )
64 +parsing+ >>source-loaded?
65 dup vocab-source-path [ parse-file ] [ [ ] ] if*
66 [ +parsing+ >>source-loaded? ] dip
67 [ % ] [ assert-depth ] if-bootstrapping
68 +done+ >>source-loaded? drop
69 ] [ ] [ f >>source-loaded? ] cleanup ;
71 : load-docs ( vocab -- )
74 +parsing+ >>docs-loaded?
75 [ vocab-docs-path [ ?run-file ] when* ] keep
77 ] [ ] [ f >>docs-loaded? ] cleanup
82 : require ( vocab -- )
83 [ load-vocab drop ] with-compiler-errors ;
87 [ [ [ load-source ] [ load-docs ] bi ] with-compiler-errors ]
92 dup load-vocab vocab-main [
95 "The " write vocab-name write
96 " vocabulary does not define an entry point." print
97 "To define one, refer to \\ MAIN: help" print
104 : add-to-blacklist ( error vocab -- )
105 vocab-name blacklist get dup [ set-at ] [ 3drop ] if ;
107 GENERIC: (load-vocab) ( name -- )
109 M: vocab (load-vocab)
111 dup source-loaded?>> +parsing+ eq? [
112 dup source-loaded?>> [ dup load-source ] unless
113 dup docs-loaded?>> [ dup load-docs ] unless
115 ] [ [ swap add-to-blacklist ] keep rethrow ] recover ;
117 M: vocab-link (load-vocab)
118 vocab-name create-vocab (load-vocab) ;
120 M: string (load-vocab)
121 create-vocab (load-vocab) ;
125 dup vocab-name blacklist get at* [ rethrow ] [
126 drop dup find-vocab-root
127 [ [ (load-vocab) ] with-compiler-errors ]
128 [ dup vocab [ drop ] [ no-vocab ] if ]
131 ] with-compiler-errors
132 ] load-vocab-hook set-global
136 : vocab-where ( vocab -- loc )
137 vocab-source-path dup [ 1 2array ] when ;
139 M: vocab where vocab-where ;
141 M: vocab-link where vocab-where ;