aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/ruby/loop.lux
blob: 4cff08fc632fd0085db13f53ef20aee8060d19f2 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except Scope symbol)
   [abstract
    ["[0]" monad (.only do)]]
   [data
    ["[0]" product]
    ["[0]" text (.only)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix)]
     ["[0]" set]]]
   [math
    [number
     ["n" nat]]]
   [meta
    [compiler
     [target
      ["_" ruby (.only Expression LVar Statement)]]]]]]
 ["[0]" //
  [runtime (.only Operation Phase Translator Phase! Translator!)]
  ["[1][0]" when]
  ["/[1]" //
   ["[1][0]" reference]
   ["/[1]" //
    [synthesis
     ["[0]" when]]
    ["/[1]" //
     ["[0]" phase]
     ["[0]" synthesis (.only Scope)]
     ["[1][0]" translation]
     ["//[1]" ///
      [reference
       ["[1][0]" variable (.only Register)]]]]]]])

(def (setup offset bindings body)
  (-> Register (List Expression) Statement Statement)
  (let [variables (|> bindings
                      list.enumeration
                      (list#each (|>> product.left (n.+ offset) //when.register)))]
    (all _.then
         (_.set variables (_.multi bindings))
         body)))

(def symbol
  (_.symbol "lux_continue"))

(def .public with_scope
  (-> Statement Statement)
  (_.while (_.bool true)))

(def .public (scope! statement expression archive [start initsS+ bodyS])
  (Translator! (Scope synthesis.Term))
  (when initsS+
    ... function/false/non-independent loop
    {.#End}
    (statement expression archive bodyS)

    ... true loop
    _
    (do [! phase.monad]
      [initsO+ (monad.each ! (expression archive) initsS+)
       body! (/////translation.with_anchor start
               (statement expression archive bodyS))]
      (in (<| (..setup start initsO+)
              ..with_scope
              body!)))))

(def .public (scope statement expression archive [start initsS+ bodyS])
  (-> Phase! (Translator (Scope synthesis.Term)))
  (when initsS+
    ... function/false/non-independent loop
    {.#End}
    (expression archive bodyS)

    ... true loop
    _
    (do [! phase.monad]
      [body! (scope! statement expression archive [start initsS+ bodyS])]
      (in (|> body!
              [(list)] (_.lambda {.#None})
              (_.apply_lambda (list)))))))

(def .public (again! statement expression archive argsS+)
  (Translator! (List synthesis.Term))
  (do [! phase.monad]
    [offset /////translation.anchor
     @temp (//when.symbol "lux_again_values")
     argsO+ (monad.each ! (expression archive) argsS+)
     .let [re_binds (|> argsO+
                        list.enumeration
                        (list#each (function (_ [idx _])
                                     (_.item (_.int (.int idx)) @temp))))]]
    (in (all _.then
             (_.set (list @temp) (_.array argsO+))
             (..setup offset re_binds
                      _.next)))))