aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/reference.jvm.lux
blob: 9d0cc91e41fd31132d95d76db57312a855edf4d1 (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
(;module:
  lux
  (lux (control [monad #+ do])
       (data [text "text/" Hash<Text>]
             text/format)
       [macro "macro/" Monad<Meta>])
  (luxc ["&" lang]
        (lang [";L" host]
              (host ["$" jvm]
                    (jvm ["$t" type]
                         ["$i" inst]))
              ["ls" synthesis]
              [";L" variable #+ Variable]
              (translation [";T" common]))))

(do-template [<name> <prefix>]
  [(def: #export (<name> idx)
     (-> Nat Text)
     (|> idx nat-to-int %i (format <prefix>)))]

  [captured "c"]
  [partial  "p"]
  )

(def: #export (translate-captured variable)
  (-> Variable (Meta $;Inst))
  (do macro;Monad<Meta>
    [function-class hostL;context]
    (wrap (|>. ($i;ALOAD +0)
               ($i;GETFIELD function-class
                            (|> variable i.inc (i.* -1) int-to-nat captured)
                            commonT;$Object)))))

(def: #export (translate-local variable)
  (-> Variable (Meta $;Inst))
  (macro/wrap ($i;ALOAD (int-to-nat variable))))

(def: #export (translate-variable variable)
  (-> Variable (Meta $;Inst))
  (if (variableL;captured? variable)
    (translate-captured variable)
    (translate-local variable)))

(def: #export (translate-definition [def-module def-name])
  (-> Ident (Meta $;Inst))
  (let [bytecode-name (format def-module "/" (&;normalize-name def-name) (%n (text/hash def-name)))]
    (macro/wrap ($i;GETSTATIC bytecode-name commonT;value-field commonT;$Object))))