aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/translation/js/loop.lux
blob: 8d0cefe4e3bc31e1dc61711442cd3741300050fe (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
(.module:
  [lux (#- Scope)
   [control
    ["." monad (#+ do)]]
   [data
    ["." product]
    ["." text
     format]
    [collection
     ["." list ("#/." functor)]]]
   [host
    ["_" js (#+ Computation Var)]]]
  [//
   [runtime (#+ Operation Phase)]
   ["." reference]
   ["//." case]
   ["/." //
    ["//." //
     [synthesis (#+ Scope Synthesis)]]]])

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

(def: #export (scope translate [start initsS+ bodyS])
  (-> Phase (Scope Synthesis) (Operation Computation))
  (do ////.monad
    [initsO+ (monad.map @ translate initsS+)
     bodyO (///.with-anchor @scope
             (translate bodyS))
     #let [closure (_.function @scope
                     (|> initsS+
                         list.enumerate
                         (list/map (|>> product.left (n/+ start) //case.register)))
                     (_.return bodyO))]]
    (wrap (_.apply/* closure initsO+))))

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