1 ! Copyright (C) 2007, 2010 Daniel Ehrenberg, Bruno Deferrari,
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays assocs combinators compiler.units
5 continuations hashtables kernel math namespaces parser.notes
6 sequences sets sorting splitting vectors vocabs words ;
9 ERROR: no-word-error name ;
11 : word-restarts ( possibilities -- restarts )
13 [ [ vocabulary>> "Use the " " vocabulary" surround ] keep ] { } map>assoc ;
15 : word-restarts-with-defer ( name possibilities -- restarts )
17 swap "Defer word in current vocabulary" swap 2array
20 : <no-word-error> ( name possibilities -- error restarts )
21 [ drop \ no-word-error boa ] [ word-restarts-with-defer ] 2bi ;
25 { search-vocab-names hashtable }
26 { search-vocabs vector }
27 { qualified-vocabs vector }
28 { auto-used vector } ;
30 : <manifest> ( -- manifest )
32 H{ } clone >>search-vocab-names
33 V{ } clone >>search-vocabs
34 V{ } clone >>qualified-vocabs
35 V{ } clone >>auto-used ;
39 [ clone ] change-search-vocab-names
40 [ clone ] change-search-vocabs
41 [ clone ] change-qualified-vocabs
42 [ clone ] change-auto-used ;
44 TUPLE: extra-words words ;
47 over extra-words? [ [ words>> ] bi@ eq? ] [ 2drop f ] if ;
49 C: <extra-words> extra-words
51 ERROR: no-word-in-vocab word vocab ;
55 : (add-qualified) ( qualified -- )
56 manifest get qualified-vocabs>> push ;
58 : (from) ( vocab words -- vocab words words' vocab )
59 2dup swap load-vocab ;
61 : extract-words ( seq vocab -- assoc )
62 [ words>> extract-keys dup ] [ name>> ] bi
63 [ swap [ 2drop ] [ no-word-in-vocab ] if ] curry assoc-each ;
65 : excluding-words ( seq vocab -- assoc )
66 [ nip words>> ] [ extract-words ] 2bi assoc-diff ;
68 : qualified-words ( prefix vocab -- assoc )
69 words>> swap [ swap [ swap ":" glue ] dip ] curry assoc-map ;
71 : (lookup) ( name assoc -- word/f )
72 at* [ dup forward-reference? [ drop f ] when ] when ;
74 : (use-words) ( assoc -- extra-words seq )
75 <extra-words> manifest get qualified-vocabs>> ;
79 : set-current-vocab ( name -- )
81 [ manifest get current-vocab<< ] [ (add-qualified) ] bi ;
83 : with-current-vocab ( name quot -- )
84 manifest get clone manifest [
85 [ set-current-vocab ] dip call
86 ] with-variable ; inline
88 TUPLE: no-current-vocab-error ;
90 : no-current-vocab ( -- vocab )
91 \ no-current-vocab-error boa
92 { { "Define words in scratchpad vocabulary" "scratchpad" } }
93 throw-restarts dup set-current-vocab ;
95 : current-vocab ( -- vocab )
96 manifest get current-vocab>> [ no-current-vocab ] unless* ;
98 : begin-private ( -- )
99 current-vocab name>> ".private" ?tail
100 [ drop ] [ ".private" append set-current-vocab ] if ;
103 current-vocab name>> ".private" ?tail
104 [ set-current-vocab ] [ drop ] if ;
106 : using-vocab? ( vocab -- ? )
107 vocab-name manifest get search-vocab-names>> key? ;
109 : use-vocab ( vocab -- )
111 [ vocab-name "Already using ``" "'' vocabulary" surround note. ] [
113 [ [ load-vocab ] dip search-vocabs>> push ]
114 [ [ vocab-name ] dip search-vocab-names>> conjoin ]
118 : auto-use-vocab ( vocab -- )
119 [ use-vocab ] [ manifest get auto-used>> push ] bi ;
121 : auto-used? ( -- ? ) manifest get auto-used>> length 0 > ;
123 : unuse-vocab ( vocab -- )
126 [ [ load-vocab ] dip search-vocabs>> remove-eq! drop ]
127 [ [ vocab-name ] dip search-vocab-names>> delete-at ]
131 TUPLE: qualified vocab prefix words ;
133 : <qualified> ( vocab prefix -- qualified )
134 (from) qualified-words qualified boa ;
136 : add-qualified ( vocab prefix -- )
137 <qualified> (add-qualified) ;
139 TUPLE: from vocab names words ;
141 : <from> ( vocab words -- from )
142 (from) extract-words from boa ;
144 : add-words-from ( vocab words -- )
145 <from> (add-qualified) ;
147 TUPLE: exclude vocab names words ;
149 : <exclude> ( vocab words -- from )
150 (from) excluding-words exclude boa ;
152 : add-words-excluding ( vocab words -- )
153 <exclude> (add-qualified) ;
155 TUPLE: rename word vocab words ;
157 : <rename> ( word vocab new-name -- rename )
158 [ 2dup load-vocab words>> dupd at [ ] [ swap no-word-in-vocab ] ?if ] dip
159 associate rename boa ;
161 : add-renamed-word ( word vocab new-name -- )
162 <rename> (add-qualified) ;
164 : use-words ( assoc -- ) (use-words) push ;
166 : unuse-words ( assoc -- ) (use-words) remove! drop ;
168 TUPLE: ambiguous-use-error words ;
170 : <ambiguous-use-error> ( words -- error restarts )
171 [ \ ambiguous-use-error boa ] [ word-restarts ] bi ;
175 : (vocab-search) ( name assocs -- words n )
176 [ words>> (lookup) ] with map
179 : vocab-search ( name manifest -- word/f )
185 drop <ambiguous-use-error> throw-restarts
186 dup [ vocabulary>> ] [ name>> 1array ] bi add-words-from
190 : qualified-search ( name manifest -- word/f )
192 (vocab-search) 0 = [ drop f ] [ last ] if ;
196 : search-manifest ( name manifest -- word/f )
197 2dup qualified-search dup [ 2nip ] [ drop vocab-search ] if ;
199 : search ( name -- word/f )
200 manifest get search-manifest ;
204 GENERIC: update ( search-path-elt -- valid? )
206 : trim-forgotten ( qualified-vocab -- valid? )
207 [ [ nip "forgotten" word-prop not ] assoc-filter ] change-words
208 words>> assoc-empty? not ;
210 M: from update trim-forgotten ;
211 M: rename update trim-forgotten ;
212 M: extra-words update trim-forgotten ;
213 M: exclude update trim-forgotten ;
216 dup vocab>> lookup-vocab [
217 dup [ prefix>> ] [ vocab>> load-vocab ] bi qualified-words
221 M: vocab update dup name>> lookup-vocab eq? ;
223 : update-manifest ( manifest -- )
224 [ dup [ name>> lookup-vocab ] when ] change-current-vocab
225 [ [ drop lookup-vocab ] assoc-filter ] change-search-vocab-names
226 dup search-vocab-names>> keys [ lookup-vocab ] V{ } map-as >>search-vocabs
227 qualified-vocabs>> [ update ] filter! drop ;
229 M: manifest definitions-changed ( assoc manifest -- )
230 nip update-manifest ;
234 : with-manifest ( quot -- )
235 <manifest> manifest [
237 [ manifest get add-definition-observer call ]
238 [ manifest get remove-definition-observer ]
242 ] with-variable ; inline