aboutsummaryrefslogtreecommitdiff
path: root/source/lux.lux
blob: 7cafb29774ea9d3cdda062c537d14284454ecfb1 (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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
## Base interfaces & classes
(jvm-interface Function
               (: apply (-> [java.lang.Object] java.lang.Object)))

## Base functions & macros
(def' let'
  (lambda' _ tokens
      (lambda' _ state
          (case' tokens
                 (#Cons [lhs (#Cons [rhs (#Cons [body #Nil])])])
                 [(#Cons [(#Form (#Cons [(#Ident "case'") (#Cons [rhs (#Cons [lhs (#Cons [body #Nil])])])]))
                          #Nil])
                  state])
          )))
(declare-macro let')

(def' lambda
  (lambda' _ tokens
      (lambda' _ state
          (let' output (case' tokens
                              (#Cons [(#Tuple (#Cons [arg args'])) (#Cons [body #Nil])])
                              (#Form (#Cons [(#Ident "lambda'")
                                             (#Cons [(#Ident "")
                                                     (#Cons [arg
                                                             (#Cons [(case' args'
                                                                            #Nil
                                                                            body

                                                                            _
                                                                            (#Form (#Cons [(#Ident "lux;lambda")
                                                                                           (#Cons [(#Tuple args')
                                                                                                   (#Cons [body #Nil])])])))
                                                                     #Nil])])])]))

                              (#Cons [(#Ident self) (#Cons [(#Tuple (#Cons [arg args'])) (#Cons [body #Nil])])])
                              (#Form (#Cons [(#Ident "lambda'")
                                             (#Cons [(#Ident self)
                                                     (#Cons [arg
                                                             (#Cons [(case' args'
                                                                            #Nil
                                                                            body

                                                                            _
                                                                            (#Form (#Cons [(#Ident "lux;lambda")
                                                                                           (#Cons [(#Tuple args')
                                                                                                   (#Cons [body #Nil])])])))
                                                                     #Nil])])])])))
                [(#Cons [output #Nil]) state])
          )))
(declare-macro lambda)

(def' def
  (lambda [tokens state]
    (let' output (case' tokens
                        (#Cons [(#Ident name) (#Cons [body #Nil])])
                        (#Form (#Cons [(#Ident "def'") tokens]))

                        (#Cons [(#Form (#Cons [(#Ident name) args]))
                                (#Cons [body #Nil])])
                        (#Form (#Cons [(#Ident "def'")
                                       (#Cons [(#Ident name)
                                               (#Cons [(#Form (#Cons [(#Ident "lux;lambda")
                                                                      (#Cons [(#Ident name)
                                                                              (#Cons [(#Tuple args)
                                                                                      (#Cons [body #Nil])])])]))
                                                       #Nil])])])))
          [(#Cons [output #Nil]) state])))
(declare-macro def)

(def (defmacro tokens state)
  (let' [fn-name fn-def] (case' tokens
                                (#Cons [(#Form (#Cons [(#Ident ?name) (#Cons [(#Ident ?tokens) (#Cons [(#Ident ?state) #Nil])])]))
                                        (#Cons [body #Nil])])
                                [?name
                                 (#Form (#Cons [(#Ident "lux;def")
                                                (#Cons [(#Form (#Cons [(#Ident ?name)
                                                                       (#Cons [(#Ident ?tokens)
                                                                               (#Cons [(#Ident ?state)
                                                                                       #Nil])])]))
                                                        (#Cons [body
                                                                #Nil])])]))])
        (let' declaration (#Form (#Cons [(#Ident "declare-macro") (#Cons [(#Ident fn-name) #Nil])]))
              [(#Cons [fn-def (#Cons [declaration #Nil])]) state])))
(declare-macro defmacro)

(defmacro (comment tokens state)
  [#Nil state])

(def (int+ x y)
  (jvm-iadd x y))

(def (id x)
  x)

(def (print x)
  (jvm-invokevirtual java.io.PrintStream "print" [java.lang.Object]
                     (jvm-getstatic java.lang.System "out") [x]))

(def (println x)
  (jvm-invokevirtual java.io.PrintStream "println" [java.lang.Object]
                     (jvm-getstatic java.lang.System "out") [x]))

#(


(def (fold f init xs)
  (case' xs
         #Nil
         init
         
         (#Cons [x xs'])
         (fold f (f init x) xs')))

(def (reverse list)
  (fold (lambda [tail head]
          (#Cons [head tail]))
        #Nil
        list))

(def (list xs state)
  (let' xs' (reverse xs)
        (let' output (fold (lambda [tail head]
                             (#Form (#Cons [(#Tag "Cons")
                                            (#Cons [(#Tuple (#Cons [head
                                                                    (#Cons [(#Form (#Cons [(#Tag "Cons")
                                                                                           (#Cons [(#Tuple (#Cons [tail
                                                                                                                   (#Cons [(#Tag "Nil") #Nil])])) #Nil])])) #Nil])])) #Nil])])))
                           (#Tag "Nil")
                           xs')
              [(#Cons [output #Nil]) state])))
(declare-macro list)

(def (list+ xs state)
  (case' (reverse xs)
         #Nil
         [#Nil state]

         (#Cons [last init'])
         (let' output (fold (lambda [tail head]
                              ## (#Form (list (#Tag "Cons") (#Tuple (list head tail))))
                              (#Form (#Cons [(#Tag "Cons") (#Cons [(#Tuple (#Cons [head (#Cons [tail #Nil])])) #Nil])])))
                            last
                            init')
               [(#Cons [output #Nil]) state])))
(declare-macro list+)

(def (as-pairs xs)
  (case' xs
         (#Cons [x (#Cons [y xs'])])
         (#Cons [[x y] (as-pairs xs')])

         _
         #Nil))

(def (let tokens state)
  (case' tokens
         (#Cons [(#Tuple bindings) (#Cons [body #Nil])])
         (let' output (fold (lambda [body binding]
                              (case' binding
                                     [label value]
                                     (#Form (list (#Ident "let'") label value body))))
                            body
                            (reverse (as-pairs bindings)))
               [(list output) state])))
(declare-macro let)

(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 (filter p xs)
  (case' xs
         #Nil
         #Nil

         (#Cons [x xs*])
         (if (p x)
           (filter p xs*)
           (#Cons [x (filter p 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)))
    ))

(def (' tokens state)
  [(map untemplate tokens) state])
(declare-macro ')

## TODO: Full pattern-matching
## TODO: Type-related macros
## TODO: (Im|Ex)ports-related macros
## TODO: Macro-related macros

(def (return val)
  (lambda [state]
    [state val]))

(def (bind f v)
  (lambda [state]
    (case (v state)
      [state' x]
      ((f x) state'))))

(def (if tokens)
  (case' tokens
         (#Cons [test (#Cons [then (#Cons [else #Nil])])])
         (return (list (' (case' (~ test)
                                 true  (~ then)
                                 false (~ else)))))))
(declare-macro if)

(def (apply-template env template)
  (case template
    (#Ident ident)
    (if-let [subst (get ident env)]
            subst
            template)

    (#Tuple elems)
    (#Tuple (map (apply-template env) elems))

    (#Form elems)
    (#Form (map (apply-template env) elems))

    (#Record members)
    (#Record (map (lambda [kv]
                    (case kv
                      [slot value]
                      [(apply-template env slot) (apply-template env value)]))
                  members))
    
    _
    template))

(def (do-template tokens)
  (case tokens
    (list+ bindings template data)
    (let [bindings-list (tuple->list bindings)
          data-lists (map tuple->list data)]
      (return (map (lambda [env] (apply-template env template))
                   (map (zip 2 bindings) data-lists))))))
(declare-macro do-template)

(def gen-ident
  (lambda [state]
    [(update@ #gen-seed inc state)
     (#Ident ($ text-++ "__" (show-int (get@ #gen-seed state)) "__"))]))

(do-template [<name> <member>]
             (def (<name> pair)
               (case' pair
                      [f s]
                      <member>))

             [first  f]
             [second s])

(def (loop tokens)
  (case' tokens
         (#Cons [bindings (#Cons [body #Nil])])
         (let [pairs (as-pairs bindings)]
           (return (list (' ((lambda (~ (#Ident "recur")) (~ (#Tuple (map first pairs)))
                               (~ body))
                             (~@ (map second pairs)))))))))
(declare-macro loop)

(def (case tokens)
  (case' tokens
         (#Cons value branches)
         (loop [kind #Pattern
                pieces branches
                new-pieces (list)]
           (case' pieces
                  #Nil
                  (return (list (' (case' (~ value) (~@ new-pieces)))))

                  (#Cons piece pieces')
                  (let [[kind' expanded more-pieces] (case' kind
                                                            #Body
                                                            [#Pattern (list piece) #Nil]

                                                            #Pattern
                                                            (do [expansion (macro-expand piece)]
                                                              (case' expansion
                                                                     #Nil
                                                                     [#Pattern #Nil #Nil]

                                                                     (#Cons exp #Nil)
                                                                     [#Body (list exp) #Nil]

                                                                     (#Cons exp exps)
                                                                     [#Body (list exp) exps]))
                                                            )]
                    (recur kind' (++ expanded new-pieces) (++ more-pieces pieces))))
           )))
(declare-macro case)

(def (do tokens state)
  (case tokens
    (list (#Tuple bindings) body)
    (let [output (fold (lambda [inner binding]
                         (case binding
                           [lhs rhs]
                           (' (bind (lambda [(~ lhs)] (~ body))
                                    (~ rhs)))))
                       body
                       (reverse (as-pairs bindings)))]
      [(list output) state])))
(declare-macro do)

(def (export tokens)
  (return (map (lambda [t]
                 (' (export' (~ t))))
               tokens)))
(declare-macro export)

## (import "lux")
## (module-alias "lux" "l")
## (def-alias "lux;map" "map")

## (def (require tokens)
##   (case tokens
##     ...))

## (require lux #as l #refer [map])

(def (type tokens)
  (case tokens
    (#Tuple elems)
    (return (list (' (#Tuple (~ (map untemplate elems))))))
    
    (#Record fields)
    (return (list (' (#Record (~ (map (lambda [kv]
                                        (case kv
                                          [(#Tag tag) val]
                                          [tag (untemplate val)]))
                                      fields))))))

    (#Form (list+ (#Ident "|") options))
    (do [options' (map% (lambda [opt]
                          (case opt
                            (#Tag tag)
                            [tag (#Tuple (list))]
                            
                            (#Form (list (#Tag tag) value))
                            [tag value]
                            
                            _
                            (fail "")))
                        options)]
      (#Variant options'))
    ))
(declare-macro type)

## (type (| #Nil
##          (#Cons [a (List a)])))

## (type [Int Bool Text])

## (type {#id Int #alive? Bool #name Text})

(def (All tokens)
  (let [[name args body] (case tokens
                           (list (#Tuple args) body)
                           ["" args body]

                           (list (#Ident name) (#Tuple args) body)
                           [name args body])]
    (return (list (' (#All (~ name) [(~@ (map (lambda [arg]
                                                (case arg
                                                  (#Ident arg')
                                                  (#Text arg')))
                                              args))]
                           (~ body)))))))
(declare-macro All)

(def (Exists tokens)
  (case tokens
    (list (#Ident name) body)
    (return (list (' (#Exists (~ name) (~ body)))))))
(declare-macro Exists)

(def (deftype tokens)
  (case tokens
    (list (#Ident name) definition)
    (return (list (' (def (~ (#Ident name))
                       (type (~ definition))))))
    
    (list (#Form (list+ (#Ident name) args)) definition)
    (let [name' (#Ident name)]
      (return (list (' (def (~ name')
                         (All (~ name') [(~@ args)]
                              (type (~ definition))))))))
    ))
(declare-macro deftype)

(def (and tokens)
  (let [as-if (case tokens
                #Nil
                (' true)

                (#Cons init tests)
                (fold (lambda [prev next]
                        (' (if (~ prev) (~ next) false)))
                      init
                      tokens)
                )]
    (return (list as-if))))
(declare-macro and)

(def (or tokens)
  (let [as-if (case tokens
                #Nil
                (' false)

                (#Cons init tests)
                (fold (lambda [prev next]
                        (' (if (~ prev) true (~ next))))
                      init
                      tokens)
                )]
    (return (list as-if))))
(declare-macro or)

(def (not x)
  (case x
    true  false
    false true))

(def (get@ tokens)
  (let [output (case tokens
                 (list (#Tag tag) record)
                 (' (get@' (~ (#Tag tag)) (~ record)))

                 (list (#Tag tag))
                 (' (lambda [record] (get@' (~ (#Tag tag)) record))))]
    (return (list output))))
(declare-macro get@)

(def (set@ tokens)
  (let [output (case tokens
                 (list (#Tag tag) value record)
                 (' (set@' (~ (#Tag tag)) (~ value) (~ record)))

                 (list (#Tag tag) value)
                 (' (lambda [record] (set@' (~ (#Tag tag)) (~ value) record)))

                 (list (#Tag tag))
                 (' (lambda [value record] (set@' (~ (#Tag tag)) value record))))]
    (return (list output))))
(declare-macro set@)

(def (update@ tokens)
  (let [output (case tokens
                 (list tag func record)
                 (` (let [_record_ (~ record)]
                      (set@ (~ tag) _record_ ((~ func) (get@ (~ tag) _record_)))))
                 
                 (list (#Tag tag) func)
                 (' (lambda [record]
                      (` (set@ (~ tag) record ((~ func) (get@ (~ tag) record))))))

                 (list (#Tag tag))
                 (' (lambda [func record]
                      (set@ (~ tag) record (func (get@ (~ tag) record))))))]
    (return (list output))))
(declare-macro update@)

(def (. f g)
  (lambda [x] (f (g x))))

(def (|> tokens)
  (case tokens
    (list+ init apps)
    (return (list (fold (lambda [acc app]
                          (case app
                            (#Form parts)
                            (#Form (++ parts (list acc)))

                            _
                            (` (~ app) (~ acc))))
                        init
                        apps)))))

(def ($ tokens)
  (case tokens
    (list+ op init args)
    (return (list (fold (lambda [acc elem]
                          (` (~ op) (~ acc) (~ elem)))
                        init
                        args)))))

(def ($keys tokens)
  (case tokens
    (list (#Tuple fields))
    (let [record (#Record (map (lambda [slot]
                                 (case slot
                                   (#Tag name)
                                   [(#Tag name) (#Ident name)]))
                               fields))]
      (return (list record)))))

(def ($or tokens)
  (case tokens
    (list (#Tuple patterns) body)
    (return (flat-map (lambda [pattern] (list pattern body))
                      patterns))))

(def (-> tokens)
  (case (reverse tokens)
    (#Cons [f-return f-args])
    (fold (lambda [f-return f-arg]
            (#Lambda [f-arg f-return]))
          f-return f-args)))

(def (defsyntax tokens)
  ...)

(def (defsig tokens)
  ...)

(def (defstruct tokens)
  ...)

(def (with tokens)
  ...)

## (deftype (List a)
##   (| #Nil
##      (#Cons [a (List a)])))

(def (complement f)
  (lambda [x] (not (f x))))

(def (cond tokens)
  (let [else (last tokens)
        branches (as-pairs (init tokens))]
    (return (list (fold (lambda [else branch]
                          (case branch
                            [test then]
                            (` (if (~ test) (~ then) (~ else)))))
                        else
                        branches)))))
(declare-macro cond)

(def (constant x)
  (lambda [_] x))

(def (repeat n x)
  (if (> n 0)
    (list+ x (repeat (dec n) x))
    #Nil))

(def (size xs)
  (case xs
    #Nil 0
    (#Cons [_ xs']) (int+ 1 (size xs'))))

(def (last xs)
  (case xs
    #Nil          #None
    (list x)      (#Some x)
    (list+ _ xs') (last xs')))

(def (init xs)
  (case xs
    #Nil            #None
    (list _)        (#Some #Nil)
    (#Cons [x xs']) (case (init xs')
                      (#Some xs'')
                      (#Cons [x xs''])

                      _
                      #None)))

(do-template [<name> <offset>]
             (def <name> (int+ <offset>))

             [inc  1]
             [dec -1])

(def (interleave xs ys)
  (case [xs ys]
    [(#Cons [x xs']) (#Cons [y ys'])]
    (list+ x y (interleave xs' ys'))

    _
    #Nil))

(def (interpose sep xs)
  (case xs
    (#Cons [x #Nil])
    xs

    (#Cons [x xs'])
    (list+ x sep (interpose sep xs'))
    
    _
    xs))

(def (flatten xss)
  (fold ++ (list) xs))

(def (flat-map f xs)
  (flatten (map f xs)))

(do-template [<name> <cmp>]
             (def (<name> x y)
               (if (<cmp> x y)
                 x
                 y))

             [max >]
             [min <])

(do-template [<name> <cmp>]
             (def (<name> n) (<cmp> n 0))

             [neg? <]
             [pos? >=])

(def (even? n)
  (int= 0 (int% n 0)))

(def (odd? n)
  (not (even? n)))

(do-template [<name> <done> <step>]
             (def (<name> n xs)
               (if (> n 0)
                 (case xs
                   #Nil          #Nil
                   (list+ x xs') <step>)
                 <done>))

             [take #Nil (list+ x (take (dec n) xs'))]
             [drop xs   (drop (dec n) xs')])

(do-template [<name> <done> <step>]
             (def (<name> f xs)
               (case xs
                 #Nil          #Nil
                 (list+ x xs') (if (f x) <step> #Nil)))

             [take-while #Nil (list+ x (take-while f xs'))]
             [drop-while xs   (drop-while f xs')])

(def (empty? xs)
  (case xs
    #Nil true
    _    false))

(do-template [<name> <op>]
             (def (<name> p xs)
               (case xs
                 #Nil true
                 (#Cons [x xs']) (<op> (p x) (<name> p xs'))))

             [every? and]
             [any?   or])

(def (range from to)
  (if (< from to)
    (list+ from (range (inc from) to))
    #Nil))
)#