aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/compiler/common.jvm.lux
blob: bd5487ef601500df3a8f90141c3bc79d7f92f22a (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
(;module:
  lux
  (lux (concurrency ["A" atom])
       (data ["R" result]
             (coll ["D" dict]))
       [macro]
       [host #+ jvm-import]))

## [Host]
(jvm-import org.objectweb.asm.MethodVisitor
  (visitLdcInsn [Object] void))

(jvm-import java.lang.ClassLoader)

## [Types]
(type: #export Compiled
  Unit)

(type: #export Blob host;Byte-Array)

(type: #export Class-Store (A;Atom (D;Dict Text Blob)))

(type: #export Host
  {#visitor (Maybe MethodVisitor)
   #loader ClassLoader
   #store Class-Store})

(def: #export unit-value Text "\u0000unit\u0000")

(def: (visitor::get compiler)
  (-> Compiler (Maybe MethodVisitor))
  (|> (get@ #;host compiler)
      (:! Host)
      (get@ #visitor)))

(def: (visitor::put ?visitor compiler)
  (-> (Maybe MethodVisitor) Compiler Compiler)
  (update@ #;host
           (function [host]
             (|> host
                 (:! Host)
                 (set@ #visitor ?visitor)
                 (:! Void)))
           compiler))

(def: #export get-visitor
  (Lux MethodVisitor)
  (function [compiler]
    (case (visitor::get compiler)
      #;None
      (#R;Error "No visitor has been set.")

      (#;Some visitor)
      (#R;Success [compiler visitor]))))

(def: #export (with-visitor visitor body)
  (All [a] (-> MethodVisitor (Lux a) (Lux a)))
  (function [compiler]
    (case (macro;run' (visitor::put (#;Some visitor) compiler) body)
      (#R;Error error)
      (#R;Error error)

      (#R;Success [compiler' output])
      (#R;Success [(visitor::put (visitor::get compiler) compiler')
                   output]))))