Skip to content

Commit 2cb684b

Browse files
author
Hongbo Zhang
committed
[refact & bug fix] type var difference from type constructor
1 parent a9a9076 commit 2cb684b

File tree

10 files changed

+291
-186
lines changed

10 files changed

+291
-186
lines changed

jscomp/syntax/ast_comb.ml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
let create_local_external loc
26+
?(pval_attributes=[])
27+
~pval_prim
28+
~pval_type
29+
?(local_module_name = "J")
30+
?(local_fun_name = "unsafe_expr")
31+
args
32+
: Parsetree.expression_desc =
33+
Pexp_letmodule
34+
({txt = local_module_name; loc},
35+
{pmod_desc =
36+
Pmod_structure
37+
[{pstr_desc =
38+
Pstr_primitive
39+
{pval_name = {txt = local_fun_name; loc};
40+
pval_type ;
41+
pval_loc = loc;
42+
pval_prim = [pval_prim];
43+
pval_attributes };
44+
pstr_loc = loc;
45+
}];
46+
pmod_loc = loc;
47+
pmod_attributes = []},
48+
{
49+
pexp_desc =
50+
Pexp_apply
51+
(({pexp_desc = Pexp_ident {txt = Ldot (Lident local_module_name, local_fun_name);
52+
loc};
53+
pexp_attributes = [] ;
54+
pexp_loc = loc} : Parsetree.expression),
55+
args);
56+
pexp_attributes = [];
57+
pexp_loc = loc
58+
})

jscomp/syntax/parsetree_util.mli renamed to jscomp/syntax/ast_comb.mli

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,10 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
26-
27-
28-
29-
30-
31-
32-
(* A utility module used when destructuring parsetree attributes, used for
33-
compiling FFI code
34-
*)
35-
36-
(* val is_single_string : Parsetree.payload -> string option *)
37-
38-
(* val is_string_or_strings : Parsetree.payload -> [ `None | `Single of string | `Some of string list ] *)
39-
25+
val create_local_external : Location.t ->
26+
?pval_attributes:Parsetree.attributes ->
27+
pval_prim:string ->
28+
pval_type:Parsetree.core_type ->
29+
?local_module_name:string ->
30+
?local_fun_name:string ->
31+
(Asttypes.label * Parsetree.expression) list -> Parsetree.expression_desc

jscomp/syntax/parsetree_util.ml renamed to jscomp/syntax/ast_lift.ml

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,56 +22,5 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
26-
27-
28-
29-
30-
31-
32-
let is_single_string (x : Parsetree.payload ) =
33-
match x with (** TODO also need detect empty phrase case *)
34-
| Parsetree.PStr [ {
35-
pstr_desc =
36-
Pstr_eval (
37-
{pexp_desc =
38-
Pexp_constant
39-
(Const_string (name,_));
40-
_},_);
41-
_}] -> Some name
42-
| _ -> None
43-
44-
let is_string_or_strings (x : Parsetree.payload ) :
45-
[ `None | `Single of string | `Some of string list ] =
46-
let module M = struct exception Not_str end in
47-
match x with
48-
| PStr [ {pstr_desc =
49-
Pstr_eval (
50-
{pexp_desc =
51-
Pexp_apply
52-
({pexp_desc = Pexp_constant (Const_string (name,_)); _},
53-
args
54-
);
55-
_},_);
56-
_}] ->
57-
(try
58-
`Some (name :: (args |> List.map (fun (_label,e) ->
59-
match (e : Parsetree.expression) with
60-
| {pexp_desc = Pexp_constant (Const_string (name,_)); _} ->
61-
name
62-
| _ -> raise M.Not_str)))
63-
64-
with M.Not_str -> `None )
65-
| Parsetree.PStr [ {
66-
pstr_desc =
67-
Pstr_eval (
68-
{pexp_desc =
69-
Pexp_constant
70-
(Const_string (name,_));
71-
_},_);
72-
_}] -> `Single name
73-
| _ -> `None
74-
75-
let lift_int ?loc ?attrs x =
25+
let int ?loc ?attrs x =
7626
Ast_helper.Exp.constant ?loc ?attrs (Const_int x)
77-

jscomp/syntax/ast_lift.mli

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
val int :
26+
?loc:Location.t ->
27+
?attrs:Parsetree.attributes -> int -> Parsetree.expression

jscomp/syntax/ast_literal.ml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
module Lid = struct
26+
type t = Longident.t
27+
let val_unit : t = Lident "()"
28+
let type_unit : t = Lident "unit"
29+
let type_string : t = Lident "string"
30+
(* TODO should be renamed in to {!Js.fn} *)
31+
(* TODO should be moved into {!Js.t} Later *)
32+
let pervasives_js_obj = Longident.Ldot (Lident "Pervasives", "js_obj")
33+
let pervasives_uncurry = Longident.Ldot (Lident "Pervasives", "uncurry")
34+
let js_obj = Longident.Ldot (Lident "Js", "t")
35+
let js_fn = Longident.Ldot (Lident "Js", "fn")
36+
let ignore_id = Longident.Ldot (Lident "Pervasives", "ignore")
37+
end
38+
39+
module No_loc = struct
40+
let loc = Location.none
41+
let val_unit =
42+
Ast_helper.Exp.construct {txt = Lid.val_unit; loc } None
43+
let type_unit =
44+
Ast_helper.Typ.mk (Ptyp_constr ({ txt = Lid.type_unit; loc}, []))
45+
46+
let type_string =
47+
Ast_helper.Typ.mk (Ptyp_constr ({ txt = Lid.type_string; loc}, []))
48+
49+
let type_any = Ast_helper.Typ.any ()
50+
end
51+
52+
type 'a lit = ?loc: Location.t -> unit -> 'a
53+
type expression_lit = Parsetree.expression lit
54+
type core_type_lit = Parsetree.core_type lit
55+
let val_unit ?loc () =
56+
match loc with
57+
| None -> No_loc.val_unit
58+
| Some loc -> Ast_helper.Exp.construct {txt = Lid.val_unit; loc} None
59+
60+
61+
let type_unit ?loc () =
62+
match loc with
63+
| None ->
64+
No_loc.type_unit
65+
| Some loc ->
66+
Ast_helper.Typ.mk ~loc (Ptyp_constr ({ txt = Lid.type_unit; loc}, []))
67+
68+
69+
let type_string ?loc () =
70+
match loc with
71+
| None -> No_loc.type_string
72+
| Some loc ->
73+
Ast_helper.Typ.mk ~loc (Ptyp_constr ({ txt = Lid.type_string; loc}, []))
74+
75+
let type_any ?loc () =
76+
match loc with
77+
| None -> No_loc.type_any
78+
| Some loc -> Ast_helper.Typ.any ~loc ()

jscomp/syntax/ast_literal.mli

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
type 'a lit = ?loc: Location.t -> unit -> 'a
27+
module Lid : sig
28+
type t = Longident.t
29+
val val_unit : t
30+
val type_unit : t
31+
val pervasives_js_obj : t
32+
val pervasives_uncurry : t
33+
val js_obj : t
34+
val js_fn : t
35+
val ignore_id : t
36+
end
37+
38+
type expression_lit = Parsetree.expression lit
39+
type core_type_lit = Parsetree.core_type lit
40+
41+
val val_unit : expression_lit
42+
43+
val type_unit : core_type_lit
44+
45+
val type_string : core_type_lit
46+
47+
val type_any : core_type_lit
48+
49+

jscomp/syntax/ast_payload.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ let is_single_string (x : t ) =
3636
_}] -> Some name
3737
| _ -> None
3838

39+
let as_string_exp (x : t ) =
40+
match x with (** TODO also need detect empty phrase case *)
41+
| PStr [ {
42+
pstr_desc =
43+
Pstr_eval (
44+
{pexp_desc =
45+
Pexp_constant
46+
(Const_string (_,_));
47+
_} as e ,_);
48+
_}] -> Some e
49+
| _ -> None
3950

4051
let is_string_or_strings (x : t) :
4152
[ `None | `Single of string | `Some of string list ] =

jscomp/syntax/ast_payload.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
type t = Parsetree.payload
3131

3232
val is_single_string : t -> string option
33+
val as_string_exp : t -> Parsetree.expression option
3334

3435
val is_string_or_strings :
3536
t -> [ `None | `Single of string | `Some of string list ]

0 commit comments

Comments
 (0)