blob: be46169ddb0439448d8883cd0d217c8df67a59a4 (
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
(.module:
[lux (#- Location Code int if cond function or and not let)
[control
[pipe (#+ case> cond> new>)]]
[data
["." text
["%" format (#+ format)]]
[collection
["." list ("#\." functor fold)]]]
[macro
["." template]]
[math
[number
["i" int]
["f" frac]]]
[type
abstract]])
(def: input_separator ", ")
(def: statement_suffix ";")
(def: nest
(-> Text Text)
(|>> (format text.new_line)
(text.replace_all text.new_line (format text.new_line text.tab))))
(abstract: #export (Code brand)
Text
(def: #export manual
(-> Text Code)
(|>> :abstraction))
(def: #export code
(-> (Code Any) Text)
(|>> :representation))
(template [<type> <super>+]
[(with_expansions [<brand> (template.identifier [<type> "'"])]
(abstract: (<brand> brand) Any)
(`` (type: #export <type> (|> Any <brand> (~~ (template.splice <super>+))))))]
[Expression [Code]]
[Computation [Expression' Code]]
[Location [Computation' Expression' Code]]
[Statement [Code]]
)
(template [<type> <super>+]
[(with_expansions [<brand> (template.identifier [<type> "'"])]
(abstract: #export <brand> Any)
(`` (type: #export <type> (|> <brand> (~~ (template.splice <super>+))))))]
[Literal [Computation' Expression' Code]]
[Var [Location' Computation' Expression' Code]]
[Access [Location' Computation' Expression' Code]]
)
(def: #export nil
Literal
(:abstraction "nil"))
(def: #export bool
(-> Bit Literal)
(|>> (case> #0 "false"
#1 "true")
:abstraction))
(def: #export (int value)
(-> Int Literal)
(:abstraction (.if (i.< +0 value)
(%.int value)
(%.nat (.nat value)))))
(def: #export float
(-> Frac Literal)
(|>> (cond> [(f.= f.positive_infinity)]
[(new> "(1.0/0.0)" [])]
[(f.= f.negative_infinity)]
[(new> "(-1.0/0.0)" [])]
[(f.= f.not_a_number)]
[(new> "(0.0/0.0)" [])]
## else
[%.frac])
:abstraction))
(def: sanitize
(-> Text Text)
(`` (|>> (~~ (template [<find> <replace>]
[(text.replace_all <find> <replace>)]
["\" "\\"]
[text.tab "\t"]
[text.vertical_tab "\v"]
[text.null "\0"]
[text.back_space "\b"]
[text.form_feed "\f"]
[text.new_line "\n"]
[text.carriage_return "\r"]
[text.double_quote (format "\" text.double_quote)]
))
)))
(def: #export string
(-> Text Literal)
(|>> ..sanitize (text.enclose' text.double_quote) :abstraction))
(def: #export array
(-> (List Expression) Literal)
(|>> (list\map ..code)
(text.join_with ..input_separator)
(text.enclose ["{" "}"])
:abstraction))
(def: #export table
(-> (List [Text Expression]) Literal)
(|>> (list\map (.function (_ [key value])
(format key " = " (:representation value))))
(text.join_with ..input_separator)
(text.enclose ["{" "}"])
:abstraction))
(def: #export (nth idx array)
(-> Expression Expression Access)
(:abstraction (format (:representation array) "[" (:representation idx) "]")))
(def: #export (the field table)
(-> Text Expression Computation)
(:abstraction (format (:representation table) "." field)))
(def: #export length
(-> Expression Computation)
(|>> :representation
(text.enclose ["#(" ")"])
:abstraction))
(def: #export (apply/* args func)
(-> (List Expression) Expression Computation)
(|> args
(list\map ..code)
(text.join_with ..input_separator)
(text.enclose ["(" ")"])
(format (:representation func))
:abstraction))
(def: #export (do method table args)
(-> Text Expression (List Expression) Computation)
(|> args
(list\map ..code)
(text.join_with ..input_separator)
(text.enclose ["(" ")"])
(format (:representation table) ":" method)
:abstraction))
(template [<op> <name>]
[(def: #export (<name> parameter subject)
(-> Expression Expression Expression)
(:abstraction (format "("
(:representation subject)
" " <op> " "
(:representation parameter)
")")))]
["==" =]
["<" <]
["<=" <=]
[">" >]
[">=" >=]
["+" +]
["-" -]
["*" *]
["/" /]
["//" //]
["%" %]
[".." concat]
["or" or]
["and" and]
["|" bit_or]
["&" bit_and]
["~" bit_xor]
["<<" bit_shl]
[">>" bit_shr]
)
(def: #export (not subject)
(-> Expression Expression)
(:abstraction (format "(not " (:representation subject) ")")))
(def: #export var
(-> Text Var)
(|>> :abstraction))
(def: #export statement
(-> Expression Statement)
(|>> :representation (text.suffix ..statement_suffix) :abstraction))
(def: #export (then pre! post!)
(-> Statement Statement Statement)
(:abstraction
(format (:representation pre!)
text.new_line
(:representation post!))))
(def: locations
(-> (List Location) Text)
(|>> (list\map ..code)
(text.join_with ..input_separator)))
(def: #export (local vars)
(-> (List Var) Statement)
(:abstraction (format "local " (..locations vars) ..statement_suffix)))
(def: #export (set vars value)
(-> (List Location) Expression Statement)
(:abstraction (format (..locations vars) " = " (:representation value) ..statement_suffix)))
(def: #export (let vars value)
(-> (List Var) Expression Statement)
($_ ..then
(local vars)
(set vars value)))
(def: #export (if test then! else!)
(-> Expression Statement Statement Statement)
(:abstraction (format "if " (:representation test)
text.new_line "then" (..nest (:representation then!))
text.new_line "else" (..nest (:representation else!))
text.new_line "end" ..statement_suffix)))
(def: #export (when test then!)
(-> Expression Statement Statement)
(:abstraction (format "if " (:representation test)
text.new_line "then" (..nest (:representation then!))
text.new_line "end" ..statement_suffix)))
(def: #export (while test body!)
(-> Expression Statement Statement)
(:abstraction
(format "while " (:representation test) " do"
(..nest (:representation body!))
text.new_line "end" ..statement_suffix)))
(def: #export (for_in vars source body!)
(-> (List Var) Expression Statement Statement)
(:abstraction
(format "for " (|> vars
(list\map ..code)
(text.join_with ..input_separator))
" in " (:representation source) " do"
(..nest (:representation body!))
text.new_line "end" ..statement_suffix)))
(def: #export (for_step var from to step body!)
(-> Var Expression Expression Expression Statement
Statement)
(:abstraction
(format "for " (:representation var)
" = " (:representation from)
..input_separator (:representation to)
..input_separator (:representation step) " do"
(..nest (:representation body!))
text.new_line "end" ..statement_suffix)))
(def: #export (return value)
(-> Expression Statement)
(:abstraction (format "return " (:representation value) ..statement_suffix)))
(def: #export (closure args body!)
(-> (List Var) Statement Expression)
(|> (format "function " (|> args
..locations
(text.enclose ["(" ")"]))
(..nest (:representation body!))
text.new_line "end")
(text.enclose ["(" ")"])
:abstraction))
(def: #export (function name args body!)
(-> Var (List Var) Statement Statement)
(:abstraction
(format "function " (:representation name)
(|> args
..locations
(text.enclose ["(" ")"]))
(..nest (:representation body!))
text.new_line "end" ..statement_suffix)))
(def: #export break
Statement
(|> "break"
(text.suffix ..statement_suffix)
:abstraction))
)
(def: #export (cond clauses else!)
(-> (List [Expression Statement]) Statement Statement)
(list\fold (.function (_ [test then!] next!)
(..if test then! next!))
else!
(list.reverse clauses)))
|