aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux
blob: 12359b7130424d145f11a13bd252b85b5cfe1324 (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
(.using
 [library
  [lux (.except i64)
   ["[0]" meta]
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["[0]" function]
    ["<>" parser (.only)
     ["<[0]>" code]]]
   [data
    ["[0]" product]
    ["[0]" text (.open: "[1]#[0]" hash)
     ["%" format (.only format)]
     [encoding
      ["[0]" utf8]]]
    [collection
     ["[0]" list (.open: "[1]#[0]" functor)]
     ["[0]" sequence]]]
   ["[0]" macro (.only)
    [syntax (.only syntax:)]
    ["[0]" code]]
   [math
    [number (.only hex)
     ["[0]" i64]]]
   [target
    ["_" js (.only Expression Var Computation Statement)]]]]
 ["[0]" ///
  ["[1][0]" reference]
  ["//[1]" ///
   ["[1][0]" synthesis (.only Synthesis)]
   ["[1][0]" generation]
   ["//[1]" /// (.only)
    ["[1][0]" phase]
    [reference
     [variable (.only Register)]]
    [meta
     [archive (.only Output Archive)
      ["[0]" registry (.only Registry)]
      ["[0]" unit]]]]]])

(template [<name> <base>]
  [(type: .public <name>
     (<base> [Register Text] Expression Statement))]

  [Operation /////generation.Operation]
  [Phase /////generation.Phase]
  [Handler /////generation.Handler]
  [Bundle /////generation.Bundle]
  )

(type: .public (Generator i)
  (-> Phase Archive i (Operation Expression)))

(type: .public Phase!
  (-> Phase Archive Synthesis (Operation Statement)))

(type: .public (Generator! i)
  (-> Phase! Phase Archive i (Operation Statement)))

(def: .public high
  (-> (I64 Any) (I64 Any))
  (i64.right_shifted 32))

(def: .public low
  (-> (I64 Any) (I64 Any))
  (let [mask (-- (i64.left_shifted 32 1))]
    (|>> (i64.and mask))))

(def: .public unit
  Computation
  (_.string /////synthesis.unit))

(def: .public (flag value)
  (-> Bit Computation)
  (if value
    (_.string "")
    _.null))

(def: (feature name definition)
  (-> Var (-> Var Expression) Statement)
  (_.define name (definition name)))

(syntax: .public (with_vars [vars (<code>.tuple (<>.some <code>.local))
                             body <code>.any])
  (do [! meta.monad]
    [ids (monad.all ! (list.repeated (list.size vars) meta.seed))]
    (in (list (` (let [(~+ (|> vars
                               (list.zipped_2 ids)
                               (list#each (function (_ [id var])
                                            (list (code.local var)
                                                  (` (_.var (~ (code.text (format "v" (%.nat id)))))))))
                               list.together))]
                   (~ body)))))))

(syntax: (runtime: [declaration (<>.or <code>.local
                                       (<code>.form (<>.and <code>.local
                                                            (<>.some <code>.local))))
                    code <code>.any])
  (macro.with_symbols [g!_ runtime]
    (let [runtime_name (` (_.var (~ (code.text (%.code runtime)))))]
      (case declaration
        {.#Left name}
        (let [g!name (code.local name)]
          (in (list (` (def: .public (~ g!name)
                         Var
                         (~ runtime_name)))
                    
                    (` (def: (~ (code.local (format "@" name)))
                         Statement
                         (..feature (~ runtime_name)
                                    (function ((~ g!_) (~ g!name))
                                      (~ code))))))))
        
        {.#Right [name inputs]}
        (let [g!name (code.local name)
              inputsC (list#each code.local inputs)
              inputs_typesC (list#each (function.constant (` _.Expression)) inputs)]
          (in (list (` (def: .public ((~ g!name) (~+ inputsC))
                         (-> (~+ inputs_typesC) Computation)
                         (_.apply (~ runtime_name) (list (~+ inputsC)))))
                    
                    (` (def: (~ (code.local (format "@" name)))
                         Statement
                         (..feature (~ runtime_name)
                                    (function ((~ g!_) (~ g!_))
                                      (..with_vars [(~+ inputsC)]
                                        (_.function (~ g!_) (list (~+ inputsC))
                                          (~ code))))))))))))))

(def: length
  (-> Expression Computation)
  (_.the "length"))

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

(def: (last_element tuple)
  (_.at (..last_index tuple)
        tuple))

(with_expansions [<recur> (these (all _.then
                                      (_.set lefts (_.- last_index_right lefts))
                                      (_.set tuple (_.at last_index_right tuple))))]
  (runtime: (tuple//left lefts tuple)
    (with_vars [last_index_right]
      (<| (_.while (_.boolean true))
          (all _.then
               (_.define last_index_right (..last_index tuple))
               (_.if (_.> lefts last_index_right)
                 ... No need for recursion
                 (_.return (_.at lefts tuple))
                 ... Needs recursion
                 <recur>)))))

  (runtime: (tuple//right lefts tuple)
    (with_vars [last_index_right right_index]
      (<| (_.while (_.boolean true))
          (all _.then
               (_.define last_index_right (..last_index tuple))
               (_.define right_index (_.+ (_.i32 +1) lefts))
               (<| (_.if (_.= last_index_right right_index)
                     (_.return (_.at right_index tuple)))
                   (_.if (_.> last_index_right right_index)
                     ... Needs recursion.
                     <recur>)
                   (_.return (_.do "slice" (list right_index) tuple)))
               )))))

(def: .public variant_tag_field "_lux_tag")
(def: .public variant_flag_field "_lux_flag")
(def: .public variant_value_field "_lux_value")

(runtime: variant//new
  (let [@this (_.var "this")]
    (with_vars [tag is_last value]
      (_.closure (list tag is_last value)
                 (all _.then
                      (_.set (_.the ..variant_tag_field @this) tag)
                      (_.set (_.the ..variant_flag_field @this) is_last)
                      (_.set (_.the ..variant_value_field @this) value)
                      )))))

(def: .public (variant tag last? value)
  (-> Expression Expression Expression Computation)
  (_.new ..variant//new (list tag last? value)))

(runtime: (sum//get sum expected##right? expected##lefts)
  (let [mismatch! (_.return _.null)
        actual##lefts (|> sum (_.the ..variant_tag_field))
        actual##right? (|> sum (_.the ..variant_flag_field))
        actual##value (|> sum (_.the ..variant_value_field))
        is_last? (_.= ..unit actual##right?)
        recur! (all _.then
                    (_.set expected##lefts (|> expected##lefts
                                               (_.- actual##lefts)
                                               (_.- (_.i32 +1))))
                    (_.set sum actual##value))]
    (<| (_.while (_.boolean true))
        (_.if (_.= expected##lefts actual##lefts)
          (_.if (_.= expected##right? actual##right?)
            (_.return actual##value)
            mismatch!))
        (_.if (_.< expected##lefts actual##lefts)
          (_.if (_.= ..unit actual##right?)
            recur!
            mismatch!))
        (_.if (_.= ..unit expected##right?)
          (_.return (..variant (|> actual##lefts
                                   (_.- expected##lefts)
                                   (_.- (_.i32 +1)))
                               actual##right?
                               actual##value)))
        mismatch!)))

(def: left
  (-> Expression Computation)
  (..variant (_.i32 +0) (flag #0)))

(def: right
  (-> Expression Computation)
  (..variant (_.i32 +0) (flag #1)))

(def: none
  Computation
  (..left ..unit))

(def: some
  (-> Expression Computation)
  ..right)

(def: runtime//structure
  Statement
  (all _.then
       @tuple//left
       @tuple//right
       @variant//new
       @sum//get
       ))

(runtime: (lux//try op)
  (with_vars [ex]
    (_.try (_.return (..right (_.apply_1 op ..unit)))
           [ex (_.return (..left (|> ex (_.do "toString" (list)))))])))

(runtime: (lux//program_args inputs)
  (with_vars [output idx]
    (all _.then
         (_.define output ..none)
         (_.for idx
                (..last_index inputs)
                (_.>= (_.i32 +0) idx)
                (_.-- idx)
                (_.set output (..some (_.array (list (_.at idx inputs)
                                                     output)))))
         (_.return output))))

(def: runtime//lux
  Statement
  (all _.then
       @lux//try
       @lux//program_args
       ))

(def: .public i64_low_field Text "_lux_low")
(def: .public i64_high_field Text "_lux_high")

(runtime: i64##new
  (let [@this (_.var "this")]
    (with_vars [high low]
      (_.closure (list high low)
                 (all _.then
                      (_.set (_.the ..i64_high_field @this) high)
                      (_.set (_.the ..i64_low_field @this) low)
                      )))))

(def: .public (i64 high low)
  (-> Expression Expression Computation)
  (_.new ..i64##new (list high low)))

(template [<name> <op>]
  [(runtime: (<name> subject parameter)
     (_.return (..i64 (<op> (_.the ..i64_high_field subject)
                            (_.the ..i64_high_field parameter))
                      (<op> (_.the ..i64_low_field subject)
                            (_.the ..i64_low_field parameter)))))]

  [i64##xor _.bit_xor]
  [i64##or  _.bit_or]
  [i64##and _.bit_and]
  )

(runtime: (i64##not value)
  (_.return (..i64 (_.bit_not (_.the ..i64_high_field value))
                   (_.bit_not (_.the ..i64_low_field value)))))

(def: (cap_shift! shift)
  (-> Var Statement)
  (_.set shift (|> shift (_.bit_and (_.i32 +63)))))

(def: (no_shift! shift input)
  (-> Var Var (-> Expression Expression))
  (_.? (|> shift (_.= (_.i32 +0)))
       input))

(def: small_shift?
  (-> Var Expression)
  (|>> (_.< (_.i32 +32))))

(runtime: (i64##left_shifted input shift)
  (all _.then
       (..cap_shift! shift)
       (_.return (<| (..no_shift! shift input)
                     (_.? (..small_shift? shift)
                          (let [high (_.bit_or (|> input (_.the ..i64_high_field) (_.left_shift shift))
                                               (|> input (_.the ..i64_low_field) (_.logic_right_shift (_.- shift (_.i32 +32)))))
                                low (|> input (_.the ..i64_low_field) (_.left_shift shift))]
                            (..i64 high low)))
                     (let [high (|> input (_.the ..i64_low_field) (_.left_shift (_.- (_.i32 +32) shift)))]
                       (..i64 high (_.i32 +0)))))
       ))

(runtime: (i64##arithmetic_right_shifted input shift)
  (all _.then
       (..cap_shift! shift)
       (_.return (<| (..no_shift! shift input)
                     (_.? (..small_shift? shift)
                          (let [high (|> input (_.the ..i64_high_field) (_.arithmetic_right_shift shift))
                                low (|> input (_.the ..i64_low_field) (_.logic_right_shift shift)
                                        (_.bit_or (|> input (_.the ..i64_high_field) (_.left_shift (_.- shift (_.i32 +32))))))]
                            (..i64 high low)))
                     (let [high (_.? (|> input (_.the ..i64_high_field) (_.>= (_.i32 +0)))
                                     (_.i32 +0)
                                     (_.i32 -1))
                           low (|> input (_.the ..i64_high_field) (_.arithmetic_right_shift (_.- (_.i32 +32) shift)))]
                       (..i64 high low))))))

(runtime: (i64##right_shifted input shift)
  (all _.then
       (..cap_shift! shift)
       (_.return (<| (..no_shift! shift input)
                     (_.? (..small_shift? shift)
                          (let [high (|> input (_.the ..i64_high_field) (_.logic_right_shift shift))
                                low (|> input (_.the ..i64_low_field) (_.logic_right_shift shift)
                                        (_.bit_or (|> input (_.the ..i64_high_field) (_.left_shift (_.- shift (_.i32 +32))))))]
                            (..i64 high low)))
                     (_.? (|> shift (_.= (_.i32 +32)))
                          (..i64 (_.i32 +0) (|> input (_.the ..i64_high_field))))
                     (..i64 (_.i32 +0)
                            (|> input (_.the ..i64_high_field) (_.logic_right_shift (_.- (_.i32 +32) shift))))))))

(def: runtime//bit
  Statement
  (all _.then
       @i64##and
       @i64##or
       @i64##xor
       @i64##not
       @i64##left_shifted
       @i64##arithmetic_right_shifted
       @i64##right_shifted
       ))

(runtime: i64##2^16
  (_.left_shift (_.i32 +16) (_.i32 +1)))

(runtime: i64##2^32
  (_.* i64##2^16 i64##2^16))

(runtime: i64##2^64
  (_.* i64##2^32 i64##2^32))

(runtime: i64##2^63
  (|> i64##2^64 (_./ (_.i32 +2))))

(runtime: (i64##unsigned_low i64)
  (_.return (_.? (|> i64 (_.the ..i64_low_field) (_.>= (_.i32 +0)))
                 (|> i64 (_.the ..i64_low_field))
                 (|> i64 (_.the ..i64_low_field) (_.+ i64##2^32)))))

(runtime: (i64##number i64)
  (_.return (|> i64
                (_.the ..i64_high_field)
                (_.* i64##2^32)
                (_.+ (i64##unsigned_low i64)))))

(runtime: i64##zero
  (..i64 (_.i32 +0) (_.i32 +0)))

(runtime: i64##min
  (..i64 (_.i32 (.int (hex "80,00,00,00")))
         (_.i32 +0)))

(runtime: i64##max
  (..i64 (_.i32 (.int (hex "7F,FF,FF,FF")))
         (_.i32 (.int (hex "FF,FF,FF,FF")))))

(runtime: i64##one
  (..i64 (_.i32 +0) (_.i32 +1)))

(runtime: (i64##= reference sample)
  (_.return (_.and (_.= (_.the ..i64_high_field reference)
                        (_.the ..i64_high_field sample))
                   (_.= (_.the ..i64_low_field reference)
                        (_.the ..i64_low_field sample)))))

(runtime: (i64##+ parameter subject)
  (let [up_16 (_.left_shift (_.i32 +16))
        high_16 (_.logic_right_shift (_.i32 +16))
        low_16 (_.bit_and (_.i32 (.int (hex "FFFF"))))
        hh (|>> (_.the ..i64_high_field) high_16)
        hl (|>> (_.the ..i64_high_field) low_16)
        lh (|>> (_.the ..i64_low_field) high_16)
        ll (|>> (_.the ..i64_low_field) low_16)]
    (with_vars [l48 l32 l16 l00
                r48 r32 r16 r00
                x48 x32 x16 x00]
      (all _.then
           (_.define l48 (hh subject))
           (_.define l32 (hl subject))
           (_.define l16 (lh subject))
           (_.define l00 (ll subject))

           (_.define r48 (hh parameter))
           (_.define r32 (hl parameter))
           (_.define r16 (lh parameter))
           (_.define r00 (ll parameter))

           (_.define x00 (_.+ l00 r00))
           
           (_.define x16 (|> (high_16 x00)
                             (_.+ l16)
                             (_.+ r16)))
           (_.set x00 (low_16 x00))
           
           (_.define x32 (|> (high_16 x16)
                             (_.+ l32)
                             (_.+ r32)))
           (_.set x16 (low_16 x16))
           
           (_.define x48 (|> (high_16 x32)
                             (_.+ l48)
                             (_.+ r48)
                             low_16))
           (_.set x32 (low_16 x32))

           (_.return (..i64 (_.bit_or (up_16 x48) x32)
                            (_.bit_or (up_16 x16) x00)))
           ))))

(runtime: (i64##opposite value)
  (_.return (_.? (i64##= i64##min value)
                 i64##min
                 (i64##+ i64##one (i64##not value)))))

(runtime: i64##-one
  (i64##opposite i64##one))

(runtime: (i64##of_number value)
  (_.return (<| (_.? (_.not_a_number? value)
                     i64##zero)
                (_.? (_.<= (_.opposite i64##2^63) value)
                     i64##min)
                (_.? (|> value (_.+ (_.i32 +1)) (_.>= i64##2^63))
                     i64##max)
                (_.? (|> value (_.< (_.i32 +0)))
                     (|> value _.opposite i64##of_number i64##opposite))
                (..i64 (|> value (_./ i64##2^32) _.to_i32)
                       (|> value (_.% i64##2^32) _.to_i32)))))

(runtime: (i64##- parameter subject)
  (_.return (i64##+ (i64##opposite parameter) subject)))

(runtime: (i64##* parameter subject)
  (let [up_16 (_.left_shift (_.i32 +16))
        high_16 (_.logic_right_shift (_.i32 +16))
        low_16 (_.bit_and (_.i32 (.int (hex "FFFF"))))
        hh (|>> (_.the ..i64_high_field) high_16)
        hl (|>> (_.the ..i64_high_field) low_16)
        lh (|>> (_.the ..i64_low_field) high_16)
        ll (|>> (_.the ..i64_low_field) low_16)]
    (with_vars [l48 l32 l16 l00
                r48 r32 r16 r00
                x48 x32 x16 x00]
      (all _.then
           (_.define l48 (hh subject))
           (_.define l32 (hl subject))
           (_.define l16 (lh subject))
           (_.define l00 (ll subject))

           (_.define r48 (hh parameter))
           (_.define r32 (hl parameter))
           (_.define r16 (lh parameter))
           (_.define r00 (ll parameter))

           (_.define x00 (_.* l00 r00))
           (_.define x16 (high_16 x00))
           (_.set x00 (low_16 x00))
           
           (_.set x16 (|> x16 (_.+ (_.* l16 r00))))
           (_.define x32 (high_16 x16)) (_.set x16 (low_16 x16))
           (_.set x16 (|> x16 (_.+ (_.* l00 r16))))
           (_.set x32 (|> x32 (_.+ (high_16 x16)))) (_.set x16 (low_16 x16))

           (_.set x32 (|> x32 (_.+ (_.* l32 r00))))
           (_.define x48 (high_16 x32)) (_.set x32 (low_16 x32))
           (_.set x32 (|> x32 (_.+ (_.* l16 r16))))
           (_.set x48 (|> x48 (_.+ (high_16 x32)))) (_.set x32 (low_16 x32))
           (_.set x32 (|> x32 (_.+ (_.* l00 r32))))
           (_.set x48 (|> x48 (_.+ (high_16 x32)))) (_.set x32 (low_16 x32))
           
           (_.set x48 (|> x48
                          (_.+ (_.* l48 r00))
                          (_.+ (_.* l32 r16))
                          (_.+ (_.* l16 r32))
                          (_.+ (_.* l00 r48))
                          low_16))

           (_.return (..i64 (_.bit_or (up_16 x48) x32)
                            (_.bit_or (up_16 x16) x00)))
           ))))

(runtime: (i64##< parameter subject)
  (let [negative? (|>> (_.the ..i64_high_field) (_.< (_.i32 +0)))]
    (with_vars [-subject? -parameter?]
      (all _.then
           (_.define -subject? (negative? subject))
           (_.define -parameter? (negative? parameter))
           (_.return (<| (_.? (_.and -subject? (_.not -parameter?))
                              (_.boolean true))
                         (_.? (_.and (_.not -subject?) -parameter?)
                              (_.boolean false))
                         (negative? (i64##- parameter subject))))
           ))))

(def: (i64##<= param subject)
  (-> Expression Expression Expression)
  (|> (i64##< param subject)
      (_.or (i64##= param subject))))

(def: negative?
  (i64##< i64##zero))

(runtime: (i64##/ parameter subject)
  (<| (_.if (i64##= i64##zero parameter)
        (_.throw (_.string "Cannot divide by zero!")))
      (_.if (i64##= i64##zero subject)
        (_.return i64##zero))
      (_.if (i64##= i64##min subject)
        (<| (_.if (_.or (i64##= i64##one parameter)
                        (i64##= i64##-one parameter))
              (_.return i64##min))
            (_.if (i64##= i64##min parameter)
              (_.return i64##one))
            (with_vars [approximation]
              (let [subject/2 (..i64##arithmetic_right_shifted subject (_.i32 +1))]
                (all _.then
                     (_.define approximation (i64##left_shifted (i64##/ parameter
                                                                        subject/2)
                                                                (_.i32 +1)))
                     (_.if (i64##= i64##zero approximation)
                       (_.return (_.? (..negative? parameter)
                                      i64##one
                                      i64##-one))
                       (let [remainder (i64##- (i64##* approximation
                                                       parameter)
                                               subject)]
                         (_.return (i64##+ (i64##/ parameter
                                                   remainder)
                                           approximation)))))))))
      (_.if (i64##= i64##min parameter)
        (_.return i64##zero))
      (_.if (..negative? subject)
        (_.return (_.? (..negative? parameter)
                       (i64##/ (i64##opposite parameter)
                               (i64##opposite subject))
                       (i64##opposite (i64##/ parameter
                                              (i64##opposite subject))))))
      (_.if (..negative? parameter)
        (_.return (i64##opposite (i64##/ (i64##opposite parameter) subject))))
      (with_vars [result remainder]
        (all _.then
             (_.define result i64##zero)
             (_.define remainder subject)
             (_.while (i64##<= remainder parameter)
                      (with_vars [approximate approximate_result approximate_remainder log2 delta]
                        (let [approximate_result' (i64##of_number approximate)
                              approx_remainder (i64##* parameter approximate_result)]
                          (all _.then
                               (_.define approximate (|> (i64##number remainder)
                                                         (_./ (i64##number parameter))
                                                         (_.apply_1 (_.var "Math.floor"))
                                                         (_.apply_2 (_.var "Math.max") (_.i32 +1))))
                               (_.define log2 (|> approximate
                                                  (_.apply_1 (_.var "Math.log"))
                                                  (_./ (_.var "Math.LN2"))
                                                  (_.apply_1 (_.var "Math.ceil"))))
                               (_.define delta (_.? (_.> (_.i32 +48) log2)
                                                    (_.apply_2 (_.var "Math.pow")
                                                               (_.i32 +2)
                                                               (_.- (_.i32 +48)
                                                                    log2))
                                                    (_.i32 +1)))
                               (_.define approximate_result approximate_result')
                               (_.define approximate_remainder approx_remainder)
                               (_.while (_.or (..negative? approximate_remainder)
                                              (i64##< approximate_remainder
                                                      remainder))
                                        (all _.then
                                             (_.set approximate (_.- delta approximate))
                                             (_.set approximate_result approximate_result')
                                             (_.set approximate_remainder approx_remainder)))
                               (_.set result (i64##+ (_.? (i64##= i64##zero approximate_result)
                                                          i64##one
                                                          approximate_result)
                                                     result))
                               (_.set remainder (i64##- approximate_remainder remainder))))))
             (_.return result)))))

(runtime: (i64##% parameter subject)
  (let [flat (|> subject
                 (i64##/ parameter)
                 (i64##* parameter))]
    (_.return (i64##- flat subject))))

(def: runtime//i64
  Statement
  (all _.then
       ..runtime//bit

       @i64##2^16
       @i64##2^32
       @i64##2^64
       @i64##2^63
       @i64##unsigned_low
       @i64##new
       @i64##zero
       @i64##min
       @i64##max
       @i64##one
       @i64##=
       @i64##+
       @i64##opposite
       @i64##-one
       @i64##number
       @i64##of_number
       @i64##-
       @i64##*
       @i64##<
       @i64##/
       @i64##%
       ))

(runtime: (text//index start part text)
  (with_vars [idx]
    (all _.then
         (_.define idx (|> text (_.do "indexOf" (list part (i64##number start)))))
         (_.return (_.? (_.= (_.i32 -1) idx)
                        ..none
                        (..some (i64##of_number idx)))))))

(runtime: (text//clip offset length text)
  (_.return (|> text (_.do "substring" (list (_.the ..i64_low_field offset)
                                             (_.+ (_.the ..i64_low_field offset)
                                                  (_.the ..i64_low_field length)))))))

(runtime: (text//char idx text)
  (with_vars [result]
    (all _.then
         (_.define result (|> text (_.do "charCodeAt" (list (_.the ..i64_low_field idx)))))
         (_.if (_.not_a_number? result)
           (_.throw (_.string "[Lux Error] Cannot get char from text."))
           (_.return (i64##of_number result))))))

(def: runtime//text
  Statement
  (all _.then
       @text//index
       @text//clip
       @text//char
       ))

(runtime: (io//log message)
  (let [console (_.var "console")
        print (_.var "print")
        end! (_.return ..unit)]
    (<| (_.if (|> console _.type_of (_.= (_.string "undefined")) _.not
                  (_.and (_.the "log" console)))
          (all _.then
               (_.statement (|> console (_.do "log" (list message))))
               end!))
        (_.if (|> print _.type_of (_.= (_.string "undefined")) _.not)
          (all _.then
               (_.statement (_.apply_1 print (_.? (_.= (_.string "string")
                                                       (_.type_of message))
                                                  message
                                                  (_.apply_1 (_.var "JSON.stringify") message))))
               end!))
        end!)))

(runtime: (io//error message)
  (_.throw message))

(def: runtime//io
  Statement
  (all _.then
       @io//log
       @io//error
       ))

(runtime: (js//get object field)
  (with_vars [temp]
    (all _.then
         (_.define temp (_.at field object))
         (_.return (_.? (_.= _.undefined temp)
                        ..none
                        (..some temp))))))

(runtime: (js//set object field input)
  (all _.then
       (_.set (_.at field object) input)
       (_.return object)))

(runtime: (js//delete object field)
  (all _.then
       (_.statement (_.delete (_.at field object)))
       (_.return object)))

(def: runtime//js
  Statement
  (all _.then
       @js//get
       @js//set
       @js//delete
       ))

(runtime: (array//write idx value array)
  (all _.then
       (_.set (_.at (_.the ..i64_low_field idx) array) value)
       (_.return array)))

(runtime: (array//delete idx array)
  (all _.then
       (_.statement (_.delete (_.at (_.the ..i64_low_field idx) array)))
       (_.return array)))

(def: runtime//array
  Statement
  (all _.then
       @array//write
       @array//delete
       ))

(def: runtime
  Statement
  (all _.then
       runtime//structure
       runtime//i64
       runtime//text
       runtime//io
       runtime//js
       runtime//array
       runtime//lux
       ))

(def: module_id
  0)

(def: .public generate
  (Operation [Registry Output])
  (do ///////phase.monad
    [_ (/////generation.execute! ..runtime)
     _ (/////generation.save! ..module_id {.#None} ..runtime)]
    (in [(|> registry.empty
             (registry.resource true unit.none)
             product.right)
         (sequence.sequence [..module_id
                             {.#None}
                             (|> ..runtime
                                 _.code
                                 (# utf8.codec encoded))])])))