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

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

    ## true loop
    _
    (do {! ///////phase.monad}
      [$scope (\ ! map _.var (/////generation.gensym "loop_scope"))
       initsO+ (monad.map ! (expression archive) initsS+)
       bodyO (/////generation.with_anchor $scope
               (expression archive bodyS))]
      (wrap (_.block
             ($_ _.then
                 (_.set! $scope
                         (_.function (|> initsS+
                                         list.size
                                         list.indices
                                         (list\map (|>> (n.+ offset) //case.register)))
                           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))))