aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/lang/compiler/translation/scheme/loop.jvm.lux
blob: 6f305336e39b65d07dc012bc8f867920c90a90d1 (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
(.module:
  [lux #- loop]
  (lux (control [monad #+ do])
       (data [product]
             [text]
             text/format
             (coll [list "list/" Functor<List>]))
       [macro])
  [////]
  (//// [name]
        (host ["_" scheme #+ Computation Var])
        [compiler "operation/" Monad<Operation>]
        [synthesis #+ Synthesis])
  [///]
  [//runtime #+ Operation Translator]
  [//reference])

(def: @loop (_.var "loop"))

(def: #export (loop translate offset initsS+ bodyS)
  (-> Translator Nat (List Synthesis) Synthesis
      (Operation Computation))
  (do compiler.Monad<Operation>
    [initsO+ (monad.map @ translate initsS+)
     bodyO (///.with-anchor @loop
             (translate bodyS))]
    (wrap (_.letrec (list [@loop (_.lambda [(|> initsS+
                                           list.enumerate
                                           (list/map (|>> product.left (n/+ offset) //reference.local')))
                                       #.None]
                                      bodyO)])
                    (_.apply/* @loop initsO+)))))

(def: #export (recur translate argsS+)
  (-> Translator (List Synthesis) (Operation Computation))
  (do compiler.Monad<Operation>
    [@loop ///.anchor
     argsO+ (monad.map @ translate argsS+)]
    (wrap (_.apply/* @loop argsO+))))