]> gitweb.factorcode.org Git - factor.git/blob - extra/modern/out/out.factor
Switch to https urls
[factor.git] / extra / modern / out / out.factor
1 ! Copyright (C) 2017 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators.short-circuit
4 constructors continuations io io.encodings.utf8 io.files
5 io.streams.string kernel modern modern.paths modern.slices
6 multiline prettyprint sequences splitting strings vocabs.loader ;
7 IN: modern.out
8
9 : token? ( obj -- ? )
10     { [ slice? ] [ seq>> string? ] } 1&& ;
11
12 TUPLE: renamed slice string ;
13 CONSTRUCTOR: <renamed> renamed ( slice string -- obj ) ;
14
15 : trim-before-newline ( seq -- seq' )
16     dup [ CHAR: \s = not ] find
17     { CHAR: \r CHAR: \n } member?
18     [ tail-slice ] [ drop ] if ;
19
20 : write-whitespace ( last obj -- )
21     swap
22     [ swap slice-between ] [ slice-before ] if*
23     trim-before-newline io:write ;
24
25 GENERIC: write-literal* ( last obj -- last' )
26 M: slice write-literal* [ write-whitespace ] [ write ] [ ] tri ;
27 M: array write-literal* [ write-literal* ] each ;
28 M: renamed write-literal* [ slice>> write-whitespace ] [ string>> write ] [ slice>> ] tri ; ! for refactoring
29
30
31
32 DEFER: map-literals
33 : (map-literals) ( obj quot: ( obj -- obj' ) -- seq )
34     over [ array? ] any? [
35         [ call drop ] [ map-literals ] 2bi
36     ] [
37         over array? [ map-literals ] [ call ] if
38     ] if ; inline recursive
39
40 : map-literals ( obj quot: ( obj -- obj' ) -- seq )
41     '[ _ (map-literals) ] map ; inline recursive
42
43
44
45 ! Start with no slice as ``last``
46 : write-literal ( obj -- ) f swap write-literal* drop ;
47
48 : write-modern-string ( seq -- string )
49     [ write-literal ] with-string-writer ; inline
50
51 : write-modern-path ( seq path -- )
52     utf8 [ write-literal nl ] with-file-writer ; inline
53
54 : write-modern-vocab ( seq vocab -- )
55     vocab-source-path write-modern-path ; inline
56
57 : rewrite-path ( path quot: ( obj -- obj' ) -- )
58     ! dup print
59     '[ [ path>literals _ map-literals ] [ ] bi write-modern-path ]
60     [ drop . ] recover ; inline recursive
61
62 : rewrite-string ( string quot: ( obj -- obj' ) -- )
63     ! dup print
64     [ string>literals ] dip map-literals write-modern-string ; inline recursive
65
66 : rewrite-paths ( seq quot: ( obj -- obj' ) -- ) '[ _ rewrite-path ] each ; inline recursive
67
68 : rewrite-vocab ( vocab quot: ( obj -- obj' ) -- )
69     [ [ vocab>literals ] dip map-literals ] keepd write-modern-vocab ; inline recursive
70
71 : rewrite-string-exact ( string -- string' )
72     string>literals write-modern-string ;
73
74 ![[
75 : rewrite-path-exact ( path -- )
76     [ path>literals ] [ ] bi write-modern-path ;
77
78 : rewrite-vocab-exact ( name -- )
79     vocab-source-path rewrite-path-exact ;
80
81 : rewrite-paths ( paths -- )
82     [ rewrite-path-exact ] each ;
83 ]]
84
85 : strings-core-to-file ( -- )
86     core-vocabs
87     [ ".private" ?tail drop vocab-source-path utf8 file-contents ] zip-with
88     [ "[========[" dup matching-delimiter-string surround ] assoc-map
89     [
90         first2 [ "VOCAB: " prepend ] dip " " glue
91     ] map
92     [ "    " prepend ] map "\n\n" join
93     "<VOCAB-ROOT: factorcode-core \"https://factorcode.org/git/factor.git\" \"core/\"\n"
94     "\n;VOCAB-ROOT>" surround "resource:core-strings.factor" utf8 set-file-contents ;
95
96 : parsed-core-to-file ( -- )
97     core-vocabs
98     [ vocab>literals ] zip-with
99     [
100         first2 [ "<VOCAB: " prepend ] dip
101         >strings
102         ! [ 3 head ] [ 3 tail* ] bi [ >strings ] bi@ { "..." } glue
103         ";VOCAB>" 3array
104     ] map 1array
105
106     { "<VOCAB-ROOT:" "factorcode-core" "https://factorcode.org/git/factor.git" "core/" }
107     { ";VOCAB-ROOT>" } surround "resource:core-parsed.factor" utf8 [ ... ] with-file-writer ;