aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/scheme/loop.lux
blob: 23718bfc5c597e0daa2e5f7fab196fc3f1b8316d (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
(.module:
  [library
   [lux (#- Scope)
    [abstract
     ["." monad (#+ do)]]
    [data
     ["." product]
     ["." text
      ["%" format (#+ format)]]
     [collection
      ["." list ("#\." functor fold)]
      ["." set (#+ Set)]]]
    [math
     [number
      ["n" nat]]]
    [target
     ["_" scheme]]]]
  ["." // #_
   [runtime (#+ Operation Phase Generator)]
   ["#." case]
   ["/#" // #_
    ["#." reference]
    ["/#" // #_
     [synthesis
      ["." case]]
     ["/#" // #_
      ["."synthesis (#+ Scope Synthesis)]
      ["#." generation]
      ["//#" /// #_
       ["#." phase]
       [meta
        [archive (#+ Archive)]]
       [reference
        [variable (#+ Register)]]]]]]])

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

(def: #export (scope expression archive [start initsS+ bodyS])
  (Generator (Scope Synthesis))
  (case initsS+
    ## function/false/non-independent loop
    #.Nil
    (expression archive bodyS)

    ## true loop
    _
    (do {! ///////phase.monad}
      [initsO+ (monad.map ! (expression archive) initsS+)
       bodyO (/////generation.with_anchor @scope
               (expression archive bodyS))]
      (wrap (_.letrec (list [@scope (_.lambda [(|> initsS+
                                                   list.enumeration
                                                   (list\map (|>> product.left (n.+ start) //case.register)))
                                               #.None]
                                              bodyO)])
                      (_.apply/* initsO+ @scope))))))

(def: #export (recur expression archive argsS+)
  (Generator (List Synthesis))
  (do {! ///////phase.monad}
    [@scope /////generation.anchor
     argsO+ (monad.map ! (expression archive) argsS+)]
    (wrap (_.apply/* argsO+ @scope))))