aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/when.lux
blob: a21bfc7e34b6f6360ab1dbda94b1f3c1d24d9fa9 (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
... 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 Type Label if let exec when int)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["[0]" function]]
   [data
    [collection
     ["[0]" list (.use "[1]#[0]" mix)]]
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]]
   [math
    [number
     ["n" nat]
     ["[0]" i32]]]
   [meta
    [macro
     ["^" pattern]]
    [compiler
     [target
      [jvm
       ["_" bytecode (.only Label Bytecode) (.use "[1]#[0]" monad)
        [environment
         [limit
          ["[0]" stack]]]]
       ["[0]" type (.only Type)
        [category (.only Method)]]]]]]]]
 ["[0]" //
  ["[1][0]" type]
  ["[1][0]" runtime (.only Operation Phase Translator)]
  ["[1][0]" value]
  ["[1][0]" complex]
  [////
   ["[0]" phase (.use "operation#[0]" monad)]
   ["[0]" translation]
   ["[0]" synthesis (.only Path Fork)
    [access
     ["[0]" member (.only Member)]]]
   [///
    [reference
     [variable (.only Register)]]]]])

(def (pop_alt stack_depth)
  (-> Nat
      (Bytecode Any))
  (.when stack_depth
    0 (_#in [])
    1 _.pop
    2 _.pop2
    _ ... (n.> 2)
    (all _.composite
         _.pop2
         (pop_alt (n.- 2 stack_depth)))))

(def int
  (-> (I64 Any)
      (Bytecode Any))
  (|>> .i64 i32.i32 _.int))

(def long
  (-> (I64 Any)
      (Bytecode Any))
  (|>> .int _.long))

(def peek
  (Bytecode Any)
  (all _.composite
       _.dup
       (//runtime.get //runtime.stack_head)))

(def pop
  (Bytecode Any)
  (all _.composite
       (//runtime.get //runtime.stack_tail)
       (_.checkcast //type.stack)))

(def (left_projection lefts)
  (-> Nat
      (Bytecode Any))
  (all _.composite
       (_.checkcast //type.tuple)
       (..int lefts)
       (.when lefts
         0
         _.aaload
         
         lefts
         //runtime.left_projection)))

(def (right_projection lefts)
  (-> Nat
      (Bytecode Any))
  (all _.composite
       (_.checkcast //type.tuple)
       (..int lefts)
       //runtime.right_projection))

(def equals@Object
  (.let [class (type.class "java.lang.Object" (list))
         method (type.method [(list) (list //type.value) type.boolean (list)])]
    (_.invokevirtual class "equals" method)))

(def (path|bind register)
  (-> Register
      (Operation (Bytecode Any)))
  (operation#in (all _.composite
                     ..peek
                     (_.astore register))))

(def (path|bit_fork again @else [when thenP elseP])
  (-> (-> Path (Operation (Bytecode Any)))
      Label [Bit Path (Maybe Path)]
      (Operation (Bytecode Any)))
  (do phase.monad
    [then! (again thenP)
     else! (.when elseP
             {.#Some elseP}
             (again elseP)

             {.#None}
             (in (_.goto @else)))
     .let [if! (.if when _.ifeq _.ifne)]]
    (in (do _.monad
          [@else _.new_label]
          (all _.composite
               ..peek
               (//value.primitive type.boolean)
               (if! @else)
               then!
               (_.set_label @else)
               else!)))))

(with_template [<name> <type> <unwrap> <dup> <pop> <test> <comparison> <if>]
  [(def (<name> again @else cons)
     (-> (-> Path (Operation (Bytecode Any)))
         Label (Fork <type> Path)
         (Operation (Bytecode Any)))
     (do [! phase.monad]
       [fork! (monad.mix ! (function (_ [test thenP] else!)
                             (do !
                               [then! (again thenP)]
                               (in (do _.monad
                                     [@else _.new_label]
                                     (all _.composite
                                          <dup>
                                          (<test> test)
                                          <comparison>
                                          (<if> @else)
                                          <pop>
                                          then!
                                          (_.set_label @else)
                                          else!)))))
                         (all _.composite
                              <pop>
                              (_.goto @else))
                         {.#Item cons})]
       (in (all _.composite
                ..peek
                <unwrap>
                fork!))))]

  [path|i64_fork (I64 Any) (//value.primitive type.long) _.dup2 _.pop2 ..long _.lcmp _.ifne]
  [path|f64_fork Frac (//value.primitive type.double) _.dup2 _.pop2 _.double _.dcmpl _.ifne]
  [path|text_fork Text (of _.monad in []) _.dup _.pop _.string ..equals@Object _.ifeq]
  )

(def (path' stack_depth @else @end phase archive)
  (-> Nat Label Label
      (Translator Path))
  (function (again path)
    (.when path
      {synthesis.#Pop}
      (operation#in ..pop)
      
      {synthesis.#Bind register}
      (..path|bind register)

      (^.with_template [<tag> <path>]
        [{<tag> it}
         (<path> again @else it)])
      ([synthesis.#Bit_Fork ..path|bit_fork]
       [synthesis.#I64_Fork ..path|i64_fork]
       [synthesis.#F64_Fork ..path|f64_fork]
       [synthesis.#Text_Fork ..path|text_fork])

      {synthesis.#Then bodyS}
      (do phase.monad
        [body! (phase archive bodyS)]
        (in (all _.composite
                 (..pop_alt stack_depth)
                 body!
                 (_.when_continuous (_.goto @end)))))
      
      (synthesis.side lefts right?)
      (operation#in
       (do _.monad
         [@success _.new_label]
         (all _.composite
              ..peek
              (_.checkcast //type.variant)
              (//complex.lefts lefts)
              (//complex.right? right?)
              //runtime.when
              _.dup
              (_.ifnonnull @success)
              _.pop
              (_.goto @else)
              (_.set_label @success)
              //runtime.push)))

      (^.with_template [<pattern> <projection>]
        [(<pattern> lefts)
         (operation#in (all _.composite
                            ..peek
                            (<projection> lefts)
                            //runtime.push))

         ... Extra optimization
         (synthesis.path/seq
          (<pattern> lefts)
          (synthesis.!bind_top register thenP))
         (do phase.monad
           [then! (path' stack_depth @else @end phase archive thenP)]
           (in (all _.composite
                    ..peek
                    (<projection> lefts)
                    (_.astore register)
                    then!)))])
      ([synthesis.member/left ..left_projection]
       [synthesis.member/right ..right_projection])
      
      {synthesis.#Seq leftP rightP}
      (do phase.monad
        [left! (path' stack_depth @else @end phase archive leftP)
         right! (path' stack_depth @else @end phase archive rightP)]
        (in (all _.composite
                 left!
                 right!)))

      {synthesis.#Alt leftP rightP}
      (do phase.monad
        [@alt_else //runtime.forge_label
         left! (path' (++ stack_depth) @alt_else @end phase archive leftP)
         right! (path' stack_depth @else @end phase archive rightP)]
        (in (all _.composite
                 _.dup
                 left!
                 (_.set_label @alt_else)
                 _.pop
                 right!)))
      )))

(def (path @end phase archive path)
  (-> Label
      (Translator Path))
  (do phase.monad
    [@else //runtime.forge_label
     path! (..path' 1 @else @end phase archive path)]
    (in (all _.composite
             path!
             (<| (_.when_acknowledged @else)
                 (all _.composite
                      (_.set_label @else)
                      //runtime.pm_failure
                      (_.goto @end)
                      ))
             ))))

(def .public (if phase archive [testS thenS elseS])
  (Translator [synthesis.Term synthesis.Term synthesis.Term])
  (do phase.monad
    [test! (phase archive testS)
     then! (phase archive thenS)
     else! (phase archive elseS)]
    (in (do _.monad
          [@else _.new_label
           @end _.new_label]
          (all _.composite
               test!
               (//value.primitive type.boolean)
               (_.ifeq @else)
               then!
               (_.when_continuous (_.goto @end))
               (_.set_label @else)
               else!
               (<| (_.when_acknowledged @end)
                   (_.set_label @end)))))))

(def .public (exec phase archive [this that])
  (Translator [synthesis.Term synthesis.Term])
  (do phase.monad
    [this! (phase archive this)
     that! (phase archive that)]
    (in (all _.composite
             this!
             _.pop
             that!))))

(def .public (let phase archive [inputS register bodyS])
  (Translator [synthesis.Term Register synthesis.Term])
  (do phase.monad
    [input! (phase archive inputS)
     body! (phase archive bodyS)]
    (in (all _.composite
             input!
             (_.astore register)
             body!))))

(def .public (get phase archive [path recordS])
  (Translator [(List Member) synthesis.Term])
  (do phase.monad
    [record! (phase archive recordS)]
    (in (list#mix (function (_ step so_far!)
                    (.let [next! (.if (the member.#right? step)
                                   (..right_projection (the member.#lefts step))
                                   (..left_projection (the member.#lefts step)))]
                      (all _.composite
                           so_far!
                           next!)))
                  record!
                  path))))

(def .public (when phase archive [valueS path])
  (Translator [synthesis.Term Path])
  (do phase.monad
    [@end //runtime.forge_label
     value! (phase archive valueS)
     path! (..path @end phase archive path)]
    (in (all _.composite
             _.aconst_null
             value!
             //runtime.push
             path!
             (<| (_.when_acknowledged @end)
                 (_.set_label @end))))))