blob: f6896343c76fa0c9116753ee8b4fd961186b5351 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
(.module:
[lux (#- nat int rev)
[control
["." equivalence (#+ Equivalence)]]
[data
bit
number
name
["." text (#+ Equivalence<Text>) ("text/." Monoid<Text>)]
[collection
["." list ("list/." Functor<List> Fold<List>)]]]])
## [Types]
## (type: (Code' w)
## (#.Bit Bit)
## (#.Nat Nat)
## (#.Int Int)
## (#.Frac Frac)
## (#.Text Text)
## (#.Identifier Name)
## (#.Tag Name)
## (#.Form (List (w (Code' w))))
## (#.Tuple (List (w (Code' w))))
## (#.Record (List [(w (Code' w)) (w (Code' w))])))
## (type: Code
## (Ann Cursor (Code' (Ann Cursor))))
## [Utils]
(def: _cursor Cursor ["" +0 +0])
## [Functions]
(do-template [<name> <type> <tag>]
[(def: #export (<name> x)
(-> <type> Code)
[_cursor (<tag> x)])]
[bit Bit #.Bit]
[nat Nat #.Nat]
[int Int #.Int]
[rev Rev #.Rev]
[frac Frac #.Frac]
[text Text #.Text]
[identifier Name #.Identifier]
[tag Name #.Tag]
[form (List Code) #.Form]
[tuple (List Code) #.Tuple]
[record (List [Code Code]) #.Record]
)
(do-template [<name> <tag> <doc>]
[(def: #export (<name> name)
{#.doc <doc>}
(-> Text Code)
[_cursor (<tag> ["" name])])]
[local-identifier #.Identifier "Produces a local identifier (an identifier with no module prefix)."]
[local-tag #.Tag "Produces a local tag (a tag with no module prefix)."])
## [Structures]
(structure: #export _ (Equivalence Code)
(def: (= x y)
(case [x y]
(^template [<tag> <eq>]
[[_ (<tag> x')] [_ (<tag> y')]]
(:: <eq> = x' y'))
([#.Bit Equivalence<Bit>]
[#.Nat Equivalence<Nat>]
[#.Int Equivalence<Int>]
[#.Rev Equivalence<Rev>]
[#.Frac Equivalence<Frac>]
[#.Text Equivalence<Text>]
[#.Identifier Equivalence<Name>]
[#.Tag Equivalence<Name>])
(^template [<tag>]
[[_ (<tag> xs')] [_ (<tag> ys')]]
(:: (list.Equivalence<List> =) = xs' ys'))
([#.Form]
[#.Tuple])
[[_ (#.Record xs')] [_ (#.Record ys')]]
(:: (list.Equivalence<List> (equivalence.product = =))
= xs' ys')
_
#0)))
## [Values]
(def: #export (to-text ast)
(-> Code Text)
(case ast
(^template [<tag> <struct>]
[_ (<tag> value)]
(:: <struct> encode value))
([#.Bit Codec<Text,Bit>]
[#.Nat Codec<Text,Nat>]
[#.Int Codec<Text,Int>]
[#.Rev Codec<Text,Rev>]
[#.Frac Codec<Text,Frac>]
[#.Identifier Codec<Text,Name>])
[_ (#.Text value)]
(text.encode value)
[_ (#.Tag name)]
(text/compose "#" (:: Codec<Text,Name> encode name))
(^template [<tag> <open> <close>]
[_ (<tag> members)]
($_ text/compose <open> (|> members (list/map to-text) (list.interpose " ") (text.join-with "")) <close>))
([#.Form "(" ")"]
[#.Tuple "[" "]"])
[_ (#.Record pairs)]
($_ text/compose
"{"
(|> pairs
(list/map (function (_ [left right])
($_ text/compose (to-text left) " " (to-text right))))
(list.interpose " ")
(text.join-with ""))
"}")
))
(def: #export (replace original substitute ast)
{#.doc "Replaces all code that looks like the 'original' with the 'substitute'."}
(-> Code Code Code Code)
(if (:: Equivalence<Code> = original ast)
substitute
(case ast
(^template [<tag>]
[cursor (<tag> parts)]
[cursor (<tag> (list/map (replace original substitute) parts))])
([#.Form]
[#.Tuple])
[cursor (#.Record parts)]
[cursor (#.Record (list/map (function (_ [left right])
[(replace original substitute left)
(replace original substitute right)])
parts))]
_
ast)))
|