blob: b2319af7e6d57f7eb21a6675d27e95045ef7ec7b (
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
|
(.module:
[library
[lux {"-" [Definition]}
[program {"+" [program:]}]
["@" target]
["[0]" ffi {"+" [import:]}]
[abstract
[monad {"+" [do]}]]
[control
["[0]" io {"+" [IO]}]
["[0]" try {"+" [Try]}]
[concurrency
["[0]" async {"+" [Async]}]]]
[data
["[0]" product]
[text
["%" format {"+" [format]}]]
[collection
[array {"+" [Array]}]
["[0]" dictionary]]]
["[0]" world "_"
["[0]" file]
["[1]/[0]" program]]
[target
["[0]" jvm "_"
[bytecode {"+" [Bytecode]}]
["[1]/[0]" type
["[1]/[0]" box]]]]
[tool
[compiler
[reference {"+" []}]
["[0]" phase]
[default
["[0]" platform {"+" [Platform]}]]
[meta
[archive {"+" [Archive]}]
["[0]" packager "_"
["[1]" jvm]]]
[language
[lux
["$" synthesis {"+" [Synthesis]}]
["[0]" generation]
[analysis
[macro {"+" [Expander]}]]
[phase
[extension {"+" [Phase Bundle Operation Handler Extender]}
["[0]" analysis "_"
["[1]" jvm]]
... ["[0]" generation "_"
... ["[1]" jvm]]
... ["[0]" directive "_"
... ["[1]" jvm]]
]
[generation
["[0]" jvm "_"
["[1]/[0]" runtime]
... ["[1]/[0]" host]
]]]]]]]]]
[program
["/" compositor
["/[0]" cli]
["/[0]" static]]]
[luxc
[lang
[host
["_" jvm]]
["[0]" directive "_"
["[1]" jvm]]
[translation
["[0]" jvm
["[0]" runtime]
["[0]" expression]
["[0]" function]
["[1]/[0]" program]
["translation" extension]]]]])
(import: java/lang/reflect/Method
["[1]::[0]"
(invoke [java/lang/Object [java/lang/Object]] "try" java/lang/Object)])
(import: java/lang/ClassLoader)
(import: (java/lang/Class c)
["[1]::[0]"
(getMethod [java/lang/String [(java/lang/Class java/lang/Object)]] "try" java/lang/reflect/Method)])
(import: java/lang/Object
["[1]::[0]"
(getClass [] (java/lang/Class java/lang/Object))])
(def: _object_class
(java/lang/Class java/lang/Object)
(ffi.class_for java/lang/Object))
(def: _apply1_args
(Array (java/lang/Class java/lang/Object))
(|> (ffi.array (java/lang/Class java/lang/Object) 1)
(ffi.write! 0 _object_class)))
(def: _apply2_args
(Array (java/lang/Class java/lang/Object))
(|> (ffi.array (java/lang/Class java/lang/Object) 2)
(ffi.write! 0 _object_class)
(ffi.write! 1 _object_class)))
(def: _apply5_args
(Array (java/lang/Class java/lang/Object))
(|> (ffi.array (java/lang/Class java/lang/Object) 5)
(ffi.write! 0 _object_class)
(ffi.write! 1 _object_class)
(ffi.write! 2 _object_class)
(ffi.write! 3 _object_class)
(ffi.write! 4 _object_class)))
(def: .public (expander macro inputs lux)
Expander
(do try.monad
[apply_method (|> macro
(:as java/lang/Object)
(java/lang/Object::getClass)
(java/lang/Class::getMethod "apply" _apply2_args))]
(:as (Try (Try [Lux (List Code)]))
(java/lang/reflect/Method::invoke
(:as java/lang/Object macro)
(|> (ffi.array java/lang/Object 2)
(ffi.write! 0 (:as java/lang/Object inputs))
(ffi.write! 1 (:as java/lang/Object lux)))
apply_method))))
(def: how_to_wrap_a_phase
Synthesis
(let [java/lang/String (jvm/type.class "java.lang.String" (list))
<java/lang/Class> (jvm/type.array (jvm/type.class "java.lang.Class" (list)))
java/lang/Object (jvm/type.class "java.lang.Object" (list))
<java/lang/Object> (jvm/type.array java/lang/Object)
jvm_type (: (All (_ c) (-> (jvm/type.Type c) Synthesis))
(|>> jvm/type.format
$.text))
class_type (: (-> Text Synthesis)
(function (_ name)
(|> (jvm/type.class name (list))
jvm_type)))
unwrap_long (: (-> Synthesis Synthesis)
(|>> (list ($.text jvm/type/box.long)
($.text "long"))
(#$.Extension "jvm object cast")))
long_to_int (: (-> Synthesis Synthesis)
(|>> (list)
(#$.Extension "jvm conversion long-to-int")))
literal_nat (: (-> Nat Synthesis)
(|>> .i64 $.i64 unwrap_long long_to_int))
write! (: (-> Text Nat Synthesis Synthesis Synthesis)
(function (_ element_class index value array)
((#$.Extension "jvm array write object"
(list (jvm_type (jvm/type.array (jvm/type.class element_class (list))))
(literal_nat index)
value
array)))))
object_array (: (-> Text Nat Synthesis)
(function (_ class_name size)
(#$.Extension "jvm array new object"
(list (class_type class_name)
(literal_nat size)))))
class_of (: (-> Synthesis Synthesis)
(function (_ object)
(#$.Extension "jvm member invoke virtual"
(list& (class_type "java.lang.Object")
($.text "getClass")
(class_type "java.lang.Class")
object
(list)))))
input (: (All (_ c) (-> (jvm/type.Type c) Synthesis Synthesis))
(function (_ value_type value)
($.tuple (list (jvm_type value_type) value))))
example_object (#$.Extension "jvm member invoke constructor"
(list& (class_type "java.lang.Object")
(list)))
phase_arity 3
$phase ($.variable/local 1)
$archive ($.variable/local 2)
$input ($.variable/local 3)
$state ($.variable/local 4)
apply_method (#$.Extension "jvm member invoke virtual"
(list& (class_type "java.lang.Class")
($.text "getMethod")
(class_type "java.lang.reflect.Method")
(class_of $phase)
(list (input java/lang/String
($.text runtime.apply_method))
(input <java/lang/Class>
(|> (object_array "java.lang.Class" phase_arity)
(write! "java.lang.Class" 0 (class_of example_object))
(write! "java.lang.Class" 1 (class_of example_object))
(write! "java.lang.Class" 2 (class_of example_object)))))))]
(#$.Extension "jvm member invoke virtual"
(list& (class_type "java.lang.reflect.Method")
($.text "invoke")
(class_type "java.lang.Object")
apply_method
(list (input java/lang/Object
$phase)
(input <java/lang/Object>
(|> (object_array "java.lang.Object" phase_arity)
(write! "java.lang.Object" 0 $archive)
(write! "java.lang.Object" 1 $input)
(write! "java.lang.Object" 2 $state))))))))
(def: (phase_wrapper archive)
(-> Archive (generation.Operation _.Anchor _.Inst _.Definition phase.Wrapper))
(do phase.monad
[instanceG (function.function' (#.Some [0 (.nat -1)]) expression.translate archive [(list) 4 ..how_to_wrap_a_phase])
phase_wrapper (generation.evaluate! [0 (.nat -2)] instanceG)]
(in (function (_ phase)
(<| try.trusted
(: (Try java/lang/Object))
(do try.monad
[apply_method (|> phase_wrapper
(:as java/lang/Object)
(java/lang/Object::getClass)
(java/lang/Class::getMethod runtime.apply_method _apply1_args))]
(java/lang/reflect/Method::invoke
(:as java/lang/Object phase_wrapper)
(|> (ffi.array java/lang/Object 1)
(ffi.write! 0 (:as java/lang/Object phase)))
apply_method)))))))
(def: .public platform
... (IO (Platform Anchor (Bytecode Any) Definition))
(IO [java/lang/ClassLoader
(Platform _.Anchor _.Inst _.Definition)])
(do io.monad
[... host jvm/host.host
[loader host] jvm.host]
(in [loader
[#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.phase_wrapper ..phase_wrapper
#platform.write product.right]])))
(def: (extender phase_wrapper)
(-> phase.Wrapper Extender)
... TODO: Stop relying on coercions ASAP.
(<| (:as Extender)
(function (@self handler))
(:as Handler)
(function (@self name phase))
(:as Phase)
(function (@self archive parameters))
(:as Operation)
(function (@self state))
(:as Try)
try.trusted
(:as Try)
(do try.monad
[method (|> handler
(:as java/lang/Object)
(java/lang/Object::getClass)
(java/lang/Class::getMethod "apply" _apply5_args))]
(java/lang/reflect/Method::invoke
(:as java/lang/Object handler)
(|> (ffi.array java/lang/Object 5)
(ffi.write! 0 (:as java/lang/Object name))
(ffi.write! 1 (:as java/lang/Object (phase_wrapper phase)))
(ffi.write! 2 (:as java/lang/Object archive))
(ffi.write! 3 (:as java/lang/Object parameters))
(ffi.write! 4 (:as java/lang/Object state)))
method))))
(def: (declare_success! _)
(-> Any (Async Any))
(async.future (\ world/program.default exit +0)))
(program: [service /cli.service]
(let [static [#/static.host @.jvm
#/static.host_module_extension ".jvm"
#/static.target (/cli.target service)
#/static.artifact_extension ".class"]]
(exec (do async.monad
[[loader platform] (async.future ..platform)
_ (/.compiler [#/static.host @.jvm
#/static.host_module_extension ".jvm"
#/static.target (/cli.target service)
#/static.artifact_extension ".class"]
..expander
(analysis.bundle loader)
(io.io platform)
... generation.bundle
translation.bundle
(|>> ..extender (directive.bundle loader))
(jvm/program.program jvm/runtime.class_name)
[_.Anchor _.Inst _.Definition]
..extender
service
[(packager.package static)
(format (/cli.target service) (\ file.default separator) "program.jar")])]
(..declare_success! []))
(io.io []))))
|