aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/program.lux
blob: 9f5627ee34d3827ddc352cbf7a2a41265b5e8d67 (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
(.module:
  [lux #*
   ["@" host (#+ import:)]
   [abstract
    [monad (#+ do)]]
   [control
    ["." io (#+ IO)]
    [parser
     [cli (#+ program:)]]]
   [data
    ["." error (#+ Error)]
    [collection
     [array (#+ Array)]
     ["." dictionary]]]
   [world
    ["." file]]
   [target
    [jvm
     ["$t" type]]]
   [tool
    [compiler
     [phase
      ["." macro (#+ Expander)]]
     [default
      ["." platform (#+ Platform)]]]]]
  [program
   ["/" compositor
    ["/." cli]]]
  [luxc
   [lang
    [host
     ["_" jvm
      ["$d" def]
      ["$i" inst]]]
    [statement
     [".S" jvm]]
    [translation
     ["." jvm
      ["." runtime]
      ["." expression]
      [procedure
       ["." common]
       ["." host]]]]]])

(import: #long java/lang/reflect/Method
  (invoke [java/lang/Object [java/lang/Object]] #try java/lang/Object))

(import: #long (java/lang/Class c)
  (getMethod [java/lang/String [(java/lang/Class java/lang/Object)]] #try java/lang/reflect/Method))

(import: #long java/lang/Object
  (getClass [] (java/lang/Class java/lang/Object)))

(def: _object-class
  (java/lang/Class java/lang/Object)
  (@.class-for java/lang/Object))

(def: _apply-args
  (Array (java/lang/Class java/lang/Object))
  (|> (@.array (java/lang/Class java/lang/Object) 2)
      (@.array-write 0 _object-class)
      (@.array-write 1 _object-class)))

(def: #export (expander macro inputs lux)
  Expander
  (do error.monad
    [apply-method (|> macro
                      (:coerce java/lang/Object)
                      (java/lang/Object::getClass)
                      (java/lang/Class::getMethod "apply" _apply-args))]
    (:coerce (Error (Error [Lux (List Code)]))
             (java/lang/reflect/Method::invoke
              (:coerce java/lang/Object macro)
              (|> (@.array java/lang/Object 2)
                  (@.array-write 0 (:coerce java/lang/Object inputs))
                  (@.array-write 1 (:coerce java/lang/Object lux)))
              apply-method))))

(def: #export jvm
  (IO (Platform IO _.Anchor _.Inst _.Definition))
  (do io.monad
    [host jvm.host]
    (wrap {#platform.&monad io.monad
           #platform.&file-system file.system
           #platform.host host
           #platform.phase expression.translate
           #platform.runtime runtime.translate})))

(def: #export (program programI)
  (-> _.Inst _.Definition)
  (let [nilI runtime.noneI
        num-inputsI (|>> ($i.ALOAD 0) $i.ARRAYLENGTH)
        decI (|>> ($i.int +1) $i.ISUB)
        headI (|>> $i.DUP
                   ($i.ALOAD 0)
                   $i.SWAP
                   $i.AALOAD
                   $i.SWAP
                   $i.DUP_X2
                   $i.POP)
        pairI (|>> ($i.int +2)
                   ($i.ANEWARRAY "java.lang.Object")
                   $i.DUP_X1
                   $i.SWAP
                   ($i.int +0)
                   $i.SWAP
                   $i.AASTORE
                   $i.DUP_X1
                   $i.SWAP
                   ($i.int +1)
                   $i.SWAP
                   $i.AASTORE)
        consI (|>> ($i.int +1)
                   ($i.string "")
                   $i.DUP2_X1
                   $i.POP2
                   runtime.variantI)
        prepare-input-listI (<| $i.with-label (function (_ @loop))
                                $i.with-label (function (_ @end))
                                (|>> nilI
                                     num-inputsI
                                     ($i.label @loop)
                                     decI
                                     $i.DUP
                                     ($i.IFLT @end)
                                     headI
                                     pairI
                                     consI
                                     $i.SWAP
                                     ($i.GOTO @loop)
                                     ($i.label @end)
                                     $i.POP
                                     ($i.ASTORE 0)))
        run-ioI (|>> ($i.CHECKCAST jvm.function-class)
                     $i.NULL
                     ($i.INVOKEVIRTUAL jvm.function-class runtime.apply-method (runtime.apply-signature 1) #0))
        main-type ($t.method (list ($t.array 1 ($t.class "java.lang.String" (list))))
                             #.None
                             (list))
        bytecode-name "_"]
    [bytecode-name
     ($d.class #_.V1_6
               #_.Public _.finalC
               bytecode-name
               (list) ["java.lang.Object" (list)]
               (list)
               (|>> ($d.method #_.Public _.staticM "main" main-type
                               (|>> prepare-input-listI
                                    programI
                                    run-ioI
                                    $i.POP
                                    $i.RETURN))))]))

(def: #export bundle
  _.Bundle
  (dictionary.merge common.bundle
                    host.bundle))

(program: [{service /cli.service}]
  (/.compiler ..expander
              ..jvm
              ..bundle
              jvmS.bundle
              ..program
              service))