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.styles 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 [ >r path>> r> 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 ] when*
28 lexer get line>> number>string write ": " write
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 ERROR: no-current-vocab ;
57 : current-vocab ( -- str )
58 in get [ no-current-vocab ] unless* ;
60 : create-in ( str -- word )
61 current-vocab create dup set-word dup save-location ;
63 : CREATE ( -- word ) scan create-in ;
65 : CREATE-WORD ( -- word ) CREATE dup reset-generic ;
67 : word-restarts ( possibilities -- restarts )
70 "Use the " swap vocabulary>> " vocabulary" 3append
74 TUPLE: no-word-error name ;
76 : no-word ( name -- newword )
78 swap words-named [ forward-reference? not ] filter
79 word-restarts throw-restarts
80 dup vocabulary>> (use+) ;
82 : check-forward ( str word -- word/f )
83 dup forward-reference? [
87 [ forward-reference? not ] find nip
92 : search ( str -- word/f )
93 dup use get assoc-stack check-forward ;
95 : scan-word ( -- word/number/f )
98 dup string>number [ ] [ no-word ] ?if
102 ERROR: staging-violation word ;
104 : execute-parsing ( word -- )
105 dup changed-definitions get key? [ staging-violation ] when
108 : scan-object ( -- object )
109 scan-word dup parsing-word?
110 [ V{ } clone swap execute-parsing first ] when ;
112 : parse-step ( accum end -- accum ? )
114 { [ 2dup eq? ] [ 2drop f ] }
115 { [ dup not ] [ drop unexpected-eof t ] }
116 { [ dup delimiter? ] [ unexpected t ] }
117 { [ dup parsing-word? ] [ nip execute-parsing t ] }
121 : (parse-until) ( accum end -- accum )
122 dup >r parse-step [ r> (parse-until) ] [ r> drop ] if ;
124 : parse-until ( end -- vec )
125 100 <vector> swap (parse-until) ;
127 : parsed ( accum obj -- accum ) over push ;
129 : (parse-lines) ( lexer -- quot )
130 [ f parse-until >quotation ] with-lexer ;
132 : parse-lines ( lines -- quot )
133 lexer-factory get call (parse-lines) ;
135 : parse-literal ( accum end quot -- accum )
136 >r parse-until r> call parsed ; inline
138 : parse-definition ( -- quot )
139 \ ; parse-until >quotation ;
141 : (:) ( -- word def ) CREATE-WORD parse-definition ;
145 : parse-base ( parsed base -- parsed )
146 scan swap base> [ bad-number ] unless* parsed ;
148 SYMBOL: bootstrap-syntax
150 : with-file-vocabs ( quot -- )
152 f in set { "syntax" } set-use
153 bootstrap-syntax get [ use get push ] when*
155 ] with-scope ; inline
157 SYMBOL: interactive-vocabs
197 } interactive-vocabs set-global
199 : with-interactive-vocabs ( quot -- )
202 interactive-vocabs get set-use
204 ] with-scope ; inline
206 : parse-fresh ( lines -- quot )
207 [ parse-lines ] with-file-vocabs ;
209 : parsing-file ( file -- )
213 "Loading " write print flush
216 : filter-moved ( assoc1 assoc2 -- seq )
218 drop where dup [ first ] when
219 file get source-file-path =
220 ] assoc-filter keys ;
222 : removed-definitions ( -- assoc1 assoc2 )
223 new-definitions old-definitions
224 [ get first2 assoc-union ] bi@ ;
226 : removed-classes ( -- assoc1 assoc2 )
227 new-definitions old-definitions
230 : forget-removed-definitions ( -- )
231 removed-definitions filter-moved forget-all ;
233 : reset-removed-classes ( -- )
235 filter-moved [ class? ] filter [ forget-class ] each ;
237 : fix-class-words ( -- )
238 #! If a class word had a compound definition which was
239 #! removed, it must go back to being a symbol.
240 new-definitions get first2
241 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
243 : forget-smudged ( -- )
244 forget-removed-definitions
245 reset-removed-classes
248 : finish-parsing ( lines quot -- )
251 [ record-definitions ]
255 : parse-stream ( stream name -- quot )
258 lines dup parse-fresh
262 ] with-compilation-unit ;
264 : parse-file-restarts ( file -- restarts )
265 "Load " swap " again" 3append t 2array 1array ;
267 : parse-file ( file -- quot )
270 [ parsing-file ] keep
271 [ utf8 <file-reader> ] keep
273 ] with-compiler-errors
275 over parse-file-restarts rethrow-restarts
279 : run-file ( file -- )
280 [ dup parse-file call ] assert-depth drop ;
282 : ?run-file ( path -- )
283 dup exists? [ run-file ] [ drop ] if ;