aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/r/loop.jvm.lux
blob: c64973d8f113d33b8c55956dfbf5bb08b28dfdcc (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
(.module:
  lux
  (lux (control [monad #+ do])
       (data [text]
             text/format
             (coll [list "list/" Functor<List>]))
       [macro])
  (luxc [lang]
        (lang ["ls" synthesis]
              (host [r #+ Expression @@])))
  [//]
  (// [".T" reference]))

(def: #export (translate-loop translate offset initsS+ bodyS)
  (-> (-> ls.Synthesis (Meta Expression)) Nat (List ls.Synthesis) ls.Synthesis
      (Meta Expression))
  (do macro.Monad<Meta>
    [loop-name (|> (macro.gensym "loop")
                   (:: @ map (|>> %code lang.normalize-name)))
     initsO+ (monad.map @ translate initsS+)
     bodyO (//.with-anchor [loop-name offset]
             (translate bodyS))
     #let [$loop-name (r.var loop-name)
           @loop-name (@@ $loop-name)]
     _ (//.save (r.set! $loop-name
                        (r.function (|> (list.n/range +0 (dec (list.size initsS+)))
                                        (list/map (|>> (n/+ offset) referenceT.variable)))
                          bodyO)))]
    (wrap (r.apply initsO+ @loop-name))))

(def: #export (translate-recur translate argsS+)
  (-> (-> ls.Synthesis (Meta Expression)) (List ls.Synthesis)
      (Meta Expression))
  (do macro.Monad<Meta>
    [[loop-name offset] //.anchor
     argsO+ (monad.map @ translate argsS+)]
    (wrap (r.apply argsO+ (r.global loop-name)))))