blob: 15bcecdbc9a6827c12825fbdca852aa544de5957 (
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
## (use "./another" as another)
(jvm/definterface Function
(: apply (-> [java.lang.Object] java.lang.Object)))
(jvm/defclass Tuple0 java.lang.Object
[])
(jvm/defclass Tuple1 java.lang.Object
[[java.lang.Object _1]])
(jvm/defclass Tuple2 java.lang.Object
[[java.lang.Object _1] [java.lang.Object _2]])
(jvm/defclass Tuple3 java.lang.Object
[[java.lang.Object _1] [java.lang.Object _2]
[java.lang.Object _3]])
(jvm/defclass Tuple4 java.lang.Object
[[java.lang.Object _1] [java.lang.Object _2]
[java.lang.Object _3] [java.lang.Object _4]])
(jvm/defclass Tuple5 java.lang.Object
[[java.lang.Object _1] [java.lang.Object _2]
[java.lang.Object _3] [java.lang.Object _4]
[java.lang.Object _5]])
(jvm/defclass Tuple6 java.lang.Object
[[java.lang.Object _1] [java.lang.Object _2]
[java.lang.Object _3] [java.lang.Object _4]
[java.lang.Object _5] [java.lang.Object _6]])
(jvm/defclass Tuple7 java.lang.Object
[[java.lang.Object _1] [java.lang.Object _2]
[java.lang.Object _3] [java.lang.Object _4]
[java.lang.Object _5] [java.lang.Object _6]
[java.lang.Object _7]])
(jvm/defclass Tuple8 java.lang.Object
[[java.lang.Object _1] [java.lang.Object _2]
[java.lang.Object _3] [java.lang.Object _4]
[java.lang.Object _5] [java.lang.Object _6]
[java.lang.Object _7] [java.lang.Object _8]])
(jvm/defclass Variant java.lang.Object
[[java.lang.String tag]])
(jvm/defclass Variant0 test2.Variant
[])
(jvm/defclass Variant1 test2.Variant
[[java.lang.Object _1]])
(jvm/defclass Variant2 test2.Variant
[[java.lang.Object _1] [java.lang.Object _2]])
(jvm/defclass Variant3 test2.Variant
[[java.lang.Object _1] [java.lang.Object _2]
[java.lang.Object _3]])
(jvm/defclass Variant4 test2.Variant
[[java.lang.Object _1] [java.lang.Object _2]
[java.lang.Object _3] [java.lang.Object _4]])
(jvm/defclass Variant5 test2.Variant
[[java.lang.Object _1] [java.lang.Object _2]
[java.lang.Object _3] [java.lang.Object _4]
[java.lang.Object _5]])
(jvm/defclass Variant6 test2.Variant
[[java.lang.Object _1] [java.lang.Object _2]
[java.lang.Object _3] [java.lang.Object _4]
[java.lang.Object _5] [java.lang.Object _6]])
(jvm/defclass Variant7 test2.Variant
[[java.lang.Object _1] [java.lang.Object _2]
[java.lang.Object _3] [java.lang.Object _4]
[java.lang.Object _5] [java.lang.Object _6]
[java.lang.Object _7]])
(jvm/defclass Variant8 test2.Variant
[[java.lang.Object _1] [java.lang.Object _2]
[java.lang.Object _3] [java.lang.Object _4]
[java.lang.Object _5] [java.lang.Object _6]
[java.lang.Object _7] [java.lang.Object _8]])
(def (print x)
(jvm/invokevirtual java.io.PrintStream "print" [Object]
(jvm/getstatic System out) [x]))
(def (println x)
(jvm/invokevirtual java.io.PrintStream "println" [Object]
(jvm/getstatic System out) [x]))
(defmacro (list xs)
(case xs
#Nil
(#Tag "Nil")
(#Cons x xs*)
(#Form (#Cons (#Tag "Cons") (#Cons x (#Cons (list xs*) #Nil))))))
(def (++ xs ys)
(case xs
#Nil
ys
(#Cons x xs*)
(#Cons x (++ xs* ys))))
(def (map f xs)
(case xs
#Nil
#Nil
(#Cons x xs*)
(#Cons (f x) (map f xs*))))
(def (untemplate-list untemplate tokens)
(case tokens
#Nil
(#Tag "Nil")
(#Cons token tokens')
(#Form (list (#Tag "Cons") (untemplate token) (untemplate-list untemplate tokens')))))
(def (untemplate token)
(case token
(#Bool elem)
(#Form (list (#Tag "Bool") (#Bool elem)))
(#Int elem)
(#Form (list (#Tag "Int") (#Int elem)))
(#Real elem)
(#Form (list (#Tag "Real") (#Real elem)))
(#Char elem)
(#Form (list (#Tag "Char") (#Char elem)))
(#Text elem)
(#Form (list (#Tag "Text") (#Text elem)))
(#Tag elem)
(#Form (list (#Tag "Tag") (#Text elem)))
(#Ident elem)
(#Form (list (#Tag "Ident") (#Text elem)))
(#Form (#Cons (#Ident "~") (#Cons unquoted #Nil)))
unquoted
(#Tuple elems)
(#Form (list (#Tag "Tuple") (untemplate-list untemplate elems)))
(#Form elems)
(#Form (list (#Tag "Form") (untemplate-list untemplate elems)))
))
(defmacro (' form)
(case form
(#Cons token #Nil)
(untemplate token)))
## Utils
(def (fail* message)
(#Failure message))
(def (return* state value)
(#Ok [state value]))
(def (fail message)
(lambda [state]
(#Failure message)))
(def (return value)
(lambda [state]
(#Ok [state value])))
(def (bind m-value step)
(lambda [state]
(let inputs (m-value state)
(case inputs
(#Ok [?state ?datum])
(step ?datum ?state)
_
inputs))))
(def (+ x y)
(jvm/i+ x y))
(def inc (+ 1))
(def (id x)
x)
(def (fold f init values)
(case values
#Nil
init
(#Cons x xs)
(fold f (f init x) xs)))
(def length (fold (lambda [l x] (inc l)) 0))
(def (rem dividend divisor)
(jvm/irem dividend divisor))
(def (= x y)
(jvm/invokevirtual Object "equals" [Object]
x [y]))
(def (as-pairs list)
(case list
(#Cons x (#Cons y list*))
(#Cons [x y] (as-pairs list*))
_
#Nil))
## Ideally, this is what I want...
## (exec [yolo lol
## #let [foo (bar 1 2 3)]]
## (meme yolo foo))
(defmacro (exec tokens)
(case tokens
(#Cons (#Tuple steps) (#Cons return #Nil))
(if (= 0 (rem (length steps) 2))
(fold (lambda [inner pair]
(case pair
[label computation]
(' (bind (~ computation)
(lambda [(~ label)] (~ inner))))))
return
(as-pairs steps))
(#Text "Oh no!"))))
(def (try-m monad)
(lambda [state]
(case (monad state)
(#Ok [?state ?datum])
(return* ?state (#Just ?datum))
(#Failure _)
(return* state #Nothing))))
(def (repeat-m monad)
(lambda [state]
(case (monad state)
(#Ok [?state ?head])
(case ((repeat-m monad) ?state)
(#Ok [?state* ?tail])
(return* ?state* (#Cons ?head ?tail)))
(#Failure ?message)
(return* state #Nil))))
(def (try-all-m monads)
(lambda [state]
(case monads
#Nil
(fail* "No alternative worked!")
(#Cons monad monads')
(let output (monad state)
(case output
(#Ok _)
output
(#Failure _)
(case monads'
#Nil
output
(#Cons _ _)
((try-all-m monads') state))
))
)))
(def (map-m f inputs)
(case inputs
#Nil
(return #Nil)
(#Cons input inputs')
(exec [output (f input)
outputs (map-m f inputs')]
(return (#Cons output outputs)))))
(def (fold-m f init inputs)
(case inputs
#Nil (return init)
(#Cons x inputs') (exec [init* (f init x)]
(fold-m f init* inputs'))))
(def (apply-m monad call-state)
(lambda [state]
(let output (monad call-state)
(case output
(#Ok [?state ?datum])
(#Ok [state ?datum])
_
output))))
(def (assert test message)
(if test
(return [])
(fail message)))
(def (pass %value)
(lambda [state]
%value))
(def get-state
(lambda [state]
(return* state state)))
(def (show x)
(jvm/invokevirtual Object "toString" []
x []))
(def (concat t1 t2)
(jvm/invokevirtual String "concat" [String]
t1 [t2]))
(def (normalize-char char)
(case char
#"*" "_ASTER_"
#"+" "_PLUS_"
#"-" "_DASH_"
#"/" "_SLASH_"
#"_" "_UNDERS_"
#"%" "_PERCENT_"
#"$" "_DOLLAR_"
#"'" "_QUOTE_"
#"`" "_BQUOTE_"
#"@" "_AT_"
#"^" "_CARET_"
#"&" "_AMPERS_"
#"=" "_EQ_"
#"!" "_BANG_"
#"?" "_QM_"
#":" "_COLON_"
#";" "_SCOLON_"
#"." "_PERIOD_"
#"," "_COMMA_"
#"<" "_LT_"
#">" "_GT_"
#"~" "_TILDE_"
##;;#"\" "_BSLASH_"
_ (show char)
))
(def (range from to)
(if (= from to)
#Nil
(#Cons from (range (inc from) to))))
(def (text->list text)
(let length (jvm/invokevirtual String "length" []
text [])
(map (lambda [idx]
(jvm/invokevirtual String "charAt" [int]
text [idx]))
(range 0 length))))
(def (normalize-ident ident)
(fold concat "" (map normalize-char (text->list ident))))
(def (fresh-class-loader path)
(let file (jvm/new java.io.File [String] [path])
(let url (jvm/invokevirtual java.io.File "toURL" []
file [])
(let urls (jvm/new-array java.net.URL 1)
(do (jvm/aastore urls 0 url)
(jvm/new java.net.URLClassLoader [(Array java.net.URL)] [urls]))))
))
(def (cons tail head)
(#Cons head tail))
(def (reverse list)
(fold cons #Nil list))
(def (enumerate list)
(case (fold (lambda [state x]
(case state
[idx list']
[(inc idx) (#Cons [idx x] list')]))
[0 #Nil]
list)
[_ list']
(reverse list')))
(def (print-enum enum)
(case enum
#Nil
(println "")
(#Cons [idx x] enum')
(do (print "[") (print idx) (print ":") (print x) (print "]") (print " ")
(print-enum enum'))))
(def get-state
(lambda [state]
(#Ok [state state])))
(def monadic-dup
(exec [foo get-state
bar get-state
baz (return 1000)]
(return (+ (+ foo bar) baz))))
(def (run-state monad state)
(monad state))
(def list-map #Nil)
(def (put key val map)
(case map
#Nil
(#Cons [key val] map)
(#Cons [?key ?val] map')
(if (= key ?key)
(#Cons [?key val] map')
(#Cons [?key ?val] (put key val map')))))
(def (get key map)
(case map
#Nil
#None
(#Cons [?key ?val] map')
(if (= key ?key)
(#Some ?val)
(get key map'))))
(def (show-kv kv)
(case kv
[?key ?val]
(fold concat "" (list "#" ?key " " (show ?val)))))
(def (interpose elem list)
(case list
(#Cons x (#Cons y list'))
(#Cons x (#Cons elem (#Cons y list')))
_
list))
(def (print-map list-map)
(do (print "{")
(print (fold concat "" (interpose " " (map show-kv list-map))))
(println "}")))
(def (show-list xs)
(case xs
#Nil
"#Nil"
(#Cons x xs')
(fold concat "" (list "(#Cons " (show x) " " (show-list xs') ")"))))
## Program
(def (main args)
(case (' ((~ "Oh yeah...")))
(#Form (#Cons text #Nil))
(do (println text)
(println (+ 10 20))
(println (inc 10))
(println (jvm/i- 10 20))
(println (jvm/i* 10 20))
(println (jvm/i/ 10 2))
(let xs (list 1 2 3 4 5 6 7 8 9)
(do (println (fold + 0 xs))
(println (length xs))))
(println (rem 21 6))
(println (rem 21 7))
(println (= false false))
(println (= false true))
(println (= true false))
(println (= true true))
(case (run-state monadic-dup 123)
(#Ok [_ ?value])
(println ?value)
(#Failure ?message)
(println ?message))
(print-enum (enumerate (list #"a" #"b" #"c" #"d" #"e")))
(print-map (put "Nyan" "cat" (put "Yolo" "lol" list-map)))
(let char #"*"
(do (print (show char)) (print " -> ")
(println (normalize-char char))))
(println (show-list (range 0 10)))
(println (normalize-ident "text->list"))
(println (fresh-class-loader "./"))
)
))
|