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.files kernel assocs words
4 vocabs definitions parser continuations io hashtables sorting
5 source-files arrays combinators strings system math.parser
6 compiler.errors splitting init accessors ;
16 } clone vocab-roots set-global
18 : vocab-dir ( vocab -- dir )
19 vocab-name { { CHAR: . CHAR: / } } substitute ;
21 : vocab-dir+ ( vocab str/f -- path )
22 >r vocab-name "." split r>
23 [ >r dup peek r> append suffix ] when*
26 : vocab-dir? ( root name -- ? )
28 ".factor" vocab-dir+ append-path exists?
35 H{ } clone root-cache set-global
37 : (find-vocab-root) ( name -- path/f )
38 vocab-roots get swap [ vocab-dir? ] curry find nip ;
40 : find-vocab-root ( vocab -- path/f )
41 vocab-name dup root-cache get at [ ] [ (find-vocab-root) ] ?if ;
43 : vocab-append-path ( vocab path -- newpath )
44 swap find-vocab-root dup [ prepend-path ] [ 2drop f ] if ;
46 : vocab-source-path ( vocab -- path/f )
47 dup ".factor" vocab-dir+ vocab-append-path ;
49 : vocab-docs-path ( vocab -- path/f )
50 dup "-docs.factor" vocab-dir+ vocab-append-path ;
54 : load-source ( vocab -- vocab )
55 f over set-vocab-source-loaded?
56 [ vocab-source-path [ parse-file ] [ [ ] ] if* ] keep
57 t swap set-vocab-source-loaded?
58 [ % ] [ call ] if-bootstrapping ;
60 : load-docs ( vocab -- vocab )
62 f over set-vocab-docs-loaded?
63 [ vocab-docs-path [ ?run-file ] when* ] keep
64 t swap set-vocab-docs-loaded?
69 dup vocab [ [ load-source ] [ load-docs ] bi ] [ no-vocab ] ?if
70 ] with-compiler-errors ;
72 : require ( vocab -- )
76 dup load-vocab vocab-main [
79 "The " write vocab-name write
80 " vocabulary does not define an entry point." print
81 "To define one, refer to \\ MAIN: help" print
86 : add-to-blacklist ( error vocab -- )
87 vocab-name blacklist get dup [ set-at ] [ 3drop ] if ;
89 GENERIC: (load-vocab) ( name -- )
93 dup vocab-source-loaded? [ dup load-source ] unless
94 dup vocab-docs-loaded? [ dup load-docs ] unless
96 ] [ [ swap add-to-blacklist ] keep rethrow ] recover ;
98 M: vocab-link (load-vocab)
99 vocab-name create-vocab (load-vocab) ;
101 M: string (load-vocab)
102 create-vocab (load-vocab) ;
106 dup vocab-name blacklist get at* [
110 dup find-vocab-root [
111 [ (load-vocab) ] with-compiler-errors
113 dup vocab [ drop ] [ no-vocab ] if
116 ] with-compiler-errors
117 ] load-vocab-hook set-global
119 : vocab-where ( vocab -- loc )
120 vocab-source-path dup [ 1 2array ] when ;
122 M: vocab where vocab-where ;
124 M: vocab-link where vocab-where ;