blob: ac65155e4f0f91c5b7c86454e2659b504ac949fc (
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
146
|
(;module:
lux
(lux (control eq)
(data bool
number
[char]
[text #+ Eq<Text> "Text/" Monoid<Text>]
ident
(coll [list #* "" Functor<List> Fold<List>])
)))
## [Types]
## (type: (AST' w)
## (#;Bool Bool)
## (#;Nat Nat)
## (#;Int Int)
## (#;Real Real)
## (#;Char Char)
## (#;Text Text)
## (#;Symbol Text Text)
## (#;Tag Text Text)
## (#;Form (List (w (AST' w))))
## (#;Tuple (List (w (AST' w))))
## (#;Record (List [(w (AST' w)) (w (AST' w))])))
## (type: AST
## (Meta Cursor (AST' (Meta Cursor))))
## [Utils]
(def: _cursor Cursor ["" +0 +0])
## [Functions]
(do-template [<name> <type> <tag>]
[(def: #export (<name> x)
(-> <type> AST)
[_cursor (<tag> x)])]
[bool Bool #;Bool]
[nat Nat #;Nat]
[int Int #;Int]
[deg Deg #;Deg]
[real Real #;Real]
[char Char #;Char]
[text Text #;Text]
[symbol Ident #;Symbol]
[tag Ident #;Tag]
[form (List AST) #;Form]
[tuple (List AST) #;Tuple]
[record (List [AST AST]) #;Record]
)
(do-template [<name> <tag> <doc>]
[(def: #export (<name> name)
{#;doc <doc>}
(-> Text AST)
[_cursor (<tag> ["" name])])]
[local-symbol #;Symbol "Produces a local symbol (a symbol with no module prefix)."]
[local-tag #;Tag "Produces a local tag (a tag with no module prefix)."])
## [Structures]
(struct: #export _ (Eq AST)
(def: (= x y)
(case [x y]
(^template [<tag> <eq>]
[[_ (<tag> x')] [_ (<tag> y')]]
(:: <eq> = x' y'))
([#;Bool Eq<Bool>]
[#;Nat Eq<Nat>]
[#;Int Eq<Int>]
[#;Deg Eq<Deg>]
[#;Real Eq<Real>]
[#;Char char;Eq<Char>]
[#;Text Eq<Text>]
[#;Symbol Eq<Ident>]
[#;Tag Eq<Ident>])
(^template [<tag>]
[[_ (<tag> xs')] [_ (<tag> ys')]]
(and (:: Eq<Nat> = (size xs') (size ys'))
(fold (function [[x' y'] old]
(and old (= x' y')))
true
(zip2 xs' ys'))))
([#;Form]
[#;Tuple])
[[_ (#;Record xs')] [_ (#;Record ys')]]
(and (:: Eq<Nat> = (size xs') (size ys'))
(fold (function [[[xl' xr'] [yl' yr']] old]
(and old (= xl' yl') (= xr' yr')))
true
(zip2 xs' ys')))
_
false)))
## [Values]
(def: #export (to-text ast)
(-> AST Text)
(case ast
(^template [<tag> <struct>]
[_ (<tag> value)]
(:: <struct> encode value))
([#;Bool Codec<Text,Bool>]
[#;Nat Codec<Text,Nat>]
[#;Int Codec<Text,Int>]
[#;Deg Codec<Text,Deg>]
[#;Real Codec<Text,Real>]
[#;Char char;Codec<Text,Char>]
[#;Text text;Codec<Text,Text>]
[#;Symbol Codec<Text,Ident>])
[_ (#;Tag ident)]
(Text/append "#" (:: Codec<Text,Ident> encode ident))
(^template [<tag> <open> <close>]
[_ (<tag> members)]
($_ Text/append <open> (|> members (map to-text) (interpose " ") (text;join-with "")) <close>))
([#;Form "(" ")"]
[#;Tuple "[" "]"])
[_ (#;Record pairs)]
($_ Text/append "{" (|> pairs (map (function [[left right]] ($_ Text/append (to-text left) " " (to-text right)))) (interpose " ") (text;join-with "")) "}")
))
(def: #export (replace original substitute ast)
{#;doc "Replaces all ASTs that look like 'original' with the 'substitute' in the given AST."}
(-> AST AST AST AST)
(if (:: Eq<AST> = original ast)
substitute
(case ast
(^template [<tag>]
[cursor (<tag> parts)]
[cursor (<tag> (map (replace original substitute) parts))])
([#;Form]
[#;Tuple])
[cursor (#;Record parts)]
[cursor (#;Record (map (function [[left right]]
[(replace original substitute left)
(replace original substitute right)])
parts))]
_
ast)))
|