Paste: Scoped Quotations
Author: | Leo Mehraban |
Mode: | factor |
Date: | Fri, 11 Jul 2025 19:38:11 |
Plain Text |
TUPLE: scoped words quot ;
M: scoped call [ words>> ] [ quot>> ] bi with-words ; inline
CONSULT: sequence-protocol scoped quot>> ;
M: scoped like drop [ ] like H{ } clone swap scoped boa ;
DEFER: [:
: :] ( -- ) ; delimiter
M: scoped pprint-delims drop \ [: \ :] ;
M: scoped >pprint-sequence ;
M: scoped pprint* pprint-object ;
INITIALIZED-SYMBOL: scope [ V{ } clone ]
: safe-parse-datum ( string -- word/number )
[ search ] [ [ dec> ] [ <uninterned-word> ] ?unless ]
?unless ;
: safe-?scan-datum ( -- word/number/f )
?scan-token [ safe-parse-datum ] ?call ;
: safe-(parse-until) ( accum end -- accum )
[
safe-?scan-datum {
{ [ 2dup eq? ] [ 2drop f ] }
{ [ dup not ] [ drop throw-unexpected-eof ] }
{ [ dup delimiter? ] [ unexpected ] }
{ [ dup parsing-word? ] [ nip execute-parsing t ] }
[ pick push drop t ]
} cond
] curry loop ;
: safe-parse-until ( end -- vec ) [ 100 <vector> ] dip safe-(parse-until) ;
: flatten-assoc-seq ( seq -- assoc )
H{ } clone [ assoc-union ] reduce ;
: search-scoped-words ( word -- word/f )
[ name>> scope get-global flatten-assoc-seq at ] transmute ;
: check-scoped ( scoped -- scoped )
[ [ dup word? [ search-scoped-words ] when ] deep-map ] change-quot ;
: parse-scoped ( -- scoped )
scope get-global H{ } clone [ suffix! drop ] keep
\ :] safe-parse-until >quotation scoped boa
check-scoped scope get-global pop drop ;
SYNTAX: [: parse-scoped suffix! ;
: delete-word ( word -- )
[
[ name>> ] [ vocabulary>> ] bi
vocab-words-assoc delete-at
] keep [ name>> ] keep associate unuse-words ;
ERROR: localword-used-outside-scope ;
SYNTAX: localword
last-word dup name>> scope get-global dup length 0 > [
[
[
<uninterned-word> over
[ definition ] [ stack-effect ] [ props>> ] tri
[ [ define-declared ] keepdd ] dip >>props
] keep
] dip last set-at delete-word
] [ localword-used-outside-scope ] if ;
New Annotation