aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/common_lisp/loop.lux
blob: bae37c8358927cbbd83b7a5ff74788e121bbd2eb (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
66
67
68
69
70
(.module:
  [library
   [lux {"-" [Scope]}
    [abstract
     ["[0]" monad {"+" [do]}]]
    [data
     ["[0]" product]
     [text
      ["%" format {"+" [format]}]]
     [collection
      ["[0]" list ("[1]\[0]" functor)]]]
    [math
     [number
      ["n" nat]]]
    [target
     ["_" common_lisp {"+" [Expression]}]]]]
  ["[0]" // "_"
   [runtime {"+" [Operation Phase Generator]}]
   ["[1][0]" case]
   ["/[1]" // "_"
    ["[1][0]" reference]
    ["/[1]" // "_"
     [synthesis
      ["[0]" case]]
     ["/[1]" // "_"
      ["[0]"synthesis {"+" [Scope Synthesis]}]
      ["[1][0]" generation]
      ["//[1]" /// "_"
       ["[1][0]" phase]
       [meta
        [archive {"+" [Archive]}]]
       [reference
        [variable {"+" [Register]}]]]]]]])

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

    ... true loop
    _
    (do [! ///////phase.monad]
      [@scope (\ ! each (|>> %.nat (format "loop_scope") _.tag) /////generation.next)
       @output (\ ! each (|>> %.nat (format "loop_output") _.var) /////generation.next)
       initsG+ (monad.each ! (expression archive) initsS+)
       bodyG (/////generation.with_anchor [@scope start]
               (expression archive bodyS))]
      (in (_.let (|> initsG+
                     list.enumeration
                     (list\each (function (_ [idx init])
                                  [(|> idx (n.+ start) //case.register)
                                   init]))
                     (list& [@output _.nil]))
            (list (_.tagbody (list @scope
                                   (_.setq @output bodyG)))
                  @output))))))

(def: .public (recur expression archive argsS+)
  (Generator (List Synthesis))
  (do [! ///////phase.monad]
    [[tag offset] /////generation.anchor
     argsO+ (monad.each ! (expression archive) argsS+)
     .let [bindings (|> argsO+
                        list.enumeration
                        (list\each (|>> product.left (n.+ offset) //case.register))
                        _.args)]]
    (in (_.progn (list (_.multiple_value_setq bindings (_.values/* argsO+))
                       (_.go tag))))))