blob: edca93d10054d6aad17caad1f0e9f3822f95a868 (
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
337
338
339
340
341
342
343
344
|
(.module:
lux
(lux (control ["ex" exception #+ exception:]
pipe
[monad #+ do])
(data [bit]
[maybe]
["e" error #+ Error]
[text "text/" Eq<Text>]
text/format
(coll [array]))
[macro]
[io #+ IO Process io]
[host #+ class: interface: object]
(world [file #+ File]))
(luxc [lang]
(lang [".L" variable #+ Register]
(host [js #+ JS Expression Statement]))
[".C" io]))
(do-template [<name>]
[(exception: #export (<name> {message Text})
message)]
[No-Active-Module-Buffer]
[Cannot-Execute]
[No-Anchor]
[Unknown-Member]
)
(host.import java/lang/Object
(toString [] String))
(host.import java/lang/String
(getBytes [String] #try (Array byte)))
(host.import java/lang/Integer
(longValue [] Long))
(host.import java/lang/Long
(intValue [] Integer))
(host.import java/lang/CharSequence)
(host.import java/lang/Appendable
(append [CharSequence] Appendable))
(host.import java/lang/StringBuilder
(new [])
(toString [] String))
(host.import javax/script/ScriptEngine
(eval [String] #try #? Object))
(host.import javax/script/ScriptEngineFactory
(getScriptEngine [] ScriptEngine))
(host.import jdk/nashorn/api/scripting/NashornScriptEngineFactory
(new []))
(host.import jdk/nashorn/api/scripting/NashornScriptEngine)
(host.import jdk/nashorn/api/scripting/JSObject)
(host.import jdk/nashorn/api/scripting/AbstractJSObject)
(host.import java/util/Arrays
(#static [t] copyOfRange [(Array t) int int] (Array t)))
(type: #export Anchor [Text Register])
(type: #export Host
{#context [Text Nat]
#anchor (Maybe Anchor)
#interpreter ScriptEngine
#module-buffer (Maybe StringBuilder)
#program-buffer StringBuilder
})
(def: #export init
(IO Host)
(io {#context ["" +0]
#anchor #.None
#interpreter (ScriptEngineFactory::getScriptEngine [] (NashornScriptEngineFactory::new []))
#module-buffer #.None
#program-buffer (StringBuilder::new [])}))
(def: #export module-js-name Text "module.js")
(def: #export init-module-buffer
(Meta Any)
(function (_ compiler)
(#e.Success [(update@ #.host
(|>> (:! Host)
(set@ #module-buffer (#.Some (StringBuilder::new [])))
(:! Nothing))
compiler)
[]])))
(def: #export (with-sub-context expr)
(All [a] (-> (Meta a) (Meta [Text a])))
(function (_ compiler)
(let [old (:! Host (get@ #.host compiler))
[old-name old-sub] (get@ #context old)
new-name (format old-name "$" (%i (nat-to-int old-sub)))]
(case (expr (set@ #.host
(:! Nothing (set@ #context [new-name +0] old))
compiler))
(#e.Success [compiler' output])
(#e.Success [(update@ #.host
(|>> (:! Host)
(set@ #context [old-name (n/inc old-sub)])
(:! Nothing))
compiler')
[new-name output]])
(#e.Error error)
(#e.Error error)))))
(def: #export context
(Meta Text)
(function (_ compiler)
(#e.Success [compiler
(|> (get@ #.host compiler)
(:! Host)
(get@ #context)
(let> [name sub]
name))])))
(def: #export (with-anchor anchor expr)
(All [a] (-> Anchor (Meta a) (Meta a)))
(function (_ compiler)
(let [old (:! Host (get@ #.host compiler))]
(case (expr (set@ #.host
(:! Nothing (set@ #anchor (#.Some anchor) old))
compiler))
(#e.Success [compiler' output])
(#e.Success [(update@ #.host
(|>> (:! Host)
(set@ #anchor (get@ #anchor old))
(:! Nothing))
compiler')
output])
(#e.Error error)
(#e.Error error)))))
(def: #export anchor
(Meta Anchor)
(function (_ compiler)
(case (|> compiler (get@ #.host) (:! Host) (get@ #anchor))
(#.Some anchor)
(#e.Success [compiler anchor])
#.None
((lang.throw No-Anchor "") compiler))))
(def: #export module-buffer
(Meta StringBuilder)
(function (_ compiler)
(case (|> compiler (get@ #.host) (:! Host) (get@ #module-buffer))
#.None
((lang.throw No-Active-Module-Buffer "") compiler)
(#.Some module-buffer)
(#e.Success [compiler module-buffer]))))
(def: #export program-buffer
(Meta StringBuilder)
(function (_ compiler)
(#e.Success [compiler (|> compiler (get@ #.host) (:! Host) (get@ #program-buffer))])))
(def: (execute code)
(-> Expression (Meta Any))
(function (_ compiler)
(case (|> compiler
(get@ #.host)
(:! Host)
(get@ #interpreter)
(ScriptEngine::eval [code]))
(#e.Error error)
((lang.throw Cannot-Execute error) compiler)
(#e.Success _)
(#e.Success [compiler []]))))
(def: (::toString js-object)
(-> Any JSObject)
(object [] AbstractJSObject []
[]
(AbstractJSObject (isFunction) boolean
true)
(AbstractJSObject (call [args (Array Object)]) Object
(Object::toString [] (:! Object js-object)))
))
(def: (::slice js-object value)
(-> (-> Object JSObject) (Array Object) JSObject)
(object [] AbstractJSObject []
[]
(AbstractJSObject (isFunction) boolean
true)
(AbstractJSObject (call [args (Array Object)]) Object
(:! Object
(js-object (Arrays::copyOfRange [value
(|> args (array.read +0) maybe.assume (:! Int))
(nat-to-int (array.size value))]))))
))
(def: #export int-high-field Text "H")
(def: #export int-low-field Text "L")
(def: #export variant-tag-field "_lux_tag")
(def: #export variant-flag-field "_lux_flag")
(def: #export variant-value-field "_lux_value")
(def: jvm-int
(-> Nat Integer)
(|>> (:! Long) (Long::intValue [])))
(def: low-mask
Nat
(|> +1 (bit.left-shift +32) n/dec))
(def: #export high (-> Nat Nat) (bit.logical-right-shift +32))
(def: #export low (-> Nat Nat) (bit.and low-mask))
(interface: IntValue
(getValue [] Long))
(host.import luxc/lang/translation/js/IntValue)
(def: (js-int value)
(-> Int JSObject)
(object [] AbstractJSObject [IntValue]
[]
## Methods
(IntValue (getValue) Long
(:! Long value))
(AbstractJSObject (getMember [member String]) Object
(cond (text/= int-high-field member)
(|> value int-to-nat high jvm-int)
(text/= int-low-field member)
(|> value int-to-nat low jvm-int)
## else
(error! (ex.construct Unknown-Member
(format " member = " member "\n"
"object(int) = " (%i value) "\n")))))))
(interface: StructureValue
(getValue [] (Array Object)))
(host.import luxc/lang/translation/js/StructureValue)
(def: (js-structure value)
(-> (Array Object) JSObject)
(object [] AbstractJSObject [StructureValue]
[]
## Methods
(StructureValue (getValue) (Array Object)
(:! (Array Object) value))
(AbstractJSObject (isArray) boolean
true)
(AbstractJSObject (getMember [member String]) Object
(cond (text/= "toString" member)
(:! Object
(::toString value))
(text/= "length" member)
(jvm-int (array.size value))
(text/= "slice" member)
(let [js-object (: (-> Object JSObject)
(|>> (cond> [(host.instance? (Array Object))]
[(:! (Array Object)) js-structure]
[(host.instance? Long)]
[(:! Int) js-int]
## else
[(:! JSObject)])))]
(:! Object
(::slice js-object value)))
## else
(error! (ex.construct Unknown-Member (format " member = " (:! Text member) "\n"
"object(structure) = " (Object::toString [] (:! Object value)) "\n")))))
(AbstractJSObject (getSlot [idx int]) Object
(|> value
(array.read (|> idx (Integer::longValue []) (:! Nat)))
maybe.assume
(cond> [(host.instance? (Array Object))]
[(:! (Array Object)) js-structure]
[(host.instance? Long)]
[(:! Int) js-int]
## else
[(:! JSObject)])
(:! Object)))
))
(def: #export unit Text "")
(def: (module-name module)
(-> Text Text)
(|> module
(text.replace-all "/" "$")
(text.replace-all "-" "_")))
(def: #export (definition-name [module name])
(-> Ident Text)
(format (module-name module) "$" (lang.normalize-name name)))
(def: #export (save-js code)
(-> JS (Meta Any))
(do macro.Monad<Meta>
[module-buffer module-buffer
#let [_ (Appendable::append [(:! CharSequence code)]
module-buffer)]]
(execute code)))
(def: #export (save-definition name code)
(-> Ident Expression (Meta Any))
(save-js (format "var " (definition-name name) " = " code ";\n")))
(def: #export (save-module! target)
(-> File (Meta (Process Any)))
(do macro.Monad<Meta>
[module macro.current-module-name
module-buffer module-buffer
program-buffer program-buffer
#let [module-code (StringBuilder::toString [] module-buffer)
_ (Appendable::append [(:! CharSequence (format module-code "\n"))]
program-buffer)]]
(wrap (ioC.write target
(format (module-name module) "/" module-js-name)
(|> module-code
(String::getBytes ["UTF-8"])
e.assume)))))
|