blob: afd140997165100588bbe5099f02ebbb07c8e5a3 (
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
(.module:
[lux #*
[control
["." monad (#+ do)]
["p" parser]
["ex" exception (#+ exception:)]]
[data
["." text
format]
[collection
["." list ("#/." functor)]
["." dictionary]]]
["." macro (#+ with-gensyms)
["." code]
["s" syntax (#+ syntax:)]]
[tool
[compiler
["." phase
[synthesis (#+ Synthesis)]
["." extension
["." bundle]]]]]
[host (#+ import:)]]
[luxc
[lang
[host
["$" jvm (#+ Label Inst Method Handler Bundle)
["_t" type]
["_" inst]]]]]
["." ///
["." runtime]])
(import: java/lang/Double
(#static MIN_VALUE Double)
(#static MAX_VALUE Double))
## [Types]
(syntax: (Vector {size s.nat} elemT)
(wrap (list (` [(~+ (list.repeat size elemT))]))))
(type: #export Nullary (-> (Vector 0 Inst) Inst))
(type: #export Unary (-> (Vector 1 Inst) Inst))
(type: #export Binary (-> (Vector 2 Inst) Inst))
(type: #export Trinary (-> (Vector 3 Inst) Inst))
(type: #export Variadic (-> (List Inst) Inst))
## [Utils]
(def: $Object-Array $.Type (_t.array 1 ///.$Object))
(def: $String $.Type (_t.class "java.lang.String" (list)))
(def: $CharSequence $.Type (_t.class "java.lang.CharSequence" (list)))
(syntax: (arity: {name s.local-identifier} {arity s.nat})
(with-gensyms [g!_ g!extension g!extension-name g!phase g!inputs]
(do @
[g!inputC+ (monad.seq @ (list.repeat arity (macro.gensym "input")))
#let [arityC (code.nat arity)]]
(wrap (list (` (def: #export ((~ (code.local-identifier name)) (~ g!extension))
(-> (-> (..Vector (~ arityC) Inst) Inst) ..Handler)
(function ((~ g!_) (~ g!extension-name) (~ g!phase) (~ g!inputs))
(case (~ g!inputs)
(^ (list (~+ g!inputC+)))
(do phase.monad
[(~+ (|> g!inputC+
(list/map (function (_ g!input)
(list g!input (` ((~ g!phase) (~ g!input))))))
list.concat))]
((~' wrap) ((~ g!extension) [(~+ g!inputC+)])))
(~ g!_)
(phase.fail (ex.construct extension.incorrect-arity
[(~ g!extension-name) (~ arityC) (list.size (~ g!inputs))])))))))))))
(arity: nullary 0)
(arity: unary 1)
(arity: binary 2)
(arity: trinary 3)
(def: #export (variadic extension)
(-> Variadic Handler)
(function (_ extension-name phase inputsS)
(do phase.monad
[inputsH (monad.map @ phase inputsS)]
(wrap (extension inputsH)))))
## [Instructions]
(def: lux-intI Inst (|>> _.I2L (_.wrap #$.Long)))
(def: jvm-intI Inst (|>> (_.unwrap #$.Long) _.L2I))
(def: check-stringI Inst (_.CHECKCAST "java.lang.String"))
(def: (predicateI tester)
(-> (-> Label Inst)
Inst)
(<| _.with-label (function (_ @then))
_.with-label (function (_ @end))
(|>> (tester @then)
(_.GETSTATIC "java.lang.Boolean" "FALSE" (_t.class "java.lang.Boolean" (list)))
(_.GOTO @end)
(_.label @then)
(_.GETSTATIC "java.lang.Boolean" "TRUE" (_t.class "java.lang.Boolean" (list)))
(_.label @end)
)))
(def: unitI Inst (_.string ///.unit))
## Extensions
### Lux
(def: (lux::is [leftI rightI])
Binary
(|>> leftI
rightI
(predicateI _.IF_ACMPEQ)))
(def: (lux::try riskyI)
Unary
(|>> riskyI
(_.CHECKCAST ///.function-class)
(_.INVOKESTATIC ///.runtime-class "try"
(_t.method (list ///.$Function) (#.Some $Object-Array) (list))
#0)))
### Bits
(do-template [<name> <op>]
[(def: (<name> [maskI inputI])
Binary
(|>> inputI (_.unwrap #$.Long)
maskI (_.unwrap #$.Long)
<op> (_.wrap #$.Long)))]
[bit::and _.LAND]
[bit::or _.LOR]
[bit::xor _.LXOR]
)
(do-template [<name> <op>]
[(def: (<name> [shiftI inputI])
Binary
(|>> inputI (_.unwrap #$.Long)
shiftI jvm-intI
<op>
(_.wrap #$.Long)))]
[bit::left-shift _.LSHL]
[bit::arithmetic-right-shift _.LSHR]
[bit::logical-right-shift _.LUSHR]
)
### Numbers
(do-template [<name> <const> <type>]
[(def: (<name> _)
Nullary
(|>> <const> (_.wrap <type>)))]
[frac::smallest (_.double (Double::MIN_VALUE)) #$.Double]
[frac::min (_.double (f/* -1.0 (Double::MAX_VALUE))) #$.Double]
[frac::max (_.double (Double::MAX_VALUE)) #$.Double]
)
(do-template [<name> <type> <op>]
[(def: (<name> [subjectI paramI])
Binary
(|>> subjectI (_.unwrap <type>)
paramI (_.unwrap <type>)
<op>
(_.wrap <type>)))]
[i64::+ #$.Long _.LADD]
[i64::- #$.Long _.LSUB]
[int::* #$.Long _.LMUL]
[int::/ #$.Long _.LDIV]
[int::% #$.Long _.LREM]
[frac::+ #$.Double _.DADD]
[frac::- #$.Double _.DSUB]
[frac::* #$.Double _.DMUL]
[frac::/ #$.Double _.DDIV]
[frac::% #$.Double _.DREM]
)
(do-template [<eq> <lt> <unwrap> <cmp>]
[(do-template [<name> <reference>]
[(def: (<name> [subjectI paramI])
Binary
(|>> subjectI <unwrap>
paramI <unwrap>
<cmp>
(_.int <reference>)
(predicateI _.IF_ICMPEQ)))]
[<eq> +0]
[<lt> -1])]
[i64::= int::< (_.unwrap #$.Long) _.LCMP]
[frac::= frac::< (_.unwrap #$.Double) _.DCMPG]
)
(do-template [<name> <prepare> <transform>]
[(def: (<name> inputI)
Unary
(|>> inputI <prepare> <transform>))]
[int::frac (_.unwrap #$.Long) (<| (_.wrap #$.Double) _.L2D)]
[int::char (_.unwrap #$.Long)
((|>> _.L2I _.I2C (_.INVOKESTATIC "java.lang.Character" "toString" (_t.method (list _t.char) (#.Some $String) (list)) #0)))]
[frac::int (_.unwrap #$.Double) (<| (_.wrap #$.Long) _.D2L)]
[frac::encode (_.unwrap #$.Double)
(_.INVOKESTATIC "java.lang.Double" "toString" (_t.method (list _t.double) (#.Some $String) (list)) #0)]
[frac::decode ..check-stringI
(_.INVOKESTATIC ///.runtime-class "decode_frac" (_t.method (list $String) (#.Some $Object-Array) (list)) #0)]
)
### Text
(def: (text::size inputI)
Unary
(|>> inputI
..check-stringI
(_.INVOKEVIRTUAL "java.lang.String" "length" (_t.method (list) (#.Some _t.int) (list)) #0)
lux-intI))
(do-template [<name> <pre-subject> <pre-param> <op> <post>]
[(def: (<name> [subjectI paramI])
Binary
(|>> subjectI <pre-subject>
paramI <pre-param>
<op> <post>))]
[text::= (<|) (<|)
(_.INVOKEVIRTUAL "java.lang.Object" "equals" (_t.method (list ///.$Object) (#.Some _t.boolean) (list)) #0)
(_.wrap #$.Boolean)]
[text::< ..check-stringI ..check-stringI
(_.INVOKEVIRTUAL "java.lang.String" "compareTo" (_t.method (list $String) (#.Some _t.int) (list)) #0)
(predicateI _.IFLT)]
[text::concat ..check-stringI ..check-stringI
(_.INVOKEVIRTUAL "java.lang.String" "concat" (_t.method (list $String) (#.Some $String) (list)) #0)
(<|)]
[text::char ..check-stringI jvm-intI
(_.INVOKEVIRTUAL "java.lang.String" "charAt" (_t.method (list _t.int) (#.Some _t.char) (list)) #0)
lux-intI]
)
(do-template [<name> <pre-subject> <pre-param> <pre-extra> <op>]
[(def: (<name> [subjectI paramI extraI])
Trinary
(|>> subjectI <pre-subject>
paramI <pre-param>
extraI <pre-extra>
<op>))]
[text::clip ..check-stringI jvm-intI jvm-intI
(_.INVOKEVIRTUAL "java.lang.String" "substring" (_t.method (list _t.int _t.int) (#.Some $String) (list)) #0)]
)
(def: index-method Method (_t.method (list $String _t.int) (#.Some _t.int) (list)))
(def: (text::index [textI partI startI])
Trinary
(<| _.with-label (function (_ @not-found))
_.with-label (function (_ @end))
(|>> textI ..check-stringI
partI ..check-stringI
startI jvm-intI
(_.INVOKEVIRTUAL "java.lang.String" "indexOf" index-method #0)
_.DUP
(_.int -1)
(_.IF_ICMPEQ @not-found)
lux-intI
runtime.someI
(_.GOTO @end)
(_.label @not-found)
_.POP
runtime.noneI
(_.label @end))))
### I/O
(def: string-method Method (_t.method (list $String) #.None (list)))
(def: (io::log messageI)
Unary
(|>> (_.GETSTATIC "java.lang.System" "out" (_t.class "java.io.PrintStream" (list)))
messageI
..check-stringI
(_.INVOKEVIRTUAL "java.io.PrintStream" "println" string-method #0)
unitI))
(def: (io::error messageI)
Unary
(|>> (_.NEW "java.lang.Error")
_.DUP
messageI
..check-stringI
(_.INVOKESPECIAL "java.lang.Error" "<init>" string-method #0)
_.ATHROW))
(def: (io::exit codeI)
Unary
(|>> codeI jvm-intI
(_.INVOKESTATIC "java.lang.System" "exit" (_t.method (list _t.int) #.None (list)) #0)
_.NULL))
(def: (io::current-time _)
Nullary
(|>> (_.INVOKESTATIC "java.lang.System" "currentTimeMillis" (_t.method (list) (#.Some _t.long) (list)) #0)
(_.wrap #$.Long)))
## Bundles
(def: bundle::lux
Bundle
(|> (: Bundle bundle.empty)
(bundle.install "is" (binary lux::is))
(bundle.install "try" (unary lux::try))))
(def: bundle::i64
Bundle
(<| (bundle.prefix "i64")
(|> (: Bundle bundle.empty)
(bundle.install "and" (binary bit::and))
(bundle.install "or" (binary bit::or))
(bundle.install "xor" (binary bit::xor))
(bundle.install "left-shift" (binary bit::left-shift))
(bundle.install "logical-right-shift" (binary bit::logical-right-shift))
(bundle.install "arithmetic-right-shift" (binary bit::arithmetic-right-shift))
(bundle.install "+" (binary i64::+))
(bundle.install "-" (binary i64::-))
(bundle.install "=" (binary i64::=)))))
(def: bundle::int
Bundle
(<| (bundle.prefix "int")
(|> (: Bundle bundle.empty)
(bundle.install "*" (binary int::*))
(bundle.install "/" (binary int::/))
(bundle.install "%" (binary int::%))
(bundle.install "<" (binary int::<))
(bundle.install "frac" (unary int::frac))
(bundle.install "char" (unary int::char)))))
(def: bundle::frac
Bundle
(<| (bundle.prefix "frac")
(|> (: Bundle bundle.empty)
(bundle.install "+" (binary frac::+))
(bundle.install "-" (binary frac::-))
(bundle.install "*" (binary frac::*))
(bundle.install "/" (binary frac::/))
(bundle.install "%" (binary frac::%))
(bundle.install "=" (binary frac::=))
(bundle.install "<" (binary frac::<))
(bundle.install "smallest" (nullary frac::smallest))
(bundle.install "min" (nullary frac::min))
(bundle.install "max" (nullary frac::max))
(bundle.install "int" (unary frac::int))
(bundle.install "encode" (unary frac::encode))
(bundle.install "decode" (unary frac::decode)))))
(def: bundle::text
Bundle
(<| (bundle.prefix "text")
(|> (: Bundle bundle.empty)
(bundle.install "=" (binary text::=))
(bundle.install "<" (binary text::<))
(bundle.install "concat" (binary text::concat))
(bundle.install "index" (trinary text::index))
(bundle.install "size" (unary text::size))
(bundle.install "char" (binary text::char))
(bundle.install "clip" (trinary text::clip)))))
(def: bundle::io
Bundle
(<| (bundle.prefix "io")
(|> (: Bundle bundle.empty)
(bundle.install "log" (unary io::log))
(bundle.install "error" (unary io::error))
(bundle.install "exit" (unary io::exit))
(bundle.install "current-time" (nullary io::current-time)))))
(def: #export bundle
Bundle
(<| (bundle.prefix "lux")
(|> bundle::lux
(dictionary.merge bundle::i64)
(dictionary.merge bundle::int)
(dictionary.merge bundle::frac)
(dictionary.merge bundle::text)
(dictionary.merge bundle::io))))
|