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 [ [ "Use the " swap vocabulary>> " vocabulary" 3append ] 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 )
92 "Added ``" swap "'' vocabulary to search path" 3append 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 )
241 print-use-hook get call
245 : parsing-file ( file -- )
249 "Loading " write print flush
252 : filter-moved ( assoc1 assoc2 -- seq )
254 drop where dup [ first ] when
256 ] assoc-filter keys ;
258 : removed-definitions ( -- assoc1 assoc2 )
259 new-definitions old-definitions
260 [ get first2 assoc-union ] bi@ ;
262 : removed-classes ( -- assoc1 assoc2 )
263 new-definitions old-definitions
266 : forget-removed-definitions ( -- )
267 removed-definitions filter-moved forget-all ;
269 : reset-removed-classes ( -- )
271 filter-moved [ class? ] filter [ forget-class ] each ;
273 : fix-class-words ( -- )
274 #! If a class word had a compound definition which was
275 #! removed, it must go back to being a symbol.
276 new-definitions get first2
277 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
279 : forget-smudged ( -- )
280 forget-removed-definitions
281 reset-removed-classes
284 : finish-parsing ( lines quot -- )
287 [ record-definitions ]
291 : parse-stream ( stream name -- quot )
294 lines dup parse-fresh
298 ] with-compilation-unit ;
300 : parse-file-restarts ( file -- restarts )
301 "Load " swap " again" 3append t 2array 1array ;
303 : parse-file ( file -- quot )
306 [ parsing-file ] keep
307 [ utf8 <file-reader> ] keep
309 ] with-compiler-errors
311 over parse-file-restarts rethrow-restarts
315 : run-file ( file -- )
316 [ dup parse-file call ] assert-depth drop ;
318 : ?run-file ( path -- )
319 dup exists? [ run-file ] [ drop ] if ;