1 ! Copyright (C) 2007, 2010 Daniel Ehrenberg, Bruno Deferrari,
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: assocs hashtables kernel namespaces sequences
5 sets strings vocabs sorting accessors arrays compiler.units
6 combinators vectors splitting continuations math words
10 ERROR: no-word-error name ;
12 : word-restarts ( possibilities -- restarts )
14 [ [ vocabulary>> "Use the " " vocabulary" surround ] keep ] { } map>assoc ;
16 : word-restarts-with-defer ( name possibilities -- restarts )
18 swap "Defer word in current vocabulary" swap 2array
21 : <no-word-error> ( name possibilities -- error restarts )
22 [ drop \ no-word-error boa ] [ word-restarts-with-defer ] 2bi ;
26 { search-vocab-names hashtable }
27 { search-vocabs vector }
28 { qualified-vocabs vector }
29 { auto-used vector } ;
31 : <manifest> ( -- manifest )
33 H{ } clone >>search-vocab-names
34 V{ } clone >>search-vocabs
35 V{ } clone >>qualified-vocabs
36 V{ } clone >>auto-used ;
40 [ clone ] change-search-vocab-names
41 [ clone ] change-search-vocabs
42 [ clone ] change-qualified-vocabs
43 [ clone ] change-auto-used ;
45 TUPLE: extra-words words ;
48 over extra-words? [ [ words>> ] bi@ eq? ] [ 2drop f ] if ;
50 C: <extra-words> extra-words
52 : clear-manifest ( -- )
54 [ search-vocab-names>> clear-assoc ]
55 [ search-vocabs>> delete-all ]
56 [ qualified-vocabs>> delete-all ]
59 ERROR: no-word-in-vocab word vocab ;
63 : (add-qualified) ( qualified -- )
64 manifest get qualified-vocabs>> push ;
66 : (from) ( vocab words -- vocab words words' vocab )
67 2dup swap load-vocab ;
69 : extract-words ( seq vocab -- assoc )
70 [ words>> extract-keys dup ] [ name>> ] bi
71 [ swap [ 2drop ] [ no-word-in-vocab ] if ] curry assoc-each ;
73 : excluding-words ( seq vocab -- assoc )
74 [ nip words>> ] [ extract-words ] 2bi assoc-diff ;
76 : qualified-words ( prefix vocab -- assoc )
77 words>> swap [ swap [ swap ":" glue ] dip ] curry assoc-map ;
79 : (lookup) ( name assoc -- word/f )
80 at dup forward-reference? [ drop f ] when ;
82 : (use-words) ( assoc -- extra-words seq )
83 <extra-words> manifest get qualified-vocabs>> ;
87 : set-current-vocab ( name -- )
89 [ manifest get current-vocab<< ] [ (add-qualified) ] bi ;
91 : with-current-vocab ( name quot -- )
92 manifest get clone manifest [
93 [ set-current-vocab ] dip call
94 ] with-variable ; inline
96 TUPLE: no-current-vocab ;
98 : no-current-vocab ( -- vocab )
99 \ no-current-vocab boa
100 { { "Define words in scratchpad vocabulary" "scratchpad" } }
101 throw-restarts dup set-current-vocab ;
103 : current-vocab ( -- vocab )
104 manifest get current-vocab>> [ no-current-vocab ] unless* ;
106 : begin-private ( -- )
107 current-vocab name>> ".private" ?tail
108 [ drop ] [ ".private" append set-current-vocab ] if ;
111 current-vocab name>> ".private" ?tail
112 [ set-current-vocab ] [ drop ] if ;
114 : using-vocab? ( vocab -- ? )
115 vocab-name manifest get search-vocab-names>> key? ;
117 : use-vocab ( vocab -- )
119 [ vocab-name "Already using ``" "'' vocabulary" surround note. ] [
121 [ [ load-vocab ] dip search-vocabs>> push ]
122 [ [ vocab-name ] dip search-vocab-names>> conjoin ]
126 : auto-use-vocab ( vocab -- )
127 [ use-vocab ] [ manifest get auto-used>> push ] bi ;
129 : auto-used? ( -- ? ) manifest get auto-used>> length 0 > ;
131 : unuse-vocab ( vocab -- )
134 [ [ load-vocab ] dip search-vocabs>> remove-eq! drop ]
135 [ [ vocab-name ] dip search-vocab-names>> delete-at ]
139 TUPLE: qualified vocab prefix words ;
141 : <qualified> ( vocab prefix -- qualified )
142 (from) qualified-words qualified boa ;
144 : add-qualified ( vocab prefix -- )
145 <qualified> (add-qualified) ;
147 TUPLE: from vocab names words ;
149 : <from> ( vocab words -- from )
150 (from) extract-words from boa ;
152 : add-words-from ( vocab words -- )
153 <from> (add-qualified) ;
155 TUPLE: exclude vocab names words ;
157 : <exclude> ( vocab words -- from )
158 (from) excluding-words exclude boa ;
160 : add-words-excluding ( vocab words -- )
161 <exclude> (add-qualified) ;
163 TUPLE: rename word vocab words ;
165 : <rename> ( word vocab new-name -- rename )
166 [ 2dup load-vocab words>> dupd at [ ] [ swap no-word-in-vocab ] ?if ] dip
167 associate rename boa ;
169 : add-renamed-word ( word vocab new-name -- )
170 <rename> (add-qualified) ;
172 : use-words ( assoc -- ) (use-words) push ;
174 : unuse-words ( assoc -- ) (use-words) remove! drop ;
176 TUPLE: ambiguous-use-error words ;
178 : <ambiguous-use-error> ( words -- error restarts )
179 [ \ ambiguous-use-error boa ] [ word-restarts ] bi ;
183 : (vocab-search) ( name assocs -- words n )
184 [ words>> (lookup) ] with map
187 : vocab-search ( name manifest -- word/f )
193 drop <ambiguous-use-error> throw-restarts
194 dup [ vocabulary>> ] [ name>> 1array ] bi add-words-from
198 : qualified-search ( name manifest -- word/f )
200 (vocab-search) 0 = [ drop f ] [ last ] if ;
204 : search-manifest ( name manifest -- word/f )
205 2dup qualified-search dup [ 2nip ] [ drop vocab-search ] if ;
207 : search ( name -- word/f )
208 manifest get search-manifest ;
212 GENERIC: update ( search-path-elt -- valid? )
214 : trim-forgotten ( qualified-vocab -- valid? )
215 [ [ nip "forgotten" word-prop not ] assoc-filter ] change-words
216 words>> assoc-empty? not ;
218 M: from update trim-forgotten ;
219 M: rename update trim-forgotten ;
220 M: extra-words update trim-forgotten ;
221 M: exclude update trim-forgotten ;
225 dup [ prefix>> ] [ vocab>> load-vocab ] bi qualified-words
229 M: vocab update dup name>> vocab eq? ;
231 : update-manifest ( manifest -- )
232 [ dup [ name>> vocab ] when ] change-current-vocab
233 [ [ drop vocab ] assoc-filter ] change-search-vocab-names
234 dup search-vocab-names>> keys [ vocab ] V{ } map-as >>search-vocabs
235 qualified-vocabs>> [ update ] filter! drop ;
237 M: manifest definitions-changed ( assoc manifest -- )
238 nip update-manifest ;
242 : with-manifest ( quot -- )
243 <manifest> manifest [
245 [ manifest get add-definition-observer call ]
246 [ manifest get remove-definition-observer ]
250 ] with-variable ; inline