-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate.ml
325 lines (320 loc) · 8.46 KB
/
generate.ml
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
(* Generate *)
open Ast;;
let fp = Format.fprintf;;
type context =
| Inner
| Top
;;
let is_property_safe =
let rex = lazy (Str.regexp !Opt.safe_property_regexp) in
fun n -> Str.string_match (Lazy.force rex) n 0
;;
let generate f pg =
let comma () =
let first = ref true in
fun () ->
if !first then
first := false
else
fp f ",@ "
in
let semi f colon =
match colon with
| None -> ()
| Some c -> fp f "%c" c
in
let rec source_element = function
| St(_, _, st) ->
fp f "@\n";
statement ~colon:';' st
| FunDecl(_, _, (Some(n), al, sl)) ->
fp f "function %s(" n;
let c = comma () in
List.iter (fun a -> c (); fp f "%s" a) al;
fp f ")@ ";
block sl
| _ -> fp f "/* FunDecl */"
and program pg = List.iter source_element pg
and block sl =
fp f "@[<v2>{@\n";
program sl;
fp f "@]@\n}"
and statement_block ?colon sl =
fp f "@[<hov2>{";
List.iter (fun s -> statement ~colon:';' s) sl;
fp f "@]@\n}%a" semi colon
and statement ?colon = function
| Throw x -> fp f "throw "; expression Inner x; fp f "%a@\n" semi colon
| Labeled(lb, s) -> fp f "%s:@\n" lb; statement ?colon s
| Break None -> fp f "break%a@\n" semi colon
| Break(Some lb) -> fp f "break %s%a@\n" lb semi colon
| Continue None -> fp f "continue%a@\n" semi colon
| Continue(Some lb) -> fp f "break %s%a@\n" lb semi colon
| With(x, s) ->
fp f "with(";
expression Top x;
fp f ")@ ";
statement ?colon s
| While(x, s) ->
fp f "while(";
expression Top x;
fp f ")@ ";
statement ?colon s
| Position(_,_,st) -> statement ?colon st
| Expr x -> expression Top x; fp f "%a@\n" semi colon
| Variable [] -> () (* XXX *)
| Variable vl ->
vars ?colon vl
| If(x, s, so) ->
fp f "@[<hov2>@[<h>if(";
expression Top x;
fp f ")@]@\n";
begin match so with
| None ->
statement s;
fp f "@]%a" semi colon
| Some s ->
statement ~colon:';' s;
fp f "@]";
fp f "@ else @[<v2>";
statement s;
fp f "@]"
end
| Nop -> ()
| Return(Some x) -> fp f "return@ "; expression Inner x; fp f "%a@\n" semi colon
| Return None -> fp f "return %a@\n" semi colon
| ForIn(LHS x1, x2, st) ->
fp f "@[<hov2>for(";
expression Inner x1;
fp f " in ";
expression Inner x2;
fp f ")";
statement ?colon st;
fp f "@\n@]"
| ForIn(Vars vdl, x2, st) ->
fp f "@[<hov2>for(";
vars vdl;
fp f " in ";
expression Inner x2;
fp f ")";
statement ?colon st;
fp f "@\n@]"
| For(so1, so2, so3, s) ->
fp f "for(";
statement_option so1;
fp f ";@ ";
statement_option so2;
fp f ";@ ";
statement_option so3;
fp f ")@ ";
statement s
| Block sl ->
fp f "@\n";
statement_block ?colon sl
| Try(st, co, fo) ->
fp f "@[<hov2>try";
statement st;
fp f "@\n";
begin match co with
| None -> ()
| Some(a, st) ->
fp f "@\ncatch(%s)" a;
statement st
end;
begin match fo with
| None -> ()
| Some st ->
fp f "@\nfinally";
statement st
end
| Do(s, x) ->
fp f "do";
statement s;
fp f "while(";
expression Inner x;
fp f ")%a" semi colon
| Switch(x, cls) ->
fp f "@[<hov2>switch(";
expression Inner x;
fp f ") {";
List.iter
begin fun (cl, s) ->
fp f "@\n";
List.iter clause cl;
fp f "@\n";
statement ~colon:';' s
end
cls;
fp f "@\n}@]";
and vars ?colon vl =
fp f "@[<hov2>var ";
let c = comma () in
List.iter
begin fun x ->
c ();
match x with
| (n, None) -> fp f "@\n%s" n
| (n, Some x) -> fp f "@\n%s = " n; expression Top x
end
vl;
fp f "%a" semi colon
and clause = function
| Default -> fp f "@\ndefault:"
| Case x -> fp f "@\ncase "; expression Inner x; fp f ":"
and statement_option ?colon = function
| None -> semi f colon
| Some s -> statement ?colon s
and expression context x =
match context, x with
| _, This -> fp f "this"
| _, Extra _ -> ()
| Top, Assign(x1, op, x2) ->
fp f "@[<hov2>";
expression Top x1;
fp f " ";
begin
match op with
| A_eq -> fp f "="
| A_mul -> fp f "*="
| A_div -> fp f "/="
| A_mod -> fp f "%%="
| A_add -> fp f "+="
| A_sub -> fp f "-="
| A_lsl -> fp f "<<="
| A_lsr -> fp f ">>="
| A_asr -> fp f ">>>="
| A_and -> fp f "&="
| A_or -> fp f "|="
| A_xor -> fp f "^="
end;
fp f "@ ";
expression Top x2;
fp f "@]"
| Top, Sq xl -> List.iter (expression Top) xl
| _, L l -> litteral l
| _, V n -> fp f "%s" n
| Top, Apply(x1, xl) ->
fp f "@[<hv2>";
expression Inner x1;
fp f "(";
let c = comma () in
List.iter (fun x -> c (); expression Inner x) xl;
fp f ")@]"
| _, Array xl ->
let c = comma () in
fp f "@[<h>[";
List.iter (fun x -> c (); expression Inner x) xl;
fp f "]@]";
| Top, Conditional(x1, x2, x3) ->
fp f "@[<h>";
expression Top x1;
fp f " ?@ ";
expression Top x2;
fp f " :@ ";
expression Top x3;
fp f "@]"
| Top, Object pl ->
let c = comma () in
fp f "@\n@[<hov2>{";
List.iter
begin fun (pn, x) ->
c ();
fp f "@\n";
begin
match pn with
| PN_Int x -> fp f "%ld" x
| PN_Float x -> fp f "%f" x
| PN_String x when is_property_safe x -> fp f "%s" x
| PN_String x -> fp f "%S" x
| PN_Empty -> fp f "*empty*"
end;
fp f ": ";
expression Inner x
end
pl;
fp f "@\n@]}"
| _, Function(_, _, func) -> generate_function func
| _, B(B_bracket, x1, L(String u)) when is_property_safe u ->
expression Top x1;
fp f ".%s" u
| Top, B(B_bracket, x1, x2) ->
fp f "@[<h>";
expression Inner x1;
fp f "[";
expression Top x2;
fp f "]@]"
| Top, B(op, x1, x2) ->
fp f "@[<h>";
expression Inner x1;
let u =
match op with
| B_add -> "+"
| B_mul -> "*"
| B_sub -> "-"
| B_div -> "/"
| B_equal -> "=="
| B_notequal -> "!="
| B_physequal -> "==="
| B_physnotequal -> "!=="
| B_bitand -> "&"
| B_bitor -> "|"
| B_bitxor -> "^"
| B_and -> "&&"
| B_or -> "||"
| B_le -> "<="
| B_lt -> "<"
| B_ge -> ">="
| B_gt -> ">"
| B_instanceof -> "instanceof"
| B_in -> "in"
| _ -> "/* operator */"
in
fp f " %s@ " u;
expression Inner x2;
fp f "@]"
| Top, U(U_post_increment, x) -> expression Inner x; fp f " ++"
| Top, U(U_post_decrement, x) -> expression Inner x; fp f " --"
| Top, U(op, x) ->
let u =
match op with
| U_new -> "new"
| U_delete -> "delete"
| U_typeof -> "typeof"
| U_pre_increment -> "++"
| U_pre_decrement -> "--"
| U_plus -> "+"
| U_minus -> "-"
| U_not -> "!"
| _ -> assert false
in
fp f "%s " u;
expression Inner x
| Inner, _ -> fp f "(@["; expression Top x; fp f "@])"
and generate_function (no, al, sl) =
fp f "function";
begin
match no with
| None -> fp f "("
| Some n -> fp f " %s(" n
end;
fp f "@[<h>";
let c = comma () in
List.iter
begin fun n ->
c ();
fp f "%s" n
end
al;
fp f "@])@ ";
block sl
and litteral = function
| Float x -> fp f "%f" x
| Int x -> fp f "%ld" x
| String s -> fp f "%S" s
| Regexp(r, o) -> fp f "/%s/%s" r o
| Bool b -> fp f "%b" b
| Null -> fp f "null"
| Undefined -> fp f "undefined"
in
program pg
;;