Skip to content

Commit aaf7a6e

Browse files
committed
class support
1 parent a153a3a commit aaf7a6e

13 files changed

+406
-128
lines changed

compiler/lib/dune

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333
--unused-token
3434
T_ERROR
3535
--unused-token
36-
T_AT
37-
--unused-token
38-
T_POUND))
36+
T_AT))
3937

4038
(menhir
4139
(modules annot_parser)

compiler/lib/javascript.ml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,15 @@ and property_list = property list
222222

223223
and property =
224224
| Property of property_name * expression
225-
| PropertyGet of property_name * function_declaration
226-
| PropertySet of property_name * function_declaration
227-
| PropertyMethod of property_name * function_declaration
228225
| PropertySpread of expression
226+
| PropertyMethod of property_name method_
229227
| CoverInitializedName of early_error * ident * initialiser
230228

229+
and 'name method_ =
230+
| MethodGet of 'name * function_declaration
231+
| MethodSet of 'name * function_declaration
232+
| Method of 'name * function_declaration
233+
231234
and property_name =
232235
| PNI of identifier
233236
| PNS of Utf8_string.t
@@ -247,6 +250,7 @@ and expression =
247250
| ENew of expression * arguments option
248251
| EVar of ident
249252
| EFun of ident option * function_declaration
253+
| EClass of ident option * class_declaration
250254
| EArrow of function_declaration
251255
| EStr of Utf8_string.t
252256
| ETemplate of template
@@ -276,6 +280,7 @@ and statement =
276280
| Block of block
277281
| Variable_statement of variable_declaration_kind * variable_declaration list
278282
| Function_declaration of ident * function_declaration
283+
| Class_declaration of ident * class_declaration
279284
| Empty_statement
280285
| Expression_statement of expression
281286
| If_statement of expression * (statement * location) * (statement * location) option
@@ -335,6 +340,20 @@ and function_kind =
335340
; generator : bool
336341
}
337342

343+
and class_declaration =
344+
{ extends : expression option
345+
; body : class_element list
346+
}
347+
348+
and class_element =
349+
| CEMethod of bool * class_element_name method_
350+
| CEField of bool * class_element_name * initialiser option
351+
| CEStaticBLock of statement_list
352+
353+
and class_element_name =
354+
| PropName of property_name
355+
| PrivName of ident
356+
338357
and ('a, 'b) list_with_rest =
339358
{ list : 'a list
340359
; rest : 'b option

compiler/lib/javascript.mli

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,15 @@ and property_list = property list
169169

170170
and property =
171171
| Property of property_name * expression
172-
| PropertyGet of property_name * function_declaration
173-
| PropertySet of property_name * function_declaration
174-
| PropertyMethod of property_name * function_declaration
175172
| PropertySpread of expression
173+
| PropertyMethod of property_name method_
176174
| CoverInitializedName of early_error * ident * initialiser
177175

176+
and 'name method_ =
177+
| MethodGet of 'name * function_declaration
178+
| MethodSet of 'name * function_declaration
179+
| Method of 'name * function_declaration
180+
178181
and property_name =
179182
| PNI of identifier
180183
| PNS of Utf8_string.t
@@ -198,6 +201,7 @@ and expression =
198201
| ENew of expression * arguments option
199202
| EVar of ident
200203
| EFun of ident option * function_declaration
204+
| EClass of ident option * class_declaration
201205
| EArrow of function_declaration
202206
| EStr of Utf8_string.t
203207
(* A UTF-8 encoded string that may contain escape sequences. *)
@@ -228,6 +232,7 @@ and statement =
228232
| Block of block
229233
| Variable_statement of variable_declaration_kind * variable_declaration list
230234
| Function_declaration of ident * function_declaration
235+
| Class_declaration of ident * class_declaration
231236
| Empty_statement
232237
| Expression_statement of expression
233238
| If_statement of expression * (statement * location) * (statement * location) option
@@ -289,6 +294,20 @@ and function_kind =
289294
; generator : bool
290295
}
291296

297+
and class_declaration =
298+
{ extends : expression option
299+
; body : class_element list
300+
}
301+
302+
and class_element =
303+
| CEMethod of bool * class_element_name method_
304+
| CEField of bool * class_element_name * initialiser option
305+
| CEStaticBLock of statement_list
306+
307+
and class_element_name =
308+
| PropName of property_name
309+
| PrivName of ident
310+
292311
and ('a, 'b) list_with_rest =
293312
{ list : 'a list
294313
; rest : 'b option

compiler/lib/js_assign.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,17 @@ class traverse record_block =
322322
object (m)
323323
inherit Js_traverse.free as super
324324

325-
method! block b =
325+
method! record_block b =
326326
record_block m#state b;
327-
super#block b
327+
super#record_block b
328328
end
329329

330330
let program' (module Strategy : Strategy) p =
331331
let nv = Var.count () in
332332
let state = Strategy.create nv in
333333
let mapper = new traverse (Strategy.record_block state) in
334334
let p = mapper#program p in
335-
mapper#block Normal;
335+
mapper#record_block Normal;
336336
let free =
337337
IdentSet.filter
338338
(function

compiler/lib/js_output.ml

Lines changed: 90 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ struct
284284
| Switch_statement _
285285
| Try_statement _
286286
| Function_declaration _
287+
| Class_declaration _
287288
| Debugger_statement -> false
288289

289290
let starts_with ~obj ~funct ~let_identifier ~async_identifier l e =
@@ -315,6 +316,7 @@ struct
315316
| ERegexp _
316317
| EUn _
317318
| ENew _
319+
| EClass _
318320
| EYield _ -> false
319321
| CoverCallExpressionAndAsyncArrowHead e
320322
| CoverParenthesizedExpressionAndArrowParameterList e -> early_error e
@@ -420,6 +422,14 @@ struct
420422
| { async = false; generator = true } -> "function*"
421423
in
422424
function_declaration f prefix ident i l b pc
425+
| EClass (i, cl_decl) ->
426+
PP.string f "class";
427+
(match i with
428+
| None -> ()
429+
| Some i ->
430+
PP.space f;
431+
ident f i);
432+
class_declaration f cl_decl
423433
| EArrow (k, p, b, pc) ->
424434
if Prec.(l > AssignementExpression)
425435
then (
@@ -796,38 +806,43 @@ struct
796806
PP.break f;
797807
expression AssignementExpression f e;
798808
PP.end_group f
799-
| PropertyGet (n, (k, l, b, loc')) | PropertySet (n, (k, l, b, loc')) ->
800-
(match k with
801-
| { async = false; generator = false } -> ()
802-
| _ -> assert false);
803-
let prefix =
804-
match p with
805-
| PropertyGet _ -> "get"
806-
| PropertySet _ -> "set"
807-
| _ -> assert false
808-
in
809-
function_declaration f prefix property_name (Some n) l b loc'
810-
| PropertyMethod (n, (k, l, b, loc')) ->
811-
let fpn f () =
812-
(match k with
813-
| { async = false; generator = false } -> ()
814-
| { async = false; generator = true } ->
815-
PP.string f "*";
816-
PP.space f
817-
| { async = true; generator = false } ->
818-
PP.string f "async";
819-
PP.space f
820-
| { async = true; generator = true } ->
821-
PP.string f "async*";
822-
PP.space f);
823-
property_name f n
824-
in
825-
function_declaration f "" fpn (Some ()) l b loc'
826809
| PropertySpread e ->
827810
PP.string f "...";
828811
expression AssignementExpression f e
812+
| PropertyMethod m -> method_ f property_name m
829813
| CoverInitializedName (e, _, _) -> early_error e
830814

815+
and method_ : 'a. _ -> (PP.t -> 'a -> unit) -> 'a method_ -> unit =
816+
fun (type a) f (name : PP.t -> a -> unit) (m : a method_) ->
817+
match m with
818+
| MethodGet (n, (k, l, b, loc')) | MethodSet (n, (k, l, b, loc')) ->
819+
(match k with
820+
| { async = false; generator = false } -> ()
821+
| _ -> assert false);
822+
let prefix =
823+
match m with
824+
| MethodGet _ -> "get"
825+
| MethodSet _ -> "set"
826+
| _ -> assert false
827+
in
828+
function_declaration f prefix name (Some n) l b loc'
829+
| Method (n, (k, l, b, loc')) ->
830+
let fpn f () =
831+
(match k with
832+
| { async = false; generator = false } -> ()
833+
| { async = false; generator = true } ->
834+
PP.string f "*";
835+
PP.space f
836+
| { async = true; generator = false } ->
837+
PP.string f "async";
838+
PP.space f
839+
| { async = true; generator = true } ->
840+
PP.string f "async*";
841+
PP.space f);
842+
name f n
843+
in
844+
function_declaration f "" fpn (Some ()) l b loc'
845+
831846
and element_list f el = comma_list f element el
832847

833848
and element f (e : element) =
@@ -1037,6 +1052,11 @@ struct
10371052
| { async = false; generator = true } -> "function*"
10381053
in
10391054
function_declaration f prefix ident (Some i) l b loc'
1055+
| Class_declaration (i, cl_decl) ->
1056+
PP.string f "class";
1057+
PP.space f;
1058+
ident f i;
1059+
class_declaration f cl_decl
10401060
| Empty_statement -> PP.string f ";"
10411061
| Debugger_statement ->
10421062
PP.string f "debugger";
@@ -1446,6 +1466,49 @@ struct
14461466
PP.end_group f;
14471467
PP.end_group f
14481468
1469+
and class_declaration f x =
1470+
Option.iter x.extends ~f:(fun e ->
1471+
PP.space f;
1472+
PP.string f "extends";
1473+
PP.space f;
1474+
expression Expression f e);
1475+
PP.string f "{";
1476+
List.iter x.body ~f:(fun x ->
1477+
match x with
1478+
| CEMethod (static, m) ->
1479+
if static
1480+
then (
1481+
PP.string f "static";
1482+
PP.space f);
1483+
method_ f class_element_name m;
1484+
PP.break f
1485+
| CEField (static, n, i) ->
1486+
if static
1487+
then (
1488+
PP.string f "static";
1489+
PP.space f);
1490+
class_element_name f n;
1491+
(match i with
1492+
| None -> ()
1493+
| Some (e, loc) ->
1494+
PP.string f "=";
1495+
PP.space f;
1496+
output_debug_info f loc;
1497+
expression Expression f e);
1498+
PP.break f
1499+
| CEStaticBLock l ->
1500+
PP.string f "static";
1501+
block f l;
1502+
PP.break f);
1503+
PP.string f "}"
1504+
1505+
and class_element_name f x =
1506+
match x with
1507+
| PropName n -> property_name f n
1508+
| PrivName i ->
1509+
PP.string f "#";
1510+
ident f i
1511+
14491512
and program f s = statement_list f s
14501513
end
14511514

0 commit comments

Comments
 (0)