blob: 7d1f709a42042e73d8173e600222285b8cf00383 (
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
(.using
[library
[lux {"-" Symbol Alias Global global function}
["@" target]
["[0]" meta]
[abstract
[monad {"+" do}]]
[control
["[0]" io]
["[0]" maybe ("[1]#[0]" functor)]
["<>" parser ("[1]#[0]" monad)
["<[0]>" code {"+" Parser}]]]
[data
["[0]" text ("[1]#[0]" equivalence)
["%" format]]
[collection
["[0]" list ("[1]#[0]" functor mix)]]]
[macro {"+" with_symbols}
[syntax {"+" syntax:}]
["[0]" code]
["[0]" template]]
[type
abstract]]])
(with_expansions [<constant> (for [@.js "js constant"
@.python "python constant"
@.lua "lua constant"
@.ruby "ruby constant"])
<apply> (for [@.js "js apply"
@.python "python apply"
@.lua "lua apply"
@.ruby "ruby apply"])
<new> (for [@.js "js object new"
@.python "python apply"]
(as_is))
<do> (for [@.js "js object do"
@.python "python object do"
@.lua "lua object do"
@.ruby "ruby object do"])
<get> (for [@.js "js object get"
@.python "python object get"
@.lua "lua object get"
@.ruby "ruby object get"]
(as_is))
<import> (for [@.python "python import"
@.lua "lua import"
@.ruby "ruby import"]
(as_is))
<function> (for [@.js "js function"
@.python "python function"
@.lua "lua function"]
(as_is))]
(abstract: .public (Object brand) Any)
(with_expansions [<un_common> (for [@.js (as_is [Symbol]
[Null]
[Undefined])
@.python (as_is [None]
[Dict])
@.lua (as_is [Nil]
[Table])
@.ruby (as_is [Nil])])
<un_common> <un_common>]
(template [<name>]
[(with_expansions [<brand> (template.symbol [<name> "'"])]
(abstract: <brand>
Any
(type: .public <name>
(Object <brand>))))]
[Function]
<un_common>
))
(with_expansions [<un_common> (for [@.js (as_is [Number Frac])
@.python (as_is [Integer Int]
[Float Frac])
@.lua (as_is [Integer Int]
[Float Frac])
@.ruby (as_is [Integer Int]
[Float Frac])])
<un_common> <un_common>]
(template [<name> <type>]
[(type: .public <name>
<type>)]
[Boolean Bit]
[String Text]
<un_common>
))
(type: Alias
(Maybe Text))
(def: alias
(Parser Alias)
(<>.maybe (<>.after (<code>.this! (' "as")) <code>.local_symbol)))
(type: Optional
(Record
[#optional? Bit
#mandatory Code]))
(def: optional
(Parser Optional)
(let [token "?"]
(<| (<>.and (<>.parses? (<code>.text! token)))
(<>.after (<>.not (<code>.text! token)))
<code>.any)))
(type: (Named a)
(Record
[#name Text
#alias Alias
#anonymous a]))
(template [<case> <name>]
[(def: <case>
(All (_ a) (-> (Parser a) (Parser (Named a))))
(|>> ($_ <>.and
<name>
..alias
)))]
[named <code>.local_symbol]
[anonymous (<>#in "")]
)
(type: Output
Optional)
(def: output
(Parser Output)
..optional)
(type: Global
(Named Output))
(def: variables
(Parser (List Text))
(<>.else (list) (<code>.tuple (<>.some <code>.local_symbol))))
(def: (generalized $ it)
(All (_ a)
(-> (-> (List Text) a a)
(-> (Parser a) (Parser a))))
(do <>.monad
[variables ..variables
it it]
(in ($ variables it))))
(type: Input
(Record
[#variables (List Text)
#parameters (List Optional)
#io? Bit
#try? Bit]))
(def: input
(Parser Input)
($_ <>.and
(<>#in (list))
(<code>.tuple (<>.some ..optional))
(<>.parses? (<code>.text! "io"))
(<>.parses? (<code>.text! "try"))))
(type: Constructor
(Named Input))
(def: constructor
(Parser Constructor)
(<| <code>.form
(..generalized (with@ [#anonymous #variables]))
(<>.after (<code>.this! (' new)))
(..anonymous ..input)))
(type: (Member a)
(Record
[#static? Bit
#member a]))
(def: static!
(Parser Any)
(<code>.text! "static"))
(def: (member it)
(All (_ a)
(-> (Parser a)
(Parser (Member a))))
(do [! <>.monad]
[static? (<>.parses? ..static!)]
(# ! each
(|>> [#static? static?
#member])
it)))
(type: Field
(Member (Named Output)))
(def: field
(Parser Field)
(<| <code>.form
..member
..named
..output))
(type: Procedure
(Record
[#input Input
#output Optional]))
(def: procedure
(Parser (Named Procedure))
(<| (..generalized (with@ [#anonymous #input #variables]))
..named
($_ <>.and
..input
..optional
)))
(type: Method
(Member (Named Procedure)))
(def: method
(Parser Method)
(<| <code>.form
..member
..procedure))
(`` (`` (type: Sub
(Variant
(~~ (for [@.lua (~~ (as_is))
@.ruby (~~ (as_is))]
{#Constructor Constructor}))
{#Field Field}
{#Method Method}))))
(`` (`` (def: sub
(Parser Sub)
($_ <>.or
(~~ (for [@.lua (~~ (as_is))
@.ruby (~~ (as_is))]
..constructor))
..field
..method
))))
(def: parameters
(-> (List Optional) (List Optional))
(|>> list.enumeration
(list#each (.function (_ [idx [optional? type]])
[#optional? optional?
#mandatory (|> idx %.nat code.local_symbol)]))))
(def: (output_type it)
(-> Optional Code)
(if (value@ #optional? it)
(` (.Maybe (~ (value@ #mandatory it))))
(value@ #mandatory it)))
(`` (template [<lux_it> <host_it>
<lux_?> <host_?>]
[(def: .public (<lux_it> _)
(-> Any Nothing)
(:expected (<host_it>)))
(def: .public <lux_?>
(-> Any Bit)
(|>> <host_?>))
(template.with_locals [g!it]
(as_is (def: g!it' (' g!it))
(def: (host_optional it)
(-> Optional Code)
(.if (.value@ #optional? it)
(` (.case (~ (value@ #mandatory it))
{.#Some (~ g!it')}
(~ g!it')
{.#None}
(<host_it>)))
(value@ #mandatory it)))
(def: (lux_optional it output)
(-> Optional Code Code)
(` (.let [(~ g!it') (~ output)]
(~ (if (value@ #optional? it)
(` (.if (<host_?> (~ g!it'))
{.#None}
{.#Some (~ g!it')}))
(` (.if (.not (<host_?> (~ g!it')))
(~ g!it')
(.panic! "Invalid output."))))))))))]
(~~ (for [@.js [null "js object null"
null? "js object null?"]
@.python [none "python object none"
none? "python object none?"]
@.lua [nil "lua object nil"
nil? "lua object nil?"]
@.ruby [nil "ruby object nil"
nil? "ruby object nil?"]]))
))
(type: Declaration
[Text (List Text)])
(type: Namespace
Text)
(type: Class
(Record
[#declaration Declaration
#class_alias Alias
#namespace Namespace
#members (List Sub)]))
(def: class
(Parser Class)
($_ <>.and
(<>.either (<>.and <code>.local_symbol
(<>#in (list)))
(<code>.form (<>.and <code>.local_symbol
(<>.some <code>.local_symbol))))
..alias
<code>.text
(<>.some ..sub)))
(type: Import
(Variant
{#Class Class}
{#Procedure (Named Procedure)}
{#Global Global}))
(def: import
(Parser Import)
($_ <>.or
..class
(<code>.form ..procedure)
(<code>.form (..named ..output))))
(def: (input_type input :it:)
(-> Input Code Code)
(let [:it: (if (value@ #try? input)
(` (.Either .Text (~ :it:)))
:it:)]
(if (value@ #io? input)
(` ((~! io.IO) (~ :it:)))
:it:)))
(def: (input_term input term)
(-> Input Code Code)
(let [term (if (value@ #try? input)
(` (.try (~ term)))
term)]
(if (value@ #io? input)
(` ((~! io.io) (~ term)))
term)))
(def: (procedure_definition import! source it)
(-> (List Code) Code (Named Procedure) Code)
(let [g!it (|> (value@ #alias it)
(maybe.else (value@ #name it))
code.local_symbol)
g!variables (list#each code.local_symbol (value@ [#anonymous #input #variables] it))
input (value@ [#anonymous #input] it)
:parameters: (value@ #parameters input)
g!parameters (..parameters :parameters:)
:output: (value@ [#anonymous #output] it)
:input:/* (case :parameters:
{.#End}
(list (` []))
parameters
(list#each ..output_type :parameters:))]
(` (.def: ((~ g!it) (~+ (case g!parameters
{.#End} (list g!it)
_ (list#each (value@ #mandatory) g!parameters))))
(.All ((~ g!it) (~+ g!variables))
(-> (~+ :input:/*)
(~ (|> :output:
..output_type
(..input_type input)))))
(.exec
(~+ import!)
(.:expected
(~ (<| (..input_term input)
(..lux_optional :output:)
(` (<apply> (.:expected (~ source))
[(~+ (list#each ..host_optional g!parameters))]))))))))))
(def: (namespaced namespace class alias member)
(-> Namespace Text Alias Text Text)
(|> namespace
(text.replaced "[1]" (maybe.else class alias))
(text.replaced "[0]" member)))
(def: class_separator ".")
(def: host_path
(text.replaced .module_separator ..class_separator))
(for [@.js (as_is)]
(def: (imported class)
(-> Text Code)
(case (text.all_split_by .module_separator class)
{.#Item head tail}
(list#mix (.function (_ sub super)
(` (<get> (~ (code.text sub))
(.:as (..Object .Any)
(~ super)))))
(` (<import> (~ (code.text head))))
tail)
{.#End}
(` (<import> (~ (code.text class)))))))
(def: (global_definition import! it)
(-> (List Code) Global Code)
(let [g!name (|> (value@ #alias it)
(maybe.else (value@ #name it))
code.local_symbol)
:output: (value@ #anonymous it)]
(` (.def: (~ g!name)
(~ (..output_type :output:))
(.exec
(~+ import!)
(.:expected
(~ (<| (lux_optional :output:)
(` (<constant> (~ (code.text (..host_path (value@ #name it))))))))))))))
(for [@.lua (as_is)
@.ruby (as_is)]
(def: (constructor_definition [class_name class_parameters] alias namespace it)
(-> Declaration Alias Namespace Constructor Code)
(let [g!it (|> it
(value@ #alias)
(maybe.else "new")
(..namespaced namespace class_name alias)
code.local_symbol)
input (value@ #anonymous it)
g!input_variables (list#each code.local_symbol (value@ #variables input))
:parameters: (value@ #parameters input)
g!parameters (..parameters :parameters:)
g!class_variables (list#each code.local_symbol class_parameters)
g!class (` ((~ (code.local_symbol (maybe.else class_name alias))) (~+ g!class_variables)))
:output: [#optional? false #mandatory g!class]]
(` (.def: ((~ g!it) (~+ (case g!parameters
{.#End} (list g!it)
_ (list#each (value@ #mandatory) g!parameters))))
(.All ((~ g!it) (~+ g!class_variables) (~+ g!input_variables))
(.-> (~+ (list#each ..output_type :parameters:))
(~ (|> :output:
..output_type
(..input_type input)))))
(.:expected
(~ (<| (..input_term input)
(..lux_optional :output:)
(` (<new> (~ (for [@.js (` (<constant> (~ (code.text (..host_path class_name)))))
@.python (` (.:as ..Function
(~ (..imported class_name))))]))
[(~+ (list#each ..host_optional g!parameters))]))))))))))
(def: (static_field_definition import! [class_name class_parameters] alias namespace it)
(-> (List Code) Declaration Alias Namespace (Named Output) Code)
(let [field (value@ #name it)
g!it (|> (value@ #alias it)
(maybe.else field)
(..namespaced namespace class_name alias)
code.local_symbol)
:field: (value@ #anonymous it)]
(` ((~! syntax:) ((~ g!it) [])
(.# (~! meta.monad) (~' in)
(.list (`' (.exec
(~+ import!)
(.:as (~ (..output_type :field:))
(~ (<| (lux_optional :field:)
(for [@.js (` (<constant> (~ (code.text (%.format (..host_path class_name) "." field)))))
@.ruby (` (<constant> (~ (code.text (%.format (..host_path class_name) "::" field)))))]
(` (<get> (~ (code.text field))
(~ (..imported class_name))))))))))))))))
(def: (virtual_field_definition [class_name class_parameters] alias namespace it)
(-> Declaration Alias Namespace (Named Output) Code)
(let [name (value@ #name it)
g!it (|> (value@ #alias it)
(maybe.else name)
(..namespaced namespace class_name alias)
code.local_symbol)
path (%.format (..host_path class_name) "." name)
:field: (value@ #anonymous it)
g!variables (list#each code.local_symbol class_parameters)
g!class (` ((~ (code.local_symbol (maybe.else class_name alias))) (~+ g!variables)))]
(` (.def: ((~ g!it) (~ g!it))
(.All ((~ g!it) (~+ g!variables))
(.-> (~ g!class)
(~ (..output_type :field:))))
(.:expected
(~ (<| (lux_optional :field:)
(` (<get> (~ (code.text name)) (~ g!it))))))))))
(def: (field_definition import! class alias namespace it)
(-> (List Code) Declaration Alias Namespace Field Code)
(if (value@ #static? it)
(..static_field_definition import! class alias namespace (value@ #member it))
(..virtual_field_definition class alias namespace (value@ #member it))))
(def: (static_method_definition import! [class_name class_parameters] alias namespace it)
(-> (List Code) Declaration Alias Namespace (Named Procedure) Code)
(let [method (value@ #name it)
name (|> (value@ #alias it)
(maybe.else (value@ #name it))
(..namespaced namespace class_name alias))]
(|> it
(with@ #alias {.#Some name})
(..procedure_definition import!
(for [@.js (` (<constant> (~ (code.text (%.format (..host_path class_name) "." method)))))
@.ruby (` (<constant> (~ (code.text (%.format (..host_path class_name) "::" method)))))]
(` (<get> (~ (code.text method))
(.:as (..Object .Any)
(~ (..imported class_name))))))))))
(def: (virtual_method_definition [class_name class_parameters] alias namespace it)
(-> Declaration Alias Namespace (Named Procedure) Code)
(let [method (value@ #name it)
g!it (|> (value@ #alias it)
(maybe.else method)
(..namespaced namespace class_name alias)
code.local_symbol)
procedure (value@ #anonymous it)
input (value@ #input procedure)
g!input_variables (list#each code.local_symbol (value@ #variables input))
:parameters: (value@ #parameters input)
g!parameters (..parameters :parameters:)
g!class_variables (list#each code.local_symbol class_parameters)
g!class (` ((~ (code.local_symbol (maybe.else class_name alias))) (~+ g!class_variables)))
:output: (value@ #output procedure)]
(` (.def: ((~ g!it) (~+ (list#each (value@ #mandatory) g!parameters)) (~ g!it))
(.All ((~ g!it) (~+ g!class_variables) (~+ g!input_variables))
(.-> (~+ (list#each ..output_type :parameters:))
(~ g!class)
(~ (|> :output:
..output_type
(..input_type input)))))
(.:expected
(~ (<| (..input_term input)
(..lux_optional :output:)
(` (<do> (~ (code.text method))
(~ g!it)
[(~+ (list#each ..host_optional g!parameters))])))))))))
(def: (method_definition import! class alias namespace it)
(-> (List Code) Declaration Alias Namespace Method Code)
(if (value@ #static? it)
(static_method_definition import! class alias namespace (value@ #member it))
(virtual_method_definition class alias namespace (value@ #member it))))
(syntax: .public (import: [host_module (<>.maybe <code>.text)
it ..import])
(let [host_module_import! (: (List Code)
(case host_module
{.#Some host_module}
(list (` (<import> (~ (code.text host_module)))))
{.#None}
(list)))]
(case it
{#Global it}
(in (list (..global_definition host_module_import! it)))
{#Procedure it}
(in (list (..procedure_definition host_module_import!
(` (<constant> (~ (code.text (..host_path (value@ #name it))))))
it)))
{#Class it}
(let [class (value@ #declaration it)
alias (value@ #class_alias it)
[class_name class_parameters] class
namespace (value@ #namespace it)
g!class_variables (list#each code.local_symbol class_parameters)
declaration (` ((~ (code.local_symbol (maybe.else class_name alias)))
(~+ g!class_variables)))]
(in (list& (` (.type: (~ declaration)
(..Object (.Primitive (~ (code.text (..host_path class_name)))
[(~+ g!class_variables)]))))
(list#each (.function (_ member)
(`` (`` (case member
(~~ (for [@.lua (~~ (as_is))
@.ruby (~~ (as_is))]
(~~ (as_is {#Constructor it}
(..constructor_definition class alias namespace it)))))
{#Field it}
(..field_definition host_module_import! class alias namespace it)
{#Method it}
(..method_definition host_module_import! class alias namespace it)))))
(value@ #members it)))))
)))
(for [@.ruby (as_is)]
(template: .public (function <inputs> <output>)
[(.:as ..Function
(`` (<function> (~~ (template.amount <inputs>))
(.function (_ [<inputs>])
<output>))))]))
(for [@.js (as_is (template: .public (type_of object)
[("js type-of" object)])
(syntax: .public (global [type <code>.any
[head tail] (<code>.tuple (<>.and <code>.local_symbol (<>.some <code>.local_symbol)))])
(with_symbols [g!_]
(let [global (` ("js constant" (~ (code.text head))))]
(case tail
{.#End}
(in (list (` (: (.Maybe (~ type))
(case (..type_of (~ global))
"undefined"
{.#None}
(~ g!_)
{.#Some (:as (~ type) (~ global))})))))
{.#Item [next tail]}
(let [separator "."]
(in (list (` (: (.Maybe (~ type))
(case (..type_of (~ global))
"undefined"
{.#None}
(~ g!_)
(..global (~ type) [(~ (code.local_symbol (%.format head "." next)))
(~+ (list#each code.local_symbol tail))])))))))))))
(template: (!defined? <global>)
[(.case (..global Any <global>)
{.#None}
.false
{.#Some _}
.true)])
(template [<name> <global>]
[(def: .public <name>
Bit
(!defined? <global>))]
[on_browser? [window]]
[on_nashorn? [java lang Object]]
)
(def: .public on_node_js?
Bit
(|> (..global (Object Any) [process])
(maybe#each (|>> []
("js apply" ("js constant" "Object.prototype.toString.call"))
(:as Text)
(text#= "[object process]")))
(maybe.else false)))
)]
(as_is))
)
|