aboutsummaryrefslogtreecommitdiff
path: root/lux-jvm/source/program.lux
blob: 65cff6d00e93968375d1e836ac0678df7c82ae01 (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
(.module:
  [lux (#- Definition)
   [program (#+ program:)]
   ["@" target]
   ["." host (#+ import:)]
   [abstract
    [monad (#+ do)]]
   [control
    ["." io (#+ IO)]
    ["." try (#+ Try)]
    [concurrency
     ["." promise (#+ Promise)]]]
   [data
    ["." product]
    [text
     ["%" format (#+ format)]]
    [collection
     [array (#+ Array)]
     ["." dictionary]]]
   ["." world #_
    ["." file]
    ["#/." program]]
   [target
    [jvm
     [bytecode (#+ Bytecode)]]]
   [tool
    [compiler
     [default
      ["." platform (#+ Platform)]]
     [meta
      ["." packager #_
       ["#" jvm]]]
     [language
      [lux
       [analysis
        [macro (#+ Expander)]]
       [phase
        [extension (#+ Phase Bundle Operation Handler Extender)
         ["." analysis #_
          ["#" jvm]]
         ## ["." generation #_
         ##  ["#" jvm]]
         ## ["." directive #_
         ##  ["#" jvm]]
         ]
        [generation
         ["." jvm #_
          ["#/." runtime]
          ## ["#/." host]
          ]]]]]]]]
  [program
   ["/" compositor
    ["/." cli]
    ["/." static]]]
  [luxc
   [lang
    [host
     ["_" jvm]]
    ["." directive #_
     ["#" jvm]]
    [translation
     ["." jvm
      ["." runtime]
      ["." expression]
      ["#/." program]
      ["translation" extension]]]]])

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

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

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

(def: _object_class
  (java/lang/Class java/lang/Object)
  (host.class_for java/lang/Object))

(def: _apply2_args
  (Array (java/lang/Class java/lang/Object))
  (|> (host.array (java/lang/Class java/lang/Object) 2)
      (host.array_write 0 _object_class)
      (host.array_write 1 _object_class)))

(def: _apply5_args
  (Array (java/lang/Class java/lang/Object))
  (|> (host.array (java/lang/Class java/lang/Object) 5)
      (host.array_write 0 _object_class)
      (host.array_write 1 _object_class)
      (host.array_write 2 _object_class)
      (host.array_write 3 _object_class)
      (host.array_write 4 _object_class)))

(def: #export (expander macro inputs lux)
  Expander
  (do try.monad
    [apply_method (|> macro
                      (:coerce java/lang/Object)
                      (java/lang/Object::getClass)
                      (java/lang/Class::getMethod "apply" _apply2_args))]
    (:coerce (Try (Try [Lux (List Code)]))
             (java/lang/reflect/Method::invoke
              (:coerce java/lang/Object macro)
              (|> (host.array java/lang/Object 2)
                  (host.array_write 0 (:coerce java/lang/Object inputs))
                  (host.array_write 1 (:coerce java/lang/Object lux)))
              apply_method))))

(def: #export platform
  ## (IO (Platform Anchor (Bytecode Any) Definition))
  (IO (Platform _.Anchor _.Inst _.Definition))
  (do io.monad
    [## host jvm/host.host
     host jvm.host]
    (wrap {#platform.&file_system (file.async file.default)
           #platform.host host
           ## #platform.phase jvm.generate
           #platform.phase expression.translate
           ## #platform.runtime runtime.generate
           #platform.runtime runtime.translate
           #platform.write product.right})))

(def: extender
  Extender
  ## TODO: Stop relying on coercions ASAP.
  (<| (:coerce Extender)
      (function (@self handler))
      (:coerce Handler)
      (function (@self name phase))
      (:coerce Phase)
      (function (@self archive parameters))
      (:coerce Operation)
      (function (@self state))
      (:coerce Try)
      try.assume
      (:coerce Try)
      (do try.monad
        [method (|> handler
                    (:coerce java/lang/Object)
                    (java/lang/Object::getClass)
                    (java/lang/Class::getMethod "apply" _apply5_args))]
        (java/lang/reflect/Method::invoke
         (:coerce java/lang/Object handler)
         (|> (host.array java/lang/Object 5)
             (host.array_write 0 (:coerce java/lang/Object name))
             (host.array_write 1 (:coerce java/lang/Object phase))
             (host.array_write 2 (:coerce java/lang/Object archive))
             (host.array_write 3 (:coerce java/lang/Object parameters))
             (host.array_write 4 (:coerce java/lang/Object state)))
         method))))

(def: (declare_success! _)
  (-> Any (Promise Any))
  (promise.future (\ world/program.default exit +0)))

(program: [{service /cli.service}]
  (exec (do promise.monad
          [_ (/.compiler {#/static.host @.jvm
                          #/static.host_module_extension ".jvm"
                          #/static.target (/cli.target service)
                          #/static.artifact_extension ".class"}
                         ..expander
                         analysis.bundle
                         ..platform
                         ## generation.bundle
                         translation.bundle
                         (directive.bundle ..extender)
                         (jvm/program.program jvm/runtime.class_name)
                         [_.Anchor _.Inst _.Definition]
                         ..extender
                         service
                         [packager.package
                          (format (/cli.target service) (\ file.default separator) "program.jar")])]
          (..declare_success! []))
    (io.io [])))