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 words.symbol 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
32 M: parsing-word stack-effect drop (( parsed -- parsed )) ;
34 TUPLE: no-current-vocab ;
36 : no-current-vocab ( -- vocab )
37 \ no-current-vocab boa
38 { { "Define words in scratchpad vocabulary" "scratchpad" } }
39 throw-restarts dup set-in ;
41 : current-vocab ( -- str )
42 in get [ no-current-vocab ] unless* ;
44 : create-in ( str -- word )
45 current-vocab create dup set-word dup save-location ;
47 : CREATE ( -- word ) scan create-in ;
49 : CREATE-WORD ( -- word ) CREATE dup reset-generic ;
55 : no-word-restarted ( restart-value -- word )
59 [ amended-use get dup [ push ] [ 2drop ] if ]
60 [ "Added ``" "'' vocabulary to search path" surround note. ]
64 : no-word ( name -- newword )
65 dup words-named [ forward-reference? not ] filter
66 dup length 1 = auto-use? get and
67 [ nip first no-word-restarted ]
68 [ <no-word-error> throw-restarts no-word-restarted ]
71 : check-forward ( str word -- word/f )
72 dup forward-reference? [
76 [ forward-reference? not ] find nip
81 : search ( str -- word/f )
82 dup use get assoc-stack check-forward ;
84 : scan-word ( -- word/number/f )
87 dup string>number [ ] [ no-word ] ?if
91 ERROR: staging-violation word ;
93 : execute-parsing ( word -- )
94 dup changed-definitions get key? [ staging-violation ] when
97 : scan-object ( -- object )
98 scan-word dup parsing-word?
99 [ V{ } clone swap execute-parsing first ] when ;
101 : parse-step ( accum end -- accum ? )
103 { [ 2dup eq? ] [ 2drop f ] }
104 { [ dup not ] [ drop unexpected-eof t ] }
105 { [ dup delimiter? ] [ unexpected t ] }
106 { [ dup parsing-word? ] [ nip execute-parsing t ] }
110 : (parse-until) ( accum end -- accum )
111 [ parse-step ] keep swap [ (parse-until) ] [ drop ] if ;
113 : parse-until ( end -- vec )
114 100 <vector> swap (parse-until) ;
116 : parsed ( accum obj -- accum ) over push ;
118 : (parse-lines) ( lexer -- quot )
120 f parse-until >quotation
123 : parse-lines ( lines -- quot )
124 lexer-factory get call (parse-lines) ;
126 : parse-literal ( accum end quot -- accum )
127 [ parse-until ] dip call parsed ; inline
129 : parse-definition ( -- quot )
130 \ ; parse-until >quotation ;
132 : (:) ( -- word def ) CREATE-WORD parse-definition ;
136 : parse-base ( parsed base -- parsed )
137 scan swap base> [ bad-number ] unless* parsed ;
139 SYMBOL: bootstrap-syntax
141 : with-file-vocabs ( quot -- )
143 f in set { "syntax" } set-use
144 bootstrap-syntax get [ use get push ] when*
146 ] with-scope ; inline
148 SYMBOL: interactive-vocabs
191 } interactive-vocabs set-global
193 : with-interactive-vocabs ( quot -- )
196 interactive-vocabs get set-use
198 ] with-scope ; inline
200 SYMBOL: print-use-hook
202 print-use-hook global [ [ ] or ] change-at
204 : parse-fresh ( lines -- quot )
206 V{ } clone amended-use set
208 amended-use get empty? [ print-use-hook get call ] unless
211 : parsing-file ( file -- )
212 "quiet" get [ drop ] [ "Loading " write print flush ] if ;
214 : filter-moved ( assoc1 assoc2 -- seq )
216 drop where dup [ first ] when
218 ] assoc-filter keys ;
220 : removed-definitions ( -- assoc1 assoc2 )
221 new-definitions old-definitions
222 [ get first2 assoc-union ] bi@ ;
224 : removed-classes ( -- assoc1 assoc2 )
225 new-definitions old-definitions
228 : forget-removed-definitions ( -- )
229 removed-definitions filter-moved forget-all ;
231 : reset-removed-classes ( -- )
233 filter-moved [ class? ] filter [ forget-class ] each ;
235 : fix-class-words ( -- )
236 #! If a class word had a compound definition which was
237 #! removed, it must go back to being a symbol.
238 new-definitions get first2
239 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
241 : forget-smudged ( -- )
242 forget-removed-definitions
243 reset-removed-classes
246 : finish-parsing ( lines quot -- )
249 [ record-definitions ]
253 : parse-stream ( stream name -- quot )
256 lines dup parse-fresh
257 [ nip ] [ finish-parsing ] 2bi
260 ] with-compilation-unit ;
262 : parse-file-restarts ( file -- restarts )
263 "Load " " again" surround t 2array 1array ;
265 : parse-file ( file -- quot )
268 [ parsing-file ] keep
269 [ utf8 <file-reader> ] keep
271 ] with-compiler-errors
273 over parse-file-restarts rethrow-restarts
277 : run-file ( file -- )
278 [ parse-file call ] curry assert-depth ;
280 : ?run-file ( path -- )
281 dup exists? [ run-file ] [ drop ] if ;