-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
js-transform.sml
391 lines (383 loc) · 15.8 KB
/
js-transform.sml
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
(*
* Copyright (c) 2023 ARATA Mizuki
* This file is part of LunarML.
*)
structure JsTransform :>
sig
type Context = {nextVId: int ref}
val doProgram: Context -> JsSyntax.Block -> JsSyntax.Block
end =
struct
structure J = JsSyntax
fun collectLetConstStat (J.LetStat vars) acc =
Vector.foldl
(fn ((vid, _), acc) => J.IdSet.add (acc, J.UserDefinedId vid)) acc
vars
| collectLetConstStat (J.ConstStat vars) acc =
Vector.foldl
(fn ((vid, _), acc) => J.IdSet.add (acc, J.UserDefinedId vid)) acc
vars
| collectLetConstStat (J.ExpStat _) acc = acc
| collectLetConstStat (J.IfStat _) acc = acc
| collectLetConstStat (J.ReturnStat _) acc = acc
| collectLetConstStat (J.TryCatchStat _) acc = acc
| collectLetConstStat (J.ThrowStat _) acc = acc
| collectLetConstStat (J.BlockStat _) acc = acc
| collectLetConstStat (J.LoopStat _) acc = acc
| collectLetConstStat (J.SwitchStat _) acc = acc
| collectLetConstStat (J.BreakStat _) acc = acc
| collectLetConstStat (J.ContinueStat _) acc = acc
| collectLetConstStat (J.DefaultExportStat _) acc = acc
| collectLetConstStat (J.NamedExportStat _) acc = acc
and collectLetConstBlock stats acc =
Vector.foldl (fn (stat, acc) => collectLetConstStat stat acc) acc stats
fun freeVarsExp (_, J.ConstExp _) acc = acc
| freeVarsExp (_, J.ThisExp) acc = acc
| freeVarsExp (bound, J.VarExp x) acc =
if J.IdSet.member (bound, x) then acc else J.IdSet.add (acc, x)
| freeVarsExp (bound, J.ObjectExp fields) acc =
Vector.foldl (fn ((_, exp), acc) => freeVarsExp (bound, exp) acc) acc
fields
| freeVarsExp (bound, J.ArrayExp elems) acc =
Vector.foldl (fn (exp, acc) => freeVarsExp (bound, exp) acc) acc elems
| freeVarsExp (bound, J.CallExp (x, ys)) acc =
Vector.foldl (fn (exp, acc) => freeVarsExp (bound, exp) acc)
(freeVarsExp (bound, x) acc) ys
| freeVarsExp (bound, J.MethodExp (x, _, ys)) acc =
Vector.foldl (fn (exp, acc) => freeVarsExp (bound, exp) acc)
(freeVarsExp (bound, x) acc) ys
| freeVarsExp (bound, J.NewExp (x, ys)) acc =
Vector.foldl (fn (exp, acc) => freeVarsExp (bound, exp) acc)
(freeVarsExp (bound, x) acc) ys
| freeVarsExp (bound, J.FunctionExp (params, body)) acc =
let
val bound' =
Vector.foldl (fn (id, bound) => J.IdSet.add (bound, id)) bound
params
val bound'' = collectLetConstBlock body bound'
in
freeVarsBlock (bound'', body) acc
end
| freeVarsExp (bound, J.BinExp (_, x, y)) acc =
freeVarsExp (bound, x) (freeVarsExp (bound, y) acc)
| freeVarsExp (bound, J.UnaryExp (_, x)) acc =
freeVarsExp (bound, x) acc
| freeVarsExp (bound, J.IndexExp (x, y)) acc =
freeVarsExp (bound, x) (freeVarsExp (bound, y) acc)
| freeVarsExp (bound, J.CondExp (x, y, z)) acc =
freeVarsExp (bound, x) (freeVarsExp (bound, y)
(freeVarsExp (bound, z) acc))
and freeVarsStat (bound, J.LetStat vars) acc =
Vector.foldl
(fn ((_, NONE), acc) => acc
| ((_, SOME exp), acc) => freeVarsExp (bound, exp) acc) acc vars
| freeVarsStat (bound, J.ConstStat vars) acc =
Vector.foldl (fn ((_, exp), acc) => freeVarsExp (bound, exp) acc) acc
vars
| freeVarsStat (bound, J.ExpStat exp) acc =
freeVarsExp (bound, exp) acc
| freeVarsStat (bound, J.IfStat (cond, then', else')) acc =
freeVarsExp (bound, cond) (freeVarsBlock (bound, then')
(freeVarsBlock (bound, else') acc))
| freeVarsStat (_, J.ReturnStat NONE) acc = acc
| freeVarsStat (bound, J.ReturnStat (SOME exp)) acc =
freeVarsExp (bound, exp) acc
| freeVarsStat (bound, J.TryCatchStat (try, vid, catch)) acc =
freeVarsBlock (bound, try)
(freeVarsBlock (J.IdSet.add (bound, J.UserDefinedId vid), catch) acc)
| freeVarsStat (bound, J.ThrowStat exp) acc =
freeVarsExp (bound, exp) acc
| freeVarsStat (bound, J.BlockStat (_, block)) acc =
freeVarsBlock (bound, block) acc
| freeVarsStat (bound, J.LoopStat (_, block)) acc =
freeVarsBlock (bound, block) acc
| freeVarsStat (bound, J.SwitchStat (exp, cases)) acc =
List.foldl (fn ((_, block), acc) => freeVarsBlock (bound, block) acc)
(freeVarsExp (bound, exp) acc) cases
| freeVarsStat (_, J.BreakStat _) acc = acc
| freeVarsStat (_, J.ContinueStat _) acc = acc
| freeVarsStat (bound, J.DefaultExportStat exp) acc =
freeVarsExp (bound, exp) acc
| freeVarsStat (bound, J.NamedExportStat entities) acc =
Vector.foldl
(fn ((x, _), acc) =>
if J.IdSet.member (bound, x) then acc else J.IdSet.add (acc, x))
acc entities
and freeVarsBlock (bound, stats) acc =
let
val bound' = collectLetConstBlock stats bound
in
Vector.foldl (fn (stat, acc) => freeVarsStat (bound', stat) acc) acc stats
end
type Context = {nextVId: int ref}
fun freshVId (ctx: Context, name) =
let
val n = !(#nextVId ctx)
val _ = #nextVId ctx := n + 1
in
TypedSyntax.MkVId (name, n)
end
fun renewVId (ctx: Context, TypedSyntax.MkVId (name, _)) =
let
val n = !(#nextVId ctx)
val _ = #nextVId ctx := n + 1
in
TypedSyntax.MkVId (name, n)
end
(*:
val goExp : Context * J.IdSet.set * TypedSyntax.VId TypedSyntax.VIdMap.map * int * J.Exp -> J.Stat list * J.Exp
val goExpVector : Context * J.IdSet.set * TypedSyntax.VId TypedSyntax.VIdMap.map * int * J.Exp vector -> J.Stat list * J.Exp vector
val goStat : Context * J.IdSet.set * TypedSyntax.VId TypedSyntax.VIdMap.map * int * J.Stat -> J.Stat list * J.Stat
val goBlock : Context * J.IdSet.set * TypedSyntax.VId TypedSyntax.VIdMap.map * int * J.Stat vector -> J.Stat list * J.Stat vector
*)
fun goExp (_, _, _, _, e as J.ConstExp _) = ([], e)
| goExp (_, _, _, _, e as J.ThisExp) = ([], e)
| goExp (_, _, renameMap, _, e as J.VarExp id) =
(case id of
J.PredefinedId _ => ([], e)
| J.UserDefinedId vid =>
case TypedSyntax.VIdMap.find (renameMap, vid) of
SOME newVId => ([], J.VarExp (J.UserDefinedId newVId))
| NONE => ([], e))
| goExp (ctx, bound, renameMap, depth, J.ObjectExp fields) =
let
val (decs, fields') =
Vector.foldr
(fn ((key, exp), (decs, fields)) =>
let
val (decs', exp') = goExp (ctx, bound, renameMap, depth, exp)
in
(decs' @ decs, (key, exp') :: fields)
end) ([], []) fields
in
(decs, J.ObjectExp (Vector.fromList fields'))
end
| goExp (ctx, bound, renameMap, depth, J.ArrayExp elems) =
let
val (decs, elems') = goExpVector (ctx, bound, renameMap, depth, elems)
in
(decs, J.ArrayExp elems')
end
| goExp (ctx, bound, renameMap, depth, J.CallExp (x, ys)) =
let
val (decs, x') = goExp (ctx, bound, renameMap, depth, x)
val (decs', ys') = goExpVector (ctx, bound, renameMap, depth, ys)
in
(decs @ decs', J.CallExp (x', ys'))
end
| goExp (ctx, bound, renameMap, depth, J.MethodExp (x, name, ys)) =
let
val (decs, x') = goExp (ctx, bound, renameMap, depth, x)
val (decs', ys') = goExpVector (ctx, bound, renameMap, depth, ys)
in
(decs @ decs', J.MethodExp (x', name, ys'))
end
| goExp (ctx, bound, renameMap, depth, J.NewExp (x, ys)) =
let
val (decs, x') = goExp (ctx, bound, renameMap, depth, x)
val (decs', ys') = goExpVector (ctx, bound, renameMap, depth, ys)
in
(decs @ decs', J.NewExp (x', ys'))
end
| goExp (ctx, bound, renameMap, depth, f as J.FunctionExp (params, body)) =
if depth > 200 then
let
val fv = freeVarsExp (J.IdSet.empty, f) J.IdSet.empty
val captures = J.IdSet.toList (J.IdSet.intersection (bound, fv))
val bound' =
Vector.foldl (fn (id, bound) => J.IdSet.add (bound, id)) bound
params
val name = freshVId (ctx, "f")
val params' = Vector.foldr (op::) [] params
val capturesAndParams = Vector.fromList (captures @ params')
val (renameMap', capturesAndParams2) =
Vector.foldr
(fn (id as J.PredefinedId _, (m, acc)) => (m, id :: acc)
| (J.UserDefinedId vid, (m, acc)) =>
let
val vid' = renewVId (ctx, vid)
in
( TypedSyntax.VIdMap.insert (m, vid, vid')
, J.UserDefinedId vid' :: acc
)
end) (renameMap, []) capturesAndParams
val (decs, body') = goBlock (ctx, bound', renameMap', 0, body)
val newDec = J.ConstStat (vector
[(name, J.FunctionExp (Vector.fromList capturesAndParams2, body'))])
fun renameVId vid =
case TypedSyntax.VIdMap.find (renameMap, vid) of
SOME vid' => vid'
| NONE => vid
fun renameId (id as J.PredefinedId _) = id
| renameId (id as J.UserDefinedId vid) =
case TypedSyntax.VIdMap.find (renameMap, vid) of
SOME vid' => J.UserDefinedId vid'
| NONE => id
val newExp = J.FunctionExp (params, vector
[J.ReturnStat (SOME (J.CallExp
( J.VarExp (J.UserDefinedId (renameVId name))
, Vector.map (J.VarExp o renameId) capturesAndParams
)))])
in
(decs @ [newDec], newExp)
end
else
let
val bound' =
Vector.foldl (fn (id, bound) => J.IdSet.add (bound, id)) bound
params
val (decs, body') = goBlock
(ctx, bound', renameMap, depth + 1, body)
in
(decs, J.FunctionExp (params, body'))
end
| goExp (ctx, bound, renameMap, depth, J.BinExp (p, x, y)) =
let
val (decs, x') = goExp (ctx, bound, renameMap, depth, x)
val (decs', y') = goExp (ctx, bound, renameMap, depth, y)
in
(decs @ decs', J.BinExp (p, x', y'))
end
| goExp (ctx, bound, renameMap, depth, J.UnaryExp (p, x)) =
let val (decs, x') = goExp (ctx, bound, renameMap, depth, x)
in (decs, J.UnaryExp (p, x'))
end
| goExp (ctx, bound, renameMap, depth, J.IndexExp (x, y)) =
let
val (decs, x') = goExp (ctx, bound, renameMap, depth, x)
val (decs', y') = goExp (ctx, bound, renameMap, depth, y)
in
(decs @ decs', J.IndexExp (x', y'))
end
| goExp (ctx, bound, renameMap, depth, J.CondExp (x, y, z)) =
let
val (decs, x') = goExp (ctx, bound, renameMap, depth, x)
val (decs', y') = goExp (ctx, bound, renameMap, depth, y)
val (decs'', z') = goExp (ctx, bound, renameMap, depth, z)
in
(decs @ decs' @ decs'', J.CondExp (x', y', z'))
end
and goExpVector (ctx, bound, renameMap, depth, xs) =
let
val (decs, ys) =
Vector.foldr
(fn (exp, (decs, ys)) =>
let val (decs', exp') = goExp (ctx, bound, renameMap, depth, exp)
in (decs' @ decs, exp' :: ys)
end) ([], []) xs
in
(decs, Vector.fromList ys)
end
and goStat (ctx, bound, renameMap, depth, J.LetStat vars) =
let
val (decs, vars) =
Vector.foldr
(fn ((vid, NONE), (decs, vars)) => (decs, (vid, NONE) :: vars)
| ((vid, SOME exp), (decs, vars)) =>
let
val (decs', exp) = goExp (ctx, bound, renameMap, depth, exp)
in
(decs' @ decs, (vid, SOME exp) :: vars)
end) ([], []) vars
in
(decs, J.LetStat (Vector.fromList vars))
end
| goStat (ctx, bound, renameMap, depth, J.ConstStat vars) =
let
val (decs, vars) =
Vector.foldr
(fn ((vid, exp), (decs, vars)) =>
let
val (decs', exp) = goExp (ctx, bound, renameMap, depth, exp)
in
(decs' @ decs, (vid, exp) :: vars)
end) ([], []) vars
in
(decs, J.ConstStat (Vector.fromList vars))
end
| goStat (ctx, bound, renameMap, depth, J.ExpStat exp) =
let val (decs, exp) = goExp (ctx, bound, renameMap, depth, exp)
in (decs, J.ExpStat exp)
end
| goStat (ctx, bound, renameMap, depth, J.IfStat (exp, then', else')) =
let
val (decs, exp) = goExp (ctx, bound, renameMap, depth, exp)
val (decs', then') = goBlock (ctx, bound, renameMap, depth, then')
val (decs'', else') = goBlock (ctx, bound, renameMap, depth, else')
in
(decs @ decs' @ decs'', J.IfStat (exp, then', else'))
end
| goStat (_, _, _, _, s as J.ReturnStat NONE) = ([], s)
| goStat (ctx, bound, renameMap, depth, J.ReturnStat (SOME exp)) =
let val (decs, exp) = goExp (ctx, bound, renameMap, depth, exp)
in (decs, J.ReturnStat (SOME exp))
end
| goStat (ctx, bound, renameMap, depth, J.TryCatchStat (try, vid, catch)) =
let
val (decs, try) = goBlock (ctx, bound, renameMap, depth, try)
val (decs', catch) = goBlock
( ctx
, J.IdSet.add (bound, J.UserDefinedId vid)
, renameMap
, depth
, catch
)
in
(decs @ decs', J.TryCatchStat (try, vid, catch))
end
| goStat (ctx, bound, renameMap, depth, J.ThrowStat exp) =
let val (decs, exp) = goExp (ctx, bound, renameMap, depth, exp)
in (decs, J.ThrowStat exp)
end
| goStat (ctx, bound, renameMap, depth, J.BlockStat (optLabel, block)) =
let val (decs, block) = goBlock (ctx, bound, renameMap, depth, block)
in (decs, J.BlockStat (optLabel, block))
end
| goStat (ctx, bound, renameMap, depth, J.LoopStat (optLabel, block)) =
let val (decs, block) = goBlock (ctx, bound, renameMap, depth, block)
in (decs, J.LoopStat (optLabel, block))
end
| goStat (ctx, bound, renameMap, depth, J.SwitchStat (exp, cases)) =
let
val (decs, exp) = goExp (ctx, bound, renameMap, depth, exp)
val (decs', cases) =
List.foldr
(fn ((c, block), (decs, cases)) =>
let
val (decs', block) = goBlock
(ctx, bound, renameMap, depth, block)
in
(decs' @ decs, (c, block) :: cases)
end) ([], []) cases
in
(decs @ decs', J.SwitchStat (exp, cases))
end
| goStat (_, _, _, _, s as J.BreakStat _) = ([], s)
| goStat (_, _, _, _, s as J.ContinueStat _) = ([], s)
| goStat (ctx, bound, renameMap, depth, J.DefaultExportStat exp) =
let val (decs, exp) = goExp (ctx, bound, renameMap, depth, exp)
in (decs, J.DefaultExportStat exp)
end
| goStat (_, _, _, _, s as J.NamedExportStat _) = ([], s)
and goBlock (ctx, bound, renameMap, depth, stats) =
let
val bound' = collectLetConstBlock stats bound
val (decs, ys) =
Vector.foldr
(fn (stat, (decs, ys)) =>
let
val (decs', stat') = goStat (ctx, bound', renameMap, depth, stat)
in
(decs' @ decs, stat' :: ys)
end) ([], []) stats
in
(decs, Vector.fromList ys)
end
fun doProgram ctx block =
let
val (decs, block') = goBlock
(ctx, J.IdSet.empty, TypedSyntax.VIdMap.empty, 0, block)
in
Vector.fromList (decs @ Vector.foldr (op::) [] block')
end
end;