blob: 33742d694ebffea6dd0f00b333d13325d78a6ef3 (
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
(.module:
[lux #*
[cli (#+ program:)]
["." io (#+ IO io)]
[control
pipe
[monad (#+ do)]
["." exception (#+ exception:)]]
[data
["." maybe]
["." error (#+ Error)]
[number
["." i64]]
["." text ("#@." hash)
format]
[collection
["." array (#+ Array)]
["." list ("#@." functor)]]]
[macro
["." template]]
[world
["." file]]
["." host (#+ import: interface: do-to object)
["_" python]]
[tool
[compiler
["." name]
["." synthesis]
[phase
[macro (#+ Expander)]
["." generation
["." python
["." runtime]
["." extension]]]]
[default
["." platform (#+ Platform)]]]]]
[program
["/" compositor
["/." cli]]])
(import: #long java/lang/String)
(import: #long (java/lang/Class a)
(getCanonicalName [] java/lang/String))
(import: #long java/lang/Object
(new [])
(toString [] java/lang/String)
(getClass [] (java/lang/Class java/lang/Object)))
(import: #long java/lang/Integer
(longValue [] java/lang/Long))
(import: #long java/lang/Long
(intValue [] java/lang/Integer))
(import: #long java/lang/Number
(intValue [] java/lang/Integer)
(longValue [] long)
(doubleValue [] double))
(def: (inspect object)
(-> java/lang/Object Text)
(<| (case (host.check java/lang/Boolean object)
(#.Some value)
(%b value)
#.None)
(case (host.check java/lang/String object)
(#.Some value)
(%t value)
#.None)
(case (host.check java/lang/Long object)
(#.Some value)
(%i (.int value))
#.None)
(case (host.check java/lang/Number object)
(#.Some value)
(%f (java/lang/Number::doubleValue value))
#.None)
(case (host.check (Array java/lang/Object) object)
(#.Some value)
(let [value (:coerce (Array java/lang/Object) value)]
(case (array.read 0 value)
(^multi (#.Some tag)
[(host.check java/lang/Integer tag)
(#.Some tag)]
[[(array.read 1 value)
(array.read 2 value)]
[last?
(#.Some choice)]])
(let [last? (case last?
(#.Some _) #1
#.None #0)]
(|> (format (%n (.nat (java/lang/Integer::longValue tag)))
" " (%b last?)
" " (inspect choice))
(text.enclose ["(" ")"])))
_
(|> value
array.to-list
(list@map inspect)
(text.join-with " ")
(text.enclose ["[" "]"]))))
#.None)
(java/lang/Object::toString object)))
(import: #long org/python/core/PyType
(getName [] java/lang/String))
(import: #long org/python/core/PyNone)
(import: #long org/python/core/PyBoolean)
(import: #long org/python/core/PyInteger)
(import: #long org/python/core/PyLong)
(import: #long org/python/core/PyFloat)
(import: #long org/python/core/PyTuple)
(import: #long org/python/core/PyList)
(import: #long org/python/core/PyString
(new [java/lang/String]))
(import: #long org/python/core/PyObject
(asInt [] java/lang/Integer)
(asLong [] long)
(asDouble [] double)
(asString [] java/lang/String)
(__nonzero__ [] boolean)
(__getitem__ [int] #try org/python/core/PyObject)
(__getitem__ #as __getitem__dict [org/python/core/PyObject] #try org/python/core/PyObject)
(__len__ [] int)
(getType [] org/python/core/PyType))
(import: #long org/python/core/PyFunction
(__call__ [(Array org/python/core/PyObject)] org/python/core/PyObject))
(import: #long org/python/core/PyArray
(new [(java/lang/Class java/lang/Object) java/lang/Object])
(getArray [] java/lang/Object))
(import: #long org/python/util/PythonInterpreter
(new [])
(exec [String] #try void)
(eval [String] #try PyObject))
(type: Translator
(-> org/python/core/PyObject (Error Any)))
(def: (read-tuple read host-object)
(-> Translator Translator)
(let [size (|> host-object org/python/core/PyObject::__len__ .nat)]
(loop [idx 0
output (:coerce (Array Any) (array.new size))]
(if (n/< size idx)
(case (org/python/core/PyObject::__getitem__ (.int idx) host-object)
(#error.Failure error)
(#error.Failure error)
(#error.Success value)
(case (read value)
(#error.Failure error)
(#error.Failure error)
(#error.Success lux-value)
(recur (inc idx) (array.write idx lux-value output))))
(#error.Success output)))))
(def: python-type
(-> org/python/core/PyObject Text)
(|>> org/python/core/PyObject::getType org/python/core/PyType::getName (:coerce Text)))
(exception: (unknown-kind-of-object {object java/lang/Object})
(exception.report
["Object" (java/lang/Object::toString object)]))
(def: (read-variant read host-object)
(-> Translator Translator)
(case [(org/python/core/PyObject::__getitem__ +0 host-object)
(org/python/core/PyObject::__getitem__ +1 host-object)
(org/python/core/PyObject::__getitem__ +2 host-object)]
(^or [(#error.Failure error) _ _] [_ (#error.Failure error) _] [_ _ (#error.Failure error)])
(#error.Failure error)
(^multi [(#error.Success tag) (#error.Success flag) (#error.Success value)]
[(read tag)
(#error.Success tag)]
[(read value)
(#error.Success value)])
(#error.Success [tag
(: Any
(case (host.check org/python/core/PyNone flag)
(#.Some _)
(host.null)
#.None
synthesis.unit))
value])
_
(exception.throw ..unknown-kind-of-object host-object)))
(def: (read host-object)
Translator
(`` (<| (~~ (template [<class> <processing>]
[(case (host.check <class> host-object)
(#.Some host-object)
(#error.Success (<| <processing> host-object))
_)]
[org/python/core/PyNone (new> [] [])]
[org/python/core/PyBoolean org/python/core/PyObject::__nonzero__]
[org/python/core/PyInteger org/python/core/PyObject::asInt]
[org/python/core/PyLong org/python/core/PyObject::asLong]
[org/python/core/PyFloat org/python/core/PyObject::asDouble]
[org/python/core/PyString org/python/core/PyObject::asString]
[org/python/core/PyFunction (|>)]
[org/python/core/PyArray org/python/core/PyArray::getArray]
[(Array java/lang/Object) (|>)]
))
(~~ (template [<class> <processing>]
[(case (host.check <class> host-object)
(#.Some host-object)
(<| <processing> host-object)
_)]
[org/python/core/PyTuple (..read-variant read)]
[org/python/core/PyList (..read-tuple read)]
))
(exec (log! (java/lang/Class::getCanonicalName
(java/lang/Object::getClass
(:coerce java/lang/Object host-object))))
(log! (python-type host-object))
(exception.throw ..unknown-kind-of-object host-object)))))
(exception: (cannot-apply-a-non-function {object java/lang/Object})
(exception.report
["Object" (java/lang/Object::toString object)]))
(def: (ensure-macro macro)
(-> Macro (Maybe org/python/core/PyFunction))
(host.check org/python/core/PyFunction (:coerce java/lang/Object macro)))
(def: object-class
(java/lang/Class java/lang/Object)
(java/lang/Object::getClass (java/lang/Object::new)))
(def: to-host
(-> Any org/python/core/PyObject)
(|>> (:coerce java/lang/Object) (org/python/core/PyArray::new ..object-class)))
(def: (call-macro inputs lux macro)
(-> (List Code) Lux org/python/core/PyFunction (Error (Error [Lux (List Code)])))
(<| (:coerce (Error (Error [Lux (List Code)])))
..read
(org/python/core/PyFunction::__call__ (|> (host.array org/python/core/PyObject 2)
(host.array-write 0 (..to-host inputs))
(host.array-write 1 (..to-host lux)))
macro)))
(def: (expander macro inputs lux)
Expander
(case (ensure-macro macro)
(#.Some macro)
(case (call-macro inputs lux macro)
(#error.Success output)
(|> output
(:coerce org/python/core/PyObject)
..read
(:coerce (Error (Error [Lux (List Code)]))))
(#error.Failure error)
(#error.Failure error))
#.None
(exception.throw cannot-apply-a-non-function (:coerce java/lang/Object macro)))
)
(def: separator "___")
(type: Host
(generation.Host (_.Expression Any) (_.Statement Any)))
(def: host
(IO Host)
(io (let [interpreter (org/python/util/PythonInterpreter::new)
evaluate! (: (-> Text (_.Expression Any) (Error Any))
(function (evaluate! alias input)
(do error.monad
[output (org/python/util/PythonInterpreter::eval (_.code input) interpreter)]
(..read output))))
execute! (: (-> Text (_.Statement Any) (Error Any))
(function (execute! alias input)
(do error.monad
[_ (org/python/util/PythonInterpreter::exec (_.code input) interpreter)]
(wrap []))))]
(: Host
(structure
(def: evaluate! evaluate!)
(def: execute! execute!)
(def: (define! [module name] input)
(let [global (format (text.replace-all .module-separator ..separator module)
..separator (name.normalize name)
"___" (%n (text@hash name)))
@global (_.var global)]
(do error.monad
[#let [definition (_.set (list @global) input)]
_ (execute! global definition)
value (evaluate! global @global)]
(wrap [global value definition])))))))))
(def: platform
(IO (Platform IO _.SVar (_.Expression Any) (_.Statement Any)))
(do io.monad
[host ..host]
(wrap {#platform.&monad io.monad
#platform.&file-system file.system
#platform.host host
#platform.phase python.generate
#platform.runtime runtime.generate})))
(def: (program program)
(-> (_.Expression Any) (_.Statement Any))
($_ _.then
(_.import "sys")
(_.when (_.= (_.string "__main__") (_.var "__name__"))
(_.statement (_.apply/2 program
(runtime.lux//program-args (|> (_.var "sys") (_.the "argv")))
_.none)))))
(program: [{service /cli.service}]
(/.compiler ..expander
..platform
extension.bundle
..program
service))
|