aboutsummaryrefslogtreecommitdiff
path: root/src/lux/compiler.clj
blob: 7fe6871b00b29b1aaf4fd97333a21e0ee87d4fec (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
;;  Copyright (c) Eduardo Julian. All rights reserved.
;;  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 http://mozilla.org/MPL/2.0/.

(ns lux.compiler
  (:refer-clojure :exclude [compile])
  (:require (clojure [string :as string]
                     [set :as set]
                     [template :refer [do-template]])
            clojure.core.match
            clojure.core.match.array
            (lux [base :as & :refer [|let |do return* return fail fail* |case]]
                 [type :as &type]
                 [reader :as &reader]
                 [lexer :as &lexer]
                 [parser :as &parser]
                 [analyser :as &analyser]
                 [optimizer :as &optimizer]
                 [host :as &host])
            [lux.host.generics :as &host-generics]
            [lux.optimizer :as &o]
            [lux.analyser.base :as &a]
            [lux.analyser.module :as &a-module]
            (lux.compiler [base :as &&]
                          [cache :as &&cache]
                          [lux :as &&lux]
                          [host :as &&host]
                          [case :as &&case]
                          [lambda :as &&lambda]
                          [module :as &&module]
                          [io :as &&io])
            [lux.packager.program :as &packager-program])
  (:import (org.objectweb.asm Opcodes
                              Label
                              ClassWriter
                              MethodVisitor)))

;; [Utils/Compilers]
(def ^:private !source->last-line (atom nil))

(defn ^:private compile-expression [syntax]
  (|let [[[?type [_file-name _line _column]] ?form] syntax]
    (|do [^MethodVisitor *writer* &/get-writer
          :let [debug-label (new Label)
                _ (when (not= _line (get @!source->last-line _file-name))
                    (doto *writer*
                      (.visitLabel debug-label)
                      (.visitLineNumber (int _line) debug-label))
                    (swap! !source->last-line assoc _file-name _line))]]
      (|case ?form
        (&o/$bool ?value)
        (&&lux/compile-bool compile-expression ?value)

        (&o/$int ?value)
        (&&lux/compile-int compile-expression ?value)

        (&o/$real ?value)
        (&&lux/compile-real compile-expression ?value)

        (&o/$char ?value)
        (&&lux/compile-char compile-expression ?value)

        (&o/$text ?value)
        (&&lux/compile-text compile-expression ?value)

        (&o/$tuple ?elems)
        (&&lux/compile-tuple compile-expression ?elems)

        (&o/$var (&/$Local ?idx))
        (&&lux/compile-local compile-expression ?idx)

        (&o/$captured ?scope ?captured-id ?source)
        (&&lux/compile-captured compile-expression ?scope ?captured-id ?source)

        (&o/$var (&/$Global ?owner-class ?name))
        (&&lux/compile-global compile-expression ?owner-class ?name)

        (&o/$apply ?fn ?args)
        (&&lux/compile-apply compile-expression ?fn ?args)

        (&o/$variant ?tag ?members)
        (&&lux/compile-variant compile-expression ?tag ?members)

        (&o/$case ?value ?match)
        (&&case/compile-case compile-expression ?value ?match)

        (&o/$lambda ?scope ?env ?body)
        (&&lambda/compile-lambda compile-expression ?scope ?env ?body)

        (&o/$ann ?value-ex ?type-ex ?value-type)
        (&&lux/compile-ann compile-expression ?value-ex ?type-ex ?value-type)

        (&o/$coerce ?value-ex ?type-ex ?value-type)
        (&&lux/compile-coerce compile-expression ?value-ex ?type-ex ?value-type)

        ;; Characters
        (&o/$jvm-ceq ?x ?y)
        (&&host/compile-jvm-ceq compile-expression ?x ?y)

        (&o/$jvm-clt ?x ?y)
        (&&host/compile-jvm-clt compile-expression ?x ?y)

        (&o/$jvm-cgt ?x ?y)
        (&&host/compile-jvm-cgt compile-expression ?x ?y)
        
        ;; Integer arithmetic
        (&o/$jvm-iadd ?x ?y)
        (&&host/compile-jvm-iadd compile-expression ?x ?y)

        (&o/$jvm-isub ?x ?y)
        (&&host/compile-jvm-isub compile-expression ?x ?y)
        
        (&o/$jvm-imul ?x ?y)
        (&&host/compile-jvm-imul compile-expression ?x ?y)
        
        (&o/$jvm-idiv ?x ?y)
        (&&host/compile-jvm-idiv compile-expression ?x ?y)
        
        (&o/$jvm-irem ?x ?y)
        (&&host/compile-jvm-irem compile-expression ?x ?y)

        (&o/$jvm-ieq ?x ?y)
        (&&host/compile-jvm-ieq compile-expression ?x ?y)

        (&o/$jvm-ilt ?x ?y)
        (&&host/compile-jvm-ilt compile-expression ?x ?y)

        (&o/$jvm-igt ?x ?y)
        (&&host/compile-jvm-igt compile-expression ?x ?y)

        ;; Long arithmetic
        (&o/$jvm-ladd ?x ?y)
        (&&host/compile-jvm-ladd compile-expression ?x ?y)
        
        (&o/$jvm-lsub ?x ?y)
        (&&host/compile-jvm-lsub compile-expression ?x ?y)
        
        (&o/$jvm-lmul ?x ?y)
        (&&host/compile-jvm-lmul compile-expression ?x ?y)
        
        (&o/$jvm-ldiv ?x ?y)
        (&&host/compile-jvm-ldiv compile-expression ?x ?y)
        
        (&o/$jvm-lrem ?x ?y)
        (&&host/compile-jvm-lrem compile-expression ?x ?y)

        (&o/$jvm-leq ?x ?y)
        (&&host/compile-jvm-leq compile-expression ?x ?y)

        (&o/$jvm-llt ?x ?y)
        (&&host/compile-jvm-llt compile-expression ?x ?y)

        (&o/$jvm-lgt ?x ?y)
        (&&host/compile-jvm-lgt compile-expression ?x ?y)

        ;; Float arithmetic
        (&o/$jvm-fadd ?x ?y)
        (&&host/compile-jvm-fadd compile-expression ?x ?y)
        
        (&o/$jvm-fsub ?x ?y)
        (&&host/compile-jvm-fsub compile-expression ?x ?y)
        
        (&o/$jvm-fmul ?x ?y)
        (&&host/compile-jvm-fmul compile-expression ?x ?y)
        
        (&o/$jvm-fdiv ?x ?y)
        (&&host/compile-jvm-fdiv compile-expression ?x ?y)
        
        (&o/$jvm-frem ?x ?y)
        (&&host/compile-jvm-frem compile-expression ?x ?y)

        (&o/$jvm-feq ?x ?y)
        (&&host/compile-jvm-feq compile-expression ?x ?y)

        (&o/$jvm-flt ?x ?y)
        (&&host/compile-jvm-flt compile-expression ?x ?y)

        (&o/$jvm-fgt ?x ?y)
        (&&host/compile-jvm-fgt compile-expression ?x ?y)

        ;; Double arithmetic
        (&o/$jvm-dadd ?x ?y)
        (&&host/compile-jvm-dadd compile-expression ?x ?y)
        
        (&o/$jvm-dsub ?x ?y)
        (&&host/compile-jvm-dsub compile-expression ?x ?y)
        
        (&o/$jvm-dmul ?x ?y)
        (&&host/compile-jvm-dmul compile-expression ?x ?y)
        
        (&o/$jvm-ddiv ?x ?y)
        (&&host/compile-jvm-ddiv compile-expression ?x ?y)
        
        (&o/$jvm-drem ?x ?y)
        (&&host/compile-jvm-drem compile-expression ?x ?y)

        (&o/$jvm-deq ?x ?y)
        (&&host/compile-jvm-deq compile-expression ?x ?y)

        (&o/$jvm-dlt ?x ?y)
        (&&host/compile-jvm-dlt compile-expression ?x ?y)

        (&o/$jvm-dgt ?x ?y)
        (&&host/compile-jvm-dgt compile-expression ?x ?y)
        
        (&o/$jvm-null _)
        (&&host/compile-jvm-null compile-expression)

        (&o/$jvm-null? ?object)
        (&&host/compile-jvm-null? compile-expression ?object)
        
        (&o/$jvm-new ?class ?classes ?args)
        (&&host/compile-jvm-new compile-expression ?class ?classes ?args)

        (&o/$jvm-getstatic ?class ?field ?output-type)
        (&&host/compile-jvm-getstatic compile-expression ?class ?field ?output-type)

        (&o/$jvm-getfield ?class ?field ?object ?output-type)
        (&&host/compile-jvm-getfield compile-expression ?class ?field ?object ?output-type)

        (&o/$jvm-putstatic ?class ?field ?value ?input-type)
        (&&host/compile-jvm-putstatic compile-expression ?class ?field ?value ?input-type)

        (&o/$jvm-putfield ?class ?field ?value ?object ?input-type)
        (&&host/compile-jvm-putfield compile-expression ?class ?field ?object ?value ?input-type)

        (&o/$jvm-invokestatic ?class ?method ?classes ?args ?output-type)
        (&&host/compile-jvm-invokestatic compile-expression ?class ?method ?classes ?args ?output-type)

        (&o/$jvm-invokevirtual ?class ?method ?classes ?object ?args ?output-type)
        (&&host/compile-jvm-invokevirtual compile-expression ?class ?method ?classes ?object ?args ?output-type)

        (&o/$jvm-invokeinterface ?class ?method ?classes ?object ?args ?output-type)
        (&&host/compile-jvm-invokeinterface compile-expression ?class ?method ?classes ?object ?args ?output-type)

        (&o/$jvm-invokespecial ?class ?method ?classes ?object ?args ?output-type)
        (&&host/compile-jvm-invokespecial compile-expression ?class ?method ?classes ?object ?args ?output-type)
        
        (&o/$jvm-znewarray ?length)
        (&&host/compile-jvm-znewarray compile-expression ?length)

        (&o/$jvm-zastore ?array ?idx ?elem)
        (&&host/compile-jvm-zastore compile-expression ?array ?idx ?elem)

        (&o/$jvm-zaload ?array ?idx)
        (&&host/compile-jvm-zaload compile-expression ?array ?idx)

        (&o/$jvm-bnewarray ?length)
        (&&host/compile-jvm-bnewarray compile-expression ?length)

        (&o/$jvm-bastore ?array ?idx ?elem)
        (&&host/compile-jvm-bastore compile-expression ?array ?idx ?elem)

        (&o/$jvm-baload ?array ?idx)
        (&&host/compile-jvm-baload compile-expression ?array ?idx)

        (&o/$jvm-snewarray ?length)
        (&&host/compile-jvm-snewarray compile-expression ?length)

        (&o/$jvm-sastore ?array ?idx ?elem)
        (&&host/compile-jvm-sastore compile-expression ?array ?idx ?elem)

        (&o/$jvm-saload ?array ?idx)
        (&&host/compile-jvm-saload compile-expression ?array ?idx)

        (&o/$jvm-inewarray ?length)
        (&&host/compile-jvm-inewarray compile-expression ?length)

        (&o/$jvm-iastore ?array ?idx ?elem)
        (&&host/compile-jvm-iastore compile-expression ?array ?idx ?elem)

        (&o/$jvm-iaload ?array ?idx)
        (&&host/compile-jvm-iaload compile-expression ?array ?idx)

        (&o/$jvm-lnewarray ?length)
        (&&host/compile-jvm-lnewarray compile-expression ?length)

        (&o/$jvm-lastore ?array ?idx ?elem)
        (&&host/compile-jvm-lastore compile-expression ?array ?idx ?elem)

        (&o/$jvm-laload ?array ?idx)
        (&&host/compile-jvm-laload compile-expression ?array ?idx)

        (&o/$jvm-fnewarray ?length)
        (&&host/compile-jvm-fnewarray compile-expression ?length)

        (&o/$jvm-fastore ?array ?idx ?elem)
        (&&host/compile-jvm-fastore compile-expression ?array ?idx ?elem)

        (&o/$jvm-faload ?array ?idx)
        (&&host/compile-jvm-faload compile-expression ?array ?idx)

        (&o/$jvm-dnewarray ?length)
        (&&host/compile-jvm-dnewarray compile-expression ?length)

        (&o/$jvm-dastore ?array ?idx ?elem)
        (&&host/compile-jvm-dastore compile-expression ?array ?idx ?elem)

        (&o/$jvm-daload ?array ?idx)
        (&&host/compile-jvm-daload compile-expression ?array ?idx)

        (&o/$jvm-cnewarray ?length)
        (&&host/compile-jvm-cnewarray compile-expression ?length)

        (&o/$jvm-castore ?array ?idx ?elem)
        (&&host/compile-jvm-castore compile-expression ?array ?idx ?elem)

        (&o/$jvm-caload ?array ?idx)
        (&&host/compile-jvm-caload compile-expression ?array ?idx)

        (&o/$jvm-anewarray ?class ?length)
        (&&host/compile-jvm-anewarray compile-expression ?class ?length)

        (&o/$jvm-aastore ?array ?idx ?elem)
        (&&host/compile-jvm-aastore compile-expression ?array ?idx ?elem)

        (&o/$jvm-aaload ?array ?idx)
        (&&host/compile-jvm-aaload compile-expression ?array ?idx)

        (&o/$jvm-arraylength ?array)
        (&&host/compile-jvm-arraylength compile-expression ?array)

        (&o/$jvm-try ?body ?catches ?finally)
        (&&host/compile-jvm-try compile-expression ?body ?catches ?finally)

        (&o/$jvm-throw ?ex)
        (&&host/compile-jvm-throw compile-expression ?ex)

        (&o/$jvm-monitorenter ?monitor)
        (&&host/compile-jvm-monitorenter compile-expression ?monitor)

        (&o/$jvm-monitorexit ?monitor)
        (&&host/compile-jvm-monitorexit compile-expression ?monitor)

        (&o/$jvm-d2f ?value)
        (&&host/compile-jvm-d2f compile-expression ?value)

        (&o/$jvm-d2i ?value)
        (&&host/compile-jvm-d2i compile-expression ?value)

        (&o/$jvm-d2l ?value)
        (&&host/compile-jvm-d2l compile-expression ?value)
        
        (&o/$jvm-f2d ?value)
        (&&host/compile-jvm-f2d compile-expression ?value)

        (&o/$jvm-f2i ?value)
        (&&host/compile-jvm-f2i compile-expression ?value)

        (&o/$jvm-f2l ?value)
        (&&host/compile-jvm-f2l compile-expression ?value)
        
        (&o/$jvm-i2b ?value)
        (&&host/compile-jvm-i2b compile-expression ?value)

        (&o/$jvm-i2c ?value)
        (&&host/compile-jvm-i2c compile-expression ?value)

        (&o/$jvm-i2d ?value)
        (&&host/compile-jvm-i2d compile-expression ?value)

        (&o/$jvm-i2f ?value)
        (&&host/compile-jvm-i2f compile-expression ?value)

        (&o/$jvm-i2l ?value)
        (&&host/compile-jvm-i2l compile-expression ?value)

        (&o/$jvm-i2s ?value)
        (&&host/compile-jvm-i2s compile-expression ?value)

        (&o/$jvm-l2d ?value)
        (&&host/compile-jvm-l2d compile-expression ?value)

        (&o/$jvm-l2f ?value)
        (&&host/compile-jvm-l2f compile-expression ?value)

        (&o/$jvm-l2i ?value)
        (&&host/compile-jvm-l2i compile-expression ?value)

        (&o/$jvm-iand ?x ?y)
        (&&host/compile-jvm-iand compile-expression ?x ?y)

        (&o/$jvm-ior ?x ?y)
        (&&host/compile-jvm-ior compile-expression ?x ?y)

        (&o/$jvm-ixor ?x ?y)
        (&&host/compile-jvm-ixor compile-expression ?x ?y)

        (&o/$jvm-ishl ?x ?y)
        (&&host/compile-jvm-ishl compile-expression ?x ?y)

        (&o/$jvm-ishr ?x ?y)
        (&&host/compile-jvm-ishr compile-expression ?x ?y)

        (&o/$jvm-iushr ?x ?y)
        (&&host/compile-jvm-iushr compile-expression ?x ?y)

        (&o/$jvm-land ?x ?y)
        (&&host/compile-jvm-land compile-expression ?x ?y)

        (&o/$jvm-lor ?x ?y)
        (&&host/compile-jvm-lor compile-expression ?x ?y)

        (&o/$jvm-lxor ?x ?y)
        (&&host/compile-jvm-lxor compile-expression ?x ?y)

        (&o/$jvm-lshl ?x ?y)
        (&&host/compile-jvm-lshl compile-expression ?x ?y)

        (&o/$jvm-lshr ?x ?y)
        (&&host/compile-jvm-lshr compile-expression ?x ?y)

        (&o/$jvm-lushr ?x ?y)
        (&&host/compile-jvm-lushr compile-expression ?x ?y)

        (&o/$jvm-instanceof ?class ?object)
        (&&host/compile-jvm-instanceof compile-expression ?class ?object)

        _
        (assert false (prn-str 'compile-expression (&/adt->text syntax)))
        ))
    ))

(defn ^:private compile-token [syntax]
  (|case syntax
    (&o/$def ?name ?body)
    (&&lux/compile-def compile-expression ?name ?body)

    (&o/$declare-macro ?module ?name)
    (&&lux/compile-declare-macro compile-expression ?module ?name)

    (&o/$jvm-program ?body)
    (&&host/compile-jvm-program compile-expression ?body)
    
    (&o/$jvm-interface ?name ?supers ?anns ?methods)
    (&&host/compile-jvm-interface compile-expression ?name ?supers ?anns ?methods)

    (&o/$jvm-class ?name ?super-class ?interfaces ?anns ?fields ?methods ??env ??ctor-args)
    (&&host/compile-jvm-class compile-expression ?name ?super-class ?interfaces ?anns ?fields ?methods ??env ??ctor-args)

    _
    (compile-expression syntax)))

(defn ^:private eval! [expr]
  (&/with-eval
    (|do [module &/get-module-name
          id &/gen-id
          [file-name _ _] &/cursor
          :let [class-name (str (&host/->module-class module) "/" id)
                =class (doto (new ClassWriter ClassWriter/COMPUTE_MAXS)
                         (.visit &host/bytecode-version (+ Opcodes/ACC_PUBLIC Opcodes/ACC_SUPER)
                                 class-name nil "java/lang/Object" nil)
                         (-> (.visitField (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_STATIC) &/eval-field "Ljava/lang/Object;" nil nil)
                             (doto (.visitEnd)))
                         (.visitSource file-name nil))]
          _ (&/with-writer (.visitMethod =class Opcodes/ACC_PUBLIC "<clinit>" "()V" nil nil)
              (|do [^MethodVisitor *writer* &/get-writer
                    :let [_ (.visitCode *writer*)]
                    _ (compile-expression expr)
                    :let [_ (doto *writer*
                              (.visitFieldInsn Opcodes/PUTSTATIC class-name &/eval-field "Ljava/lang/Object;")
                              (.visitInsn Opcodes/RETURN)
                              (.visitMaxs 0 0)
                              (.visitEnd))]]
                (return nil)))
          :let [bytecode (.toByteArray (doto =class
                                         .visitEnd))]
          _ (&&/save-class! (str id) bytecode)
          loader &/loader]
      (-> (.loadClass ^ClassLoader loader (str (&host-generics/->class-name module) "." id))
          (.getField &/eval-field)
          (.get nil)
          return))))

(defn ^:private compile-module [name]
  (let [file-name (str name ".lux")]
    (|do [file-content (&&io/read-file file-name)
          :let [file-hash (hash file-content)]]
      (if (&&cache/cached? name)
        (&&cache/load name file-hash compile-module)
        (let [compiler-step (&optimizer/optimize eval! compile-module compile-token)]
          (|do [module-exists? (&a-module/exists? name)]
            (if module-exists?
              (fail "[Compiler Error] Can't redefine a module!")
              (|do [_ (&&cache/delete name)
                    _ (&a-module/enter-module name)
                    _ (&/flag-active-module name)
                    :let [=class (doto (new ClassWriter ClassWriter/COMPUTE_MAXS)
                                   (.visit &host/bytecode-version (+ Opcodes/ACC_PUBLIC Opcodes/ACC_SUPER)
                                           (str (&host/->module-class name) "/_") nil "java/lang/Object" nil)
                                   (-> (.visitField (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_STATIC) &/hash-field "I" nil file-hash)
                                       .visitEnd)
                                   (-> (.visitField (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_STATIC) &/compiler-field "Ljava/lang/String;" nil &&/version)
                                       .visitEnd)
                                   (.visitSource file-name nil))]
                    _ (if (= "lux" name)
                        &&host/compile-Function-class
                        (return nil))]
                (fn [state]
                  (|case ((&/with-writer =class
                            (&/exhaust% compiler-step))
                          (&/set$ &/$source (&reader/from name file-content) state))
                    (&/$Right ?state _)
                    (&/run-state (|do [defs &a-module/defs
                                       imports &a-module/imports
                                       tag-groups &&module/tag-groups
                                       :let [_ (doto =class
                                                 (-> (.visitField (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_STATIC) &/defs-field "Ljava/lang/String;" nil
                                                                  (->> defs
                                                                       (&/|map (fn [_def]
                                                                                 (|let [[?exported ?name ?ann] _def]
                                                                                   (str (if ?exported &&/exported-true &&/exported-false)
                                                                                        &&/exported-separator
                                                                                        ?name
                                                                                        &&/exported-separator
                                                                                        ?ann))))
                                                                       (&/|interpose &&/def-separator)
                                                                       (&/fold str "")))
                                                     .visitEnd)
                                                 (-> (.visitField (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_STATIC) &/imports-field "Ljava/lang/String;" nil
                                                                  (->> imports (&/|interpose &&/import-separator) (&/fold str "")))
                                                     .visitEnd)
                                                 (-> (.visitField (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_STATIC) &/tags-field "Ljava/lang/String;" nil
                                                                  (->> tag-groups
                                                                       (&/|map (fn [group]
                                                                                 (|let [[type tags] group]
                                                                                   (->> tags (&/|interpose &&/tag-separator) (&/fold str "")
                                                                                        (str type &&/type-separator)))))
                                                                       (&/|interpose &&/tag-group-separator)
                                                                       (&/fold str "")))
                                                     .visitEnd)
                                                 (.visitEnd))
                                             ]
                                       _ (&/flag-compiled-module name)]
                                   (&&/save-class! &/module-class-name (.toByteArray =class)))
                                 ?state)
                    
                    (&/$Left ?message)
                    (fail* ?message)))))))
        ))
    ))

(defn ^:private init! []
  (reset! !source->last-line {})
  (.mkdirs (java.io.File. &&/output-dir)))

;; [Resources]
(defn compile-program [program-module]
  (init!)
  (let [m-action (&/map% compile-module (&/|list "lux" program-module))]
    (|case (m-action (&/init-state nil))
      (&/$Right ?state _)
      (do (println "Compilation complete!")
        (&&cache/clean ?state)
        (&packager-program/package program-module))

      (&/$Left ?message)
      (assert false ?message))))