2018-07-04 19:47:14 -04:00
|
|
|
! Copyright (C) 2017 Doug Coleman.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: accessors arrays assocs combinators.short-circuit
|
2018-07-04 21:19:54 -04:00
|
|
|
constructors continuations fry io io.encodings.utf8 io.files
|
2018-07-04 19:47:14 -04:00
|
|
|
io.streams.string kernel modern modern.paths modern.slices
|
2018-07-04 21:19:54 -04:00
|
|
|
multiline prettyprint sequences sequences.extras splitting
|
|
|
|
strings vocabs.loader ;
|
2018-07-04 19:47:14 -04:00
|
|
|
IN: modern.out
|
|
|
|
|
|
|
|
: token? ( obj -- ? )
|
|
|
|
{ [ slice? ] [ seq>> string? ] } 1&& ;
|
|
|
|
|
|
|
|
TUPLE: renamed slice string ;
|
|
|
|
CONSTRUCTOR: <renamed> renamed ( slice string -- obj ) ;
|
|
|
|
|
|
|
|
: trim-before-newline ( seq -- seq' )
|
2018-07-04 21:19:54 -04:00
|
|
|
dup [ CHAR: \s = not ] find
|
|
|
|
{ CHAR: \r CHAR: \n } member?
|
2018-07-04 19:47:14 -04:00
|
|
|
[ tail-slice ] [ drop ] if ;
|
|
|
|
|
|
|
|
: write-whitespace ( last obj -- )
|
|
|
|
swap
|
|
|
|
[ swap slice-between ] [ slice-before ] if*
|
2018-07-04 21:19:54 -04:00
|
|
|
trim-before-newline io:write ;
|
2018-07-04 19:47:14 -04:00
|
|
|
|
|
|
|
GENERIC: write-literal* ( last obj -- last' )
|
|
|
|
M: slice write-literal* [ write-whitespace ] [ write ] [ ] tri ;
|
|
|
|
M: array write-literal* [ write-literal* ] each ;
|
|
|
|
M: renamed write-literal* [ slice>> write-whitespace ] [ string>> write ] [ slice>> ] tri ; ! for refactoring
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DEFER: map-literals
|
|
|
|
: (map-literals) ( obj quot: ( obj -- obj' ) -- seq )
|
|
|
|
over [ array? ] any? [
|
|
|
|
[ call drop ] [ map-literals ] 2bi
|
|
|
|
] [
|
|
|
|
over array? [ map-literals ] [ call ] if
|
|
|
|
] if ; inline recursive
|
|
|
|
|
|
|
|
: map-literals ( obj quot: ( obj -- obj' ) -- seq )
|
|
|
|
'[ _ (map-literals) ] map ; inline recursive
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
! Start with no slice as ``last``
|
|
|
|
: write-literal ( obj -- ) f swap write-literal* drop ;
|
|
|
|
|
|
|
|
: write-modern-string ( seq -- string )
|
|
|
|
[ write-literal ] with-string-writer ; inline
|
|
|
|
|
|
|
|
: write-modern-path ( seq path -- )
|
|
|
|
utf8 [ write-literal nl ] with-file-writer ; inline
|
|
|
|
|
|
|
|
: write-modern-vocab ( seq vocab -- )
|
|
|
|
vocab-source-path write-modern-path ; inline
|
|
|
|
|
|
|
|
: rewrite-path ( path quot: ( obj -- obj' ) -- )
|
|
|
|
! dup print
|
|
|
|
'[ [ path>literals _ map-literals ] [ ] bi write-modern-path ]
|
|
|
|
[ drop . ] recover ; inline recursive
|
|
|
|
|
|
|
|
: rewrite-string ( string quot: ( obj -- obj' ) -- )
|
|
|
|
! dup print
|
|
|
|
[ string>literals ] dip map-literals write-modern-string ; inline recursive
|
|
|
|
|
|
|
|
: rewrite-paths ( seq quot: ( obj -- obj' ) -- ) '[ _ rewrite-path ] each ; inline recursive
|
|
|
|
|
|
|
|
: rewrite-vocab ( vocab quot: ( obj -- obj' ) -- )
|
|
|
|
[ [ vocab>literals ] dip map-literals ] 2keep drop write-modern-vocab ; inline recursive
|
|
|
|
|
|
|
|
: rewrite-string-exact ( string -- string' )
|
|
|
|
string>literals write-modern-string ;
|
|
|
|
|
|
|
|
![[
|
|
|
|
: rewrite-path-exact ( path -- )
|
|
|
|
[ path>literals ] [ ] bi write-modern-path ;
|
|
|
|
|
|
|
|
: rewrite-vocab-exact ( name -- )
|
|
|
|
vocab-source-path rewrite-path-exact ;
|
|
|
|
|
|
|
|
: rewrite-paths ( paths -- )
|
|
|
|
[ rewrite-path-exact ] each ;
|
|
|
|
]]
|
|
|
|
|
|
|
|
: strings-core-to-file ( -- )
|
2018-07-04 21:19:54 -04:00
|
|
|
core-vocabs
|
2018-07-04 19:47:14 -04:00
|
|
|
[ ".private" ?tail drop vocab-source-path utf8 file-contents ] map-zip
|
|
|
|
[ "[========[" dup matching-delimiter-string surround ] assoc-map
|
|
|
|
[
|
|
|
|
first2 [ "VOCAB: " prepend ] dip " " glue
|
|
|
|
] map
|
|
|
|
[ " " prepend ] map "\n\n" join
|
|
|
|
"<VOCAB-ROOT: factorcode-core \"https://factorcode.org/git/factor.git\" \"core/\"\n"
|
|
|
|
"\n;VOCAB-ROOT>" surround "resource:core-strings.factor" utf8 set-file-contents ;
|
|
|
|
|
|
|
|
: parsed-core-to-file ( -- )
|
2018-07-04 21:19:54 -04:00
|
|
|
core-vocabs
|
2018-07-04 19:47:14 -04:00
|
|
|
[ vocab>literals ] map-zip
|
|
|
|
[
|
|
|
|
first2 [ "<VOCAB: " prepend ] dip
|
|
|
|
>strings
|
|
|
|
! [ 3 head ] [ 3 tail* ] bi [ >strings ] bi@ { "..." } glue
|
|
|
|
";VOCAB>" 3array
|
|
|
|
] map 1array
|
|
|
|
|
|
|
|
{ "<VOCAB-ROOT:" "factorcode-core" "https://factorcode.org/git/factor.git" "core/" }
|
|
|
|
{ ";VOCAB-ROOT>" } surround "resource:core-parsed.factor" utf8 [ ... ] with-file-writer ;
|