aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/js/when.lux
blob: 75754f0fcc98976cb208808a38bebdfe13aaadb8 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
... 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 when exec let if)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["[0]" maybe]]
   [data
    ["[0]" text]
    [collection
     ["[0]" list (.use "[1]#[0]" functor mix)]]]
   [math
    [number
     ["n" nat]]]
   [meta
    [macro
     ["^" pattern]]
    [compiler
     [target
      ["_" js (.only Expression Computation Var Statement)]]]]]]
 ["[0]" //
  ["[1][0]" runtime (.only Operation Phase Phase! Translator Translator!)]
  ["[1][0]" reference]
  ["[1][0]" primitive]
  ["/[1]" //
   ["[1][0]" reference]
   ["/[1]" //
    ["[1][0]" synthesis
     ["[1]/[0]" when]]
    ["/[1]" //
     ["[0]" phase (.use "[1]#[0]" monad)]
     ["[0]" synthesis (.only Path)
      [access
       ["[0]" member (.only Member)]]]
     ["//[1]" ///
      [reference
       [variable (.only Register)]]
      [meta
       [archive (.only Archive)]]]]]]])

(def .public register
  (-> Register Var)
  (|>> (///reference.local //reference.system) as_expected))

(def .public (exec next archive it)
  (Translator (synthesis.Exec synthesis.Term))
  (do [! phase.monad]
    [.let [[tail after] (synthesis.flat_exec (the synthesis.#after it))]
     all_before (monad.each ! (next archive)
                            (list.partial (the synthesis.#before it) tail))
     after (next archive after)]
    (in (|> (_.array (list (_.array all_before)
                           after))
            (_.at (_.int +1))))))

(def .public (exec! statement expression archive [this that])
  (Translator! (synthesis.Exec synthesis.Term))
  (do phase.monad
    [this (expression archive this)
     that (statement expression archive that)]
    (in (all _.then
             (_.statement this)
             that))))

(def .public (let next archive it)
  (Translator (synthesis.Let synthesis.Term))
  (do [! phase.monad]
    [.let [[tail body] (synthesis.flat_let (the synthesis.#expression it))
           context (the synthesis.#context it)]
     bindings (monad.each ! (function (_ [binding value])
                              (phase#each (_.define (..register binding))
                                          (next archive value)))
                          (list.partial context
                                        tail))
     body (next archive body)]
    ... TODO: Find some way to do 'let' without paying the price of the closure.
    (in (_.apply (<| (_.closure (list))
                     (list#mix _.then
                               (_.return body)
                               (list.reversed bindings)))
                 (list)))))

(def .public (let! statement expression archive [[register valueS] bodyS])
  (Translator! (synthesis.Let synthesis.Term))
  (do phase.monad
    [valueO (expression archive valueS)
     bodyO (statement expression archive bodyS)]
    (in (all _.then
             (_.define (..register register) valueO)
             bodyO))))

(def .public (if expression archive [testS thenS elseS])
  (Translator [synthesis.Term synthesis.Term synthesis.Term])
  (do phase.monad
    [testO (expression archive testS)
     thenO (expression archive thenS)
     elseO (expression archive elseS)]
    (in (_.? testO thenO elseO))))

(def .public (if! statement expression archive [testS thenS elseS])
  (Translator! [synthesis.Term synthesis.Term synthesis.Term])
  (do phase.monad
    [testO (expression archive testS)
     thenO (statement expression archive thenS)
     elseO (statement expression archive elseS)]
    (in (_.if testO
          thenO
          elseO))))

(def .public (get expression archive [pathP valueS])
  (Translator [(List Member) synthesis.Term])
  (do phase.monad
    [valueO (expression archive valueS)]
    (in (list#mix (function (_ side source)
                    (.let [method (.if (the member.#right? side)
                                    (//runtime.tuple//right (_.i32 (.int (the member.#lefts side))))
                                    (//runtime.tuple//left (_.i32 (.int (the member.#lefts side)))))]
                      (method source)))
                  valueO
                  pathP))))

(def @savepoint (_.var "lux_pm_cursor_savepoint"))
(def @cursor (_.var "lux_pm_cursor"))
(def @temp (_.var "lux_pm_temp"))

(def (push_cursor! value)
  (-> Expression Statement)
  (_.statement (|> @cursor (_.do "push" (list value)))))

(def peek_and_pop_cursor
  Expression
  (|> @cursor (_.do "pop" (list))))

(def pop_cursor!
  Statement
  (_.statement ..peek_and_pop_cursor))

(def length
  (|>> (_.the "length")))

(def last_index
  (|>> ..length (_.- (_.i32 +1))))

(def peek_cursor
  Expression
  (|> @cursor (_.at (last_index @cursor))))

(def save_cursor!
  Statement
  (.let [cursor (|> @cursor (_.do "slice" (list)))]
    (_.statement (|> @savepoint (_.do "push" (list cursor))))))

(def restore_cursor!
  Statement
  (_.statement (_.set @cursor (|> @savepoint (_.do "pop" (list))))))

(def fail_pm! _.break)

(def (multi_pop_cursor! pops)
  (-> Nat Statement)
  (.let [popsJS (_.i32 (.int pops))]
    (_.statement (|> @cursor (_.do "splice" (list (|> @cursor ..length (_.- popsJS))
                                                  popsJS))))))

(with_template [<name> <flag>]
  [(def (<name> simple? idx)
     (-> Bit Nat Statement)
     (all _.then
          (_.statement (_.set @temp (//runtime.sum//get ..peek_cursor <flag>
                                                        (|> idx .int _.i32))))
          (.if simple?
            (_.when (_.= _.null @temp)
              ..fail_pm!)
            (_.if (_.= _.null @temp)
              ..fail_pm!
              (push_cursor! @temp)))))]

  [left_choice  _.null]
  [right_choice //runtime.unit]
  )

(def (alternation pre! post!)
  (-> Statement Statement Statement)
  (all _.then
       (_.do_while (_.boolean false)
                   (all _.then
                        ..save_cursor!
                        pre!))
       (all _.then
            ..restore_cursor!
            post!)))

(def (optimized_pattern_matching again pathP)
  (-> (-> Path (Operation Statement))
      (-> Path (Operation (Maybe Statement))))
  (.when pathP
    (^.with_template [<simple> <choice>]
      [(<simple> idx nextP)
       (|> nextP
           again
           (of phase.monad each (|>> (_.then (<choice> true idx)) {.#Some})))])
    ([synthesis.simple_left_side  ..left_choice]
     [synthesis.simple_right_side ..right_choice])

    (synthesis.member/left 0)
    (phase#in {.#Some (push_cursor! (_.at (_.i32 +0) ..peek_cursor))})

    ... Extra optimization
    (synthesis.path/seq
     (synthesis.member/left 0)
     (synthesis.!bind_top register thenP))
    (do phase.monad
      [then! (again thenP)]
      (in {.#Some (all _.then
                       (_.define (..register register) (_.at (_.i32 +0) ..peek_cursor))
                       then!)}))

    ... Extra optimization
    (^.with_template [<pm> <getter>]
      [(synthesis.path/seq
        (<pm> lefts)
        (synthesis.!bind_top register thenP))
       (do phase.monad
         [then! (again thenP)]
         (in {.#Some (all _.then
                          (_.define (..register register) (<getter> (_.i32 (.int lefts)) ..peek_cursor))
                          then!)}))])
    ([synthesis.member/left  //runtime.tuple//left]
     [synthesis.member/right //runtime.tuple//right])

    (synthesis.!bind_top register thenP)
    (do phase.monad
      [then! (again thenP)]
      (in {.#Some (all _.then
                       (_.define (..register register) ..peek_and_pop_cursor)
                       then!)}))

    (synthesis.!multi_pop nextP)
    (.let [[extra_pops nextP'] (////synthesis/when.count_pops nextP)]
      (do phase.monad
        [next! (again nextP')]
        (in {.#Some (all _.then
                         (multi_pop_cursor! (n.+ 2 extra_pops))
                         next!)})))

    _
    (phase#in {.#None})))

(def (pattern_matching' statement expression archive)
  (-> Phase! Phase Archive
      (-> Path (Operation Statement)))
  (function (again pathP)
    (do phase.monad
      [outcome (optimized_pattern_matching again pathP)]
      (.when outcome
        {.#Some outcome}
        (in outcome)

        {.#None}
        (.when pathP
          {synthesis.#Then bodyS}
          (statement expression archive bodyS)

          {synthesis.#Pop}
          (phase#in pop_cursor!)

          {synthesis.#Bind register}
          (phase#in (_.define (..register register) ..peek_cursor))

          {synthesis.#Bit_Fork when thenP elseP}
          (do [! phase.monad]
            [then! (again thenP)
             else! (.when elseP
                     {.#Some elseP}
                     (again elseP)

                     {.#None}
                     (in ..fail_pm!))]
            (in (.if when
                  (_.if ..peek_cursor
                    then!
                    else!)
                  (_.if ..peek_cursor
                    else!
                    then!))))

          {synthesis.#I64_Fork item}
          (do [! phase.monad]
            [clauses (monad.each ! (function (_ [match then])
                                     (do !
                                       [then! (again then)]
                                       (in [(//runtime.i64::= (//primitive.i64 (.int match))
                                                              ..peek_cursor)
                                            then!])))
                                 {.#Item item})]
            (in (list#mix (function (_ [when then] else)
                            (_.if when then else))
                          ..fail_pm!
                          clauses)))

          (^.with_template [<tag> <format>]
            [{<tag> item}
             (do [! phase.monad]
               [cases (monad.each ! (function (_ [match then])
                                      (of ! each (|>> [(list (<format> match))]) (again then)))
                                  {.#Item item})]
               (in (_.switch ..peek_cursor
                             cases
                             {.#Some ..fail_pm!})))])
          ([synthesis.#F64_Fork //primitive.f64]
           [synthesis.#Text_Fork //primitive.text])

          (^.with_template [<complex> <choice>]
            [(<complex> idx)
             (phase#in (<choice> false idx))])
          ([synthesis.side/left  ..left_choice]
           [synthesis.side/right ..right_choice])

          (^.with_template [<pm> <getter>]
            [(<pm> lefts)
             (phase#in (push_cursor! (<getter> (_.i32 (.int lefts)) ..peek_cursor)))])
          ([synthesis.member/left  //runtime.tuple//left]
           [synthesis.member/right //runtime.tuple//right])

          (^.with_template [<tag> <combinator>]
            [(<tag> leftP rightP)
             (do phase.monad
               [left! (again leftP)
                right! (again rightP)]
               (in (<combinator> left! right!)))])
          ([synthesis.path/seq _.then]
           [synthesis.path/alt ..alternation]))))))

(def (pattern_matching statement expression archive pathP)
  (-> Phase! Phase Archive Path (Operation Statement))
  (do phase.monad
    [pattern_matching! (pattern_matching' statement expression archive pathP)]
    (in (all _.then
             (_.do_while (_.boolean false)
                         pattern_matching!)
             (_.throw (_.string ////synthesis/when.pattern_matching_error))))))

(def .public (when! statement expression archive [valueS pathP])
  (Translator! [synthesis.Term Path])
  (do phase.monad
    [stack_init (expression archive valueS)
     pattern_matching! (pattern_matching statement expression archive pathP)]
    (in (all _.then
             (_.declare @temp)
             (_.define @cursor (_.array (list stack_init)))
             (_.define @savepoint (_.array (list)))
             pattern_matching!))))

(def .public (when statement expression archive [valueS pathP])
  (-> Phase! (Translator [synthesis.Term Path]))
  (do phase.monad
    [pattern_matching! (..when! statement expression archive [valueS pathP])]
    (in (_.apply (_.closure (list) pattern_matching!) (list)))))