blob: cf7902a8fc7cbf40fb83809514d08447bf02a620 (
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
|
(.module:
[lux #*
[abstract
[monad (#+ do)]]
[control
["." io]
["<>" parser
["<c>" code (#+ Parser)]]]
[data
["." product]
[text
["%" format (#+ format)]]
[collection
["." list ("#@." functor)]]]
[type
abstract]
["." macro (#+ with-gensyms)
[syntax (#+ syntax:)]
["." code]
["." template]]])
(abstract: #export (Object brand) {} Any)
(template [<name>]
[(with-expansions [<brand> (template.identifier [<name> "'"])]
(abstract: #export <brand> {} Any)
(type: #export <name> (Object <brand>)))]
[Function]
[Symbol]
[Null]
[Undefined]
)
(template [<name> <type>]
[(type: #export <name> <type>)]
[Boolean Bit]
[Number Frac]
[String Text]
)
(type: Nullable [Bit Code])
(def: nullable
(Parser Nullable)
(let [token (' #?)]
(<| (<>.and (<>.parses? (<c>.this! token)))
(<>.after (<>.not (<c>.this! token)))
<c>.any)))
(type: Constructor (List Nullable))
(def: constructor
(Parser Constructor)
(<c>.form (<>.after (<c>.this! (' new))
(<c>.tuple (<>.some ..nullable)))))
(type: Field [Text Nullable])
(def: field
(Parser Field)
(<c>.form ($_ <>.and
<c>.local-identifier
..nullable)))
(type: Common-Method [Text (List Nullable) Bit Nullable])
(type: Static-Method Common-Method)
(type: Virtual-Method Common-Method)
(type: Method
(#Static Static-Method)
(#Virtual Virtual-Method))
(def: common-method
(Parser Common-Method)
($_ <>.and
<c>.local-identifier
(<c>.tuple (<>.some ..nullable))
(<>.parses? (<c>.this! (' #try)))
..nullable))
(def: static-method
(<c>.form (<>.after (<c>.this! (' #static)) ..common-method)))
(def: method
(Parser Method)
(<>.or ..static-method
(<c>.form ..common-method)))
(type: Member
(#Constructor Constructor)
(#Field Field)
(#Method Method))
(def: member
(Parser Member)
($_ <>.or
..constructor
..field
..method
))
(def: input-variables
(-> (List Nullable) (List [Bit Code]))
(|>> list.enumerate
(list@map (function (_ [idx [nullable? type]])
[nullable? (|> idx %.nat code.local-identifier)]))))
(def: (nullable-type [nullable? type])
(-> Nullable Code)
(if nullable?
(` (.Maybe (~ type)))
type))
(def: (with-null g!temp [nullable? input])
(-> Code [Bit Code] Code)
(if nullable?
(` (case (~ input)
(#.Some (~ g!temp))
(~ g!temp)
#.None
("js object null")))
input))
(def: (without-null g!temp [nullable? outputT] output)
(-> Code Nullable Code Code)
(if nullable?
(` (let [(~ g!temp) (~ output)]
(if ("js object null?" (~ g!temp))
#.None
(#.Some (~ g!temp)))))
output))
(type: Import
(#Class [Text (List Member)])
(#Function Static-Method))
(def: import
($_ <>.or
($_ <>.and
<c>.local-identifier
(<>.some member))
..static-method
))
(def: (with-try try? without-try)
(-> Bit Code Code)
(if try?
(` ("lux try"
((~! io.io) (~ without-try))))
without-try))
(def: (try-type try? rawT)
(-> Bit Code Code)
(if try?
(` (.Either .Text (~ rawT)))
rawT))
(def: (make-function g!method g!temp source inputsT try? outputT)
(-> Code Code Text (List Nullable) Bit Nullable Code)
(let [g!inputs (input-variables inputsT)]
(` (def: ((~ g!method)
[(~+ (list@map product.right g!inputs))])
(-> [(~+ (list@map nullable-type inputsT))]
(~ (try-type try? (nullable-type outputT))))
(:assume
(~ (<| (with-try try?)
(without-null g!temp outputT)
(` ("js apply"
("js constant" (~ (code.text source)))
(~+ (list@map (with-null g!temp) g!inputs)))))))))))
(syntax: #export (import: {import ..import})
(with-gensyms [g!temp]
(case import
(#Class [class members])
(with-gensyms [g!object]
(let [qualify (: (-> Text Code)
(|>> (format class "::") code.local-identifier))
g!type (code.local-identifier class)]
(wrap (list& (` (type: (~ g!type) (..Object (primitive (~ (code.text class))))))
(list@map (function (_ member)
(case member
(#Constructor inputsT)
(let [g!inputs (input-variables inputsT)]
(` (def: ((~ (qualify "new"))
[(~+ (list@map product.right g!inputs))])
(-> [(~+ (list@map nullable-type inputsT))]
(~ g!type))
(:assume
("js object new"
("js constant" (~ (code.text class)))
[(~+ (list@map (with-null g!temp) g!inputs))])))))
(#Field [field fieldT])
(` (def: ((~ (qualify field))
(~ g!object))
(-> (~ g!type)
(~ (nullable-type fieldT)))
(:assume
(~ (without-null g!temp fieldT (` ("js object get" (~ (code.text field)) (~ g!object))))))))
(#Method method)
(case method
(#Static [method inputsT try? outputT])
(make-function (qualify method) g!temp method inputsT try? outputT)
(#Virtual [method inputsT try? outputT])
(let [g!inputs (input-variables inputsT)]
(` (def: ((~ (qualify method))
[(~+ (list@map product.right g!inputs))]
(~ g!object))
(-> [(~+ (list@map nullable-type inputsT))]
(~ g!type)
(~ (try-type try? (nullable-type outputT))))
(:assume
(~ (<| (with-try try?)
(without-null g!temp outputT)
(` ("js object do"
(~ (code.text method))
(~ g!object)
[(~+ (list@map (with-null g!temp) g!inputs))])))))))))))
members)))))
(#Function [name inputsT try? outputT])
(wrap (list (make-function (code.local-identifier name) g!temp name inputsT try? outputT)))
)))
(syntax: #export (type-of object)
(wrap (list (` ("js type-of" (~ object))))))
(def: #export on-browser?
Bit
(case (..type-of ("js constant" "window"))
"undefined"
false
_
true))
(def: #export on-node-js?
Bit
(case (..type-of ("js constant" "process"))
"undefined"
false
_
(case (:coerce .Text
("js apply"
("js constant" "Object.prototype.toString.call")
("js constant" "process")))
"[object process]"
true
_
false)))
|