aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/generation/php/loop.lux
blob: 3ec2d2d403cc5a6a0d09623889858c4b9763ea55 (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
(.module:
  [lux (#- Scope)
   [abstract
    ["." monad (#+ do)]]
   [data
    ["." product]
    [number
     ["n" nat]]
    [text
     ["%" format (#+ format)]]
    [collection
     ["." list ("#@." functor)]]]
   [target
    ["_" php (#+ Expression)]]]
  ["." // #_
   [runtime (#+ Operation Phase)]
   ["#." case]
   ["#/" //
    ["#/" //
     [//
      [synthesis (#+ Scope Synthesis)]]]]])

(def: #export (scope generate [start initsS+ bodyS])
  (-> Phase (Scope Synthesis) (Operation (Expression Any)))
  (do ////.monad
    [@loop (:: @ map (|>> %.nat (format "loop")) ///.next)
     #let [@loopG (_.global @loop)
           @loopL (_.var @loop)]
     initsO+ (monad.map @ generate initsS+)
     bodyO (///.with-anchor @loopL
             (generate bodyS))
     _ (///.save! true ["" @loop]
                  ($_ _.then
                      (<| _.;
                          (_.set @loopL)
                          (_.closure (list (_.reference @loopL))
                                     (|> initsS+
                                         list.enumerate
                                         (list@map (|>> product.left (n.+ start) //case.register [#0])))
                                     (_.return bodyO)))
                      (_.; (_.set @loopG @loopL))))]
    (wrap (_.apply/* initsO+ @loopG))))

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