1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays definitions generic assocs kernel math namespaces
4 sequences strings vectors words quotations io
5 combinators sorting splitting math.parser effects continuations
6 io.files io.streams.string vocabs io.encodings.utf8 source-files
7 classes hashtables compiler.errors compiler.units accessors sets
12 file get lexer get line>> 2dup and
13 [ [ path>> ] dip 2array ] [ 2drop f ] if ;
15 : save-location ( definition -- )
16 location remember-definition ;
20 t parser-notes set-global
22 : parser-notes? ( -- ? )
23 parser-notes get "quiet" get not and ;
27 file get [ path>> write ":" write ] when*
28 lexer get [ line>> number>string write ": " write ] when*
29 "Note: " write dup print
36 vocab-words use get push ;
41 : add-use ( seq -- ) [ use+ ] each ;
44 [ vocab-words ] V{ } map-as sift use set ;
46 : check-vocab-string ( name -- name )
48 [ "Vocabulary name must be a string" throw ] unless ;
51 check-vocab-string dup in set create-vocab (use+) ;
53 M: parsing-word stack-effect drop (( parsed -- parsed )) ;
55 TUPLE: no-current-vocab ;
57 : no-current-vocab ( -- vocab )
58 \ no-current-vocab boa
59 { { "Define words in scratchpad vocabulary" "scratchpad" } }
60 throw-restarts dup set-in ;
62 : current-vocab ( -- str )
63 in get [ no-current-vocab ] unless* ;
65 : create-in ( str -- word )
66 current-vocab create dup set-word dup save-location ;
68 : CREATE ( -- word ) scan create-in ;
70 : CREATE-WORD ( -- word ) CREATE dup reset-generic ;
72 : word-restarts ( name possibilities -- restarts )
74 [ [ vocabulary>> "Use the " " vocabulary" surround ] keep ] { } map>assoc
75 swap "Defer word in current vocabulary" swap 2array
78 ERROR: no-word-error name ;
80 : <no-word-error> ( name possibilities -- error restarts )
81 [ drop \ no-word-error boa ] [ word-restarts ] 2bi ;
87 : no-word-restarted ( restart-value -- word )
91 [ amended-use get dup [ push ] [ 2drop ] if ]
92 [ "Added ``" "'' vocabulary to search path" surround note. ]
96 : no-word ( name -- newword )
97 dup words-named [ forward-reference? not ] filter
98 dup length 1 = auto-use? get and
99 [ nip first no-word-restarted ]
100 [ <no-word-error> throw-restarts no-word-restarted ]
103 : check-forward ( str word -- word/f )
104 dup forward-reference? [
108 [ forward-reference? not ] find nip
113 : search ( str -- word/f )
114 dup use get assoc-stack check-forward ;
116 : scan-word ( -- word/number/f )
119 dup string>number [ ] [ no-word ] ?if
123 ERROR: staging-violation word ;
125 : execute-parsing ( word -- )
126 dup changed-definitions get key? [ staging-violation ] when
129 : scan-object ( -- object )
130 scan-word dup parsing-word?
131 [ V{ } clone swap execute-parsing first ] when ;
133 : parse-step ( accum end -- accum ? )
135 { [ 2dup eq? ] [ 2drop f ] }
136 { [ dup not ] [ drop unexpected-eof t ] }
137 { [ dup delimiter? ] [ unexpected t ] }
138 { [ dup parsing-word? ] [ nip execute-parsing t ] }
142 : (parse-until) ( accum end -- accum )
143 [ parse-step ] keep swap [ (parse-until) ] [ drop ] if ;
145 : parse-until ( end -- vec )
146 100 <vector> swap (parse-until) ;
148 : parsed ( accum obj -- accum ) over push ;
150 : (parse-lines) ( lexer -- quot )
152 f parse-until >quotation
155 : parse-lines ( lines -- quot )
156 lexer-factory get call (parse-lines) ;
158 : parse-literal ( accum end quot -- accum )
159 [ parse-until ] dip call parsed ; inline
161 : parse-definition ( -- quot )
162 \ ; parse-until >quotation ;
164 : (:) ( -- word def ) CREATE-WORD parse-definition ;
168 : parse-base ( parsed base -- parsed )
169 scan swap base> [ bad-number ] unless* parsed ;
171 SYMBOL: bootstrap-syntax
173 : with-file-vocabs ( quot -- )
175 f in set { "syntax" } set-use
176 bootstrap-syntax get [ use get push ] when*
178 ] with-scope ; inline
180 SYMBOL: interactive-vocabs
223 } interactive-vocabs set-global
225 : with-interactive-vocabs ( quot -- )
228 interactive-vocabs get set-use
230 ] with-scope ; inline
232 SYMBOL: print-use-hook
234 print-use-hook global [ [ ] or ] change-at
236 : parse-fresh ( lines -- quot )
238 V{ } clone amended-use set
240 amended-use get empty? [ print-use-hook get call ] unless
243 : parsing-file ( file -- )
244 "quiet" get [ drop ] [ "Loading " write print flush ] if ;
246 : filter-moved ( assoc1 assoc2 -- seq )
248 drop where dup [ first ] when
250 ] assoc-filter keys ;
252 : removed-definitions ( -- assoc1 assoc2 )
253 new-definitions old-definitions
254 [ get first2 assoc-union ] bi@ ;
256 : removed-classes ( -- assoc1 assoc2 )
257 new-definitions old-definitions
260 : forget-removed-definitions ( -- )
261 removed-definitions filter-moved forget-all ;
263 : reset-removed-classes ( -- )
265 filter-moved [ class? ] filter [ forget-class ] each ;
267 : fix-class-words ( -- )
268 #! If a class word had a compound definition which was
269 #! removed, it must go back to being a symbol.
270 new-definitions get first2
271 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
273 : forget-smudged ( -- )
274 forget-removed-definitions
275 reset-removed-classes
278 : finish-parsing ( lines quot -- )
281 [ record-definitions ]
285 : parse-stream ( stream name -- quot )
288 lines dup parse-fresh
292 ] with-compilation-unit ;
294 : parse-file-restarts ( file -- restarts )
295 "Load " " again" surround t 2array 1array ;
297 : parse-file ( file -- quot )
300 [ parsing-file ] keep
301 [ utf8 <file-reader> ] keep
303 ] with-compiler-errors
305 over parse-file-restarts rethrow-restarts
309 : run-file ( file -- )
310 [ parse-file call ] curry assert-depth ;
312 : ?run-file ( path -- )
313 dup exists? [ run-file ] [ drop ] if ;