Skip to content

Commit bca090c

Browse files
committed
finish, no regression found
Note: we need adapt `typeof polyvar === "number"` into `typeof polyvar === "string"` or maybe eliminate it completely
1 parent 6b31db7 commit bca090c

20 files changed

+421
-340
lines changed

jscomp/core/bs_conditional_initial.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let setup_env () =
3030
Translcore.wrap_single_field_record := Transl_single_field_record.wrap_single_field_record;
3131
Translmod.eval_rec_bindings := Compile_rec_module.eval_rec_bindings;
3232
Typemod.should_hide := Typemod_hide.should_hide;
33-
#if 0 then
33+
#if 1 then
3434
Matching.make_test_sequence_variant_constant := Polyvar_pattern_match.make_test_sequence_variant_constant;
3535
Matching.call_switcher_variant_constant := Polyvar_pattern_match.call_switcher_variant_constant;
3636
Matching.call_switcher_variant_constr := Polyvar_pattern_match.call_switcher_variant_constr;

jscomp/core/polyvar_pattern_match.ml

Lines changed: 111 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,59 +22,139 @@
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+
type lam = Lambda.lambda
2526

26-
27-
let make_test_sequence_variant_constant
28-
(fail : Lambda.lambda option) (arg : Lambda.lambda)
29-
(int_lambda_list : (int * (string * Lambda.lambda) ) list) : Lambda.lambda=
27+
28+
module Map_lambda = struct
29+
open Map_gen
30+
type key =
31+
| Bottom of int
32+
| Normalized of lam
33+
34+
let bottom_id = ref (-1)
35+
let bottom () = incr bottom_id ; Bottom !bottom_id
36+
let compare_key (x : key) (y : key) = Pervasives.compare x y
37+
let old_order = ref 1
38+
let next_id () =
39+
incr old_order; !old_order
40+
let rec adjust (tree : _ Map_gen.t as 'a) x replace : 'a =
41+
match tree with
42+
| Empty ->
43+
singleton x (replace None)
44+
| Leaf {k ; v} ->
45+
let c = compare_key x k in
46+
if c = 0 then singleton x (replace (Some v)) else
47+
if c < 0 then
48+
Map_gen.unsafe_two_elements x (replace None) k v
49+
else
50+
Map_gen.unsafe_two_elements k v x (replace None)
51+
| Node ({l; k ; r} as tree) ->
52+
let c = compare_key x k in
53+
if c = 0 then
54+
Map_gen.unsafe_node x (replace (Some tree.v)) l r tree.h
55+
else if c < 0 then
56+
bal (adjust l x replace ) k tree.v r
57+
else
58+
bal l k tree.v (adjust r x replace )
59+
60+
61+
let of_list (int_lambda_list : (int * (string * lam)) list) : (key, (int * string) list * lam * int ) t=
62+
Ext_list.fold_left int_lambda_list empty (fun acc (hash,(name,lam)) ->
63+
let key =
64+
match Lambda.make_key lam with
65+
| None -> bottom ()
66+
| Some key -> Normalized key in
67+
adjust acc key (function
68+
| None -> [hash, name], lam, next_id ()
69+
| Some (acc,action,stamp) -> (hash,name) :: acc, action, stamp
70+
))
71+
let rec values_aux s acc =
72+
match s with
73+
| Empty -> acc
74+
| Leaf {v} -> v :: acc
75+
| Node {l;v;r} ->
76+
values_aux l (v ::values_aux r acc)
77+
let values s : ((int * string) list * lam) list =
78+
Ext_list.sort_via_arrayf ( values_aux s [])
79+
(fun (_,_,d0) (_,_,d1) -> compare d0 d1)
80+
(fun (a,b,_) -> (a,b))
81+
82+
end
83+
84+
let or_list (arg : lam) (hash_names : (int * string) list) =
85+
match hash_names with
86+
| (hash,name):: rest ->
87+
let init : lam =
88+
Lprim(Pintcomp Ceq,
89+
[arg; Lconst ((Const_pointer (hash, Pt_variant{name})))],
90+
Location.none) in
91+
Ext_list.fold_left rest init (fun acc (hash,name) ->
92+
Lambda.Lprim
93+
(Psequor ,
94+
[acc ;
95+
Lprim(Pintcomp Ceq,
96+
[arg;
97+
Lconst ((Const_pointer (hash, Pt_variant{name})))],
98+
Location.none)], Location.none)
99+
)
100+
| _ -> assert false
101+
102+
let make_test_sequence_variant_constant
103+
(fail : lam option) (arg : lam)
104+
(int_lambda_list : (int * (string * lam) ) list) : lam =
105+
let int_lambda_list : ((int * string) list * lam) list =
106+
Map_lambda.(values (of_list int_lambda_list)) in
107+
match int_lambda_list, fail with
108+
| (_, act) :: rest, None
109+
| rest, Some act ->
110+
Ext_list.fold_right rest act (fun (hash_names,act1) acc ->
111+
let predicate : lam = or_list arg hash_names in
112+
Lifthenelse (predicate,act1, acc))
113+
| [], None -> assert false
114+
115+
let make_test_sequence_variant_constant_2
116+
(fail : lam option) (arg : lam)
117+
(int_lambda_list : (int * (string * lam) ) list) : lam=
30118
match int_lambda_list, fail with
31-
| (_, (_,act)) :: rest, None ->
119+
| (_, (_,act)) :: rest, None
120+
| rest , Some act ->
32121
Ext_list.fold_right rest act (fun (hash1,(name,act1)) acc ->
33-
Lifthenelse (Lprim(Pintcomp Ceq,
34-
[arg; Lconst ((Const_pointer (hash1, Pt_variant{name})))], Location.none),
35-
act1, acc
36-
)
37-
)
38-
| _, Some fail ->
39-
Ext_list.fold_right int_lambda_list fail (fun (hash1,(name,act1)) acc ->
40122
Lifthenelse (Lprim(Pintcomp Ceq,
41123
[arg; Lconst (Const_pointer(hash1, Pt_variant{name}))], Location.none),
42124
act1, acc
43125
)
44126
)
45127
| [], None -> assert false
46128

47-
let call_switcher_variant_constant
129+
let call_switcher_variant_constant
48130
(_loc : Location.t)
49-
(fail : Lambda.lambda option)
50-
(arg : Lambda.lambda)
51-
(int_lambda_list : (int * (string * Lambda.lambda)) list)
131+
(fail : lam option)
132+
(arg : lam)
133+
(int_lambda_list : (int * (string * lam)) list)
52134
(_names : Lambda.switch_names option) =
53-
Ext_log.dwarn ~__POS__ "%a@." Ext_obj.pp_any _names;
135+
136+
let int_lambda_list : ((int * string) list * lam) list =
137+
Map_lambda.(values (of_list int_lambda_list)) in
54138
match int_lambda_list, fail with
55-
| (_, (_,act)) :: rest, None ->
56-
Ext_list.fold_right rest act (fun (hash1,(name,act1)) acc ->
57-
Lifthenelse (Lprim(Pintcomp Ceq,
58-
[arg; Lconst (Const_pointer(hash1, Pt_variant{name}))], Location.none),
59-
act1, acc
60-
)
61-
)
62-
| _, Some fail ->
63-
Ext_list.fold_right int_lambda_list fail (fun (hash1,(name,act1)) acc ->
64-
Lifthenelse (Lprim(Pintcomp Ceq,
65-
[arg; Lconst (Const_pointer(hash1, Pt_variant{name}))], Location.none),
139+
| (_,act) :: rest, None
140+
| rest, Some act ->
141+
Ext_list.fold_right rest act (fun (hash_names,act1) acc ->
142+
let predicate = or_list arg hash_names in
143+
Lifthenelse (predicate,
66144
act1, acc
67145
)
68146
)
69147
| [], None -> assert false
70148

71149

150+
151+
72152
let call_switcher_variant_constr
73153
(loc : Location.t)
74-
(fail : Lambda.lambda option)
75-
(arg : Lambda.lambda)
154+
(fail : lam option)
155+
(arg : lam)
76156
int_lambda_list
77-
(names : Lambda.switch_names option) : Lambda.lambda =
157+
(names : Lambda.switch_names option) : lam =
78158
let v = Ident.create "variant" in
79159
Llet(Alias, Pgenval, v, Lprim(Pfield (0, Fld_poly_var_tag), [arg], loc),
80160
call_switcher_variant_constant

jscomp/test/bb.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33

44
function f(x) {
5-
if (x !== 98) {
6-
if (x >= 99) {
7-
return "c";
8-
} else {
9-
return "a";
10-
}
11-
} else {
5+
if (x === /* b */98) {
126
return "b";
7+
} else if (x === /* c */99) {
8+
return "c";
9+
} else {
10+
return "a";
1311
}
1412
}
1513

@@ -57,14 +55,12 @@ function test(x) {
5755
Error: new Error()
5856
};
5957
}
60-
if (match !== 98) {
61-
if (match >= 99) {
62-
return "c";
63-
} else {
64-
return "a";
65-
}
66-
} else {
58+
if (match === /* b */98) {
6759
return "b";
60+
} else if (match === /* c */99) {
61+
return "c";
62+
} else {
63+
return "a";
6864
}
6965
}
7066

jscomp/test/const_test.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ function fff(x) {
2626
}
2727

2828
function h(x) {
29-
if (x !== 66) {
30-
if (x >= 67) {
31-
return 2;
32-
} else {
33-
return 0;
34-
}
35-
} else {
29+
if (x === /* B */66) {
3630
return 1;
31+
} else if (x === /* C */67) {
32+
return 2;
33+
} else {
34+
return 0;
3735
}
3836
}
3937

jscomp/test/ext_filename_test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ function chop_extension_if_any(fname) {
115115
var os_path_separator_char = Filename.dir_sep.charCodeAt(0);
116116

117117
function relative_path(file_or_dir_1, file_or_dir_2) {
118-
var relevant_dir1 = file_or_dir_1.HASH >= 781515420 ? Curry._1(Filename.dirname, file_or_dir_1.VAL) : file_or_dir_1.VAL;
119-
var relevant_dir2 = file_or_dir_2.HASH >= 781515420 ? Curry._1(Filename.dirname, file_or_dir_2.VAL) : file_or_dir_2.VAL;
118+
var relevant_dir1 = file_or_dir_1.HASH === /* File */781515420 ? Curry._1(Filename.dirname, file_or_dir_1.VAL) : file_or_dir_1.VAL;
119+
var relevant_dir2 = file_or_dir_2.HASH === /* File */781515420 ? Curry._1(Filename.dirname, file_or_dir_2.VAL) : file_or_dir_2.VAL;
120120
var dir1 = Ext_string_test.split(undefined, relevant_dir1, os_path_separator_char);
121121
var dir2 = Ext_string_test.split(undefined, relevant_dir2, os_path_separator_char);
122122
var go = function (_dir1, _dir2) {
@@ -149,13 +149,13 @@ function node_relative_path(node_modules_shorten, file1, dep_file) {
149149
var v = Ext_string_test.find(undefined, Test_literals.node_modules, file2);
150150
var len = file2.length;
151151
if (!(node_modules_shorten && v >= 0)) {
152-
return relative_path(dep_file.HASH >= 781515420 ? ({
152+
return relative_path(dep_file.HASH === /* File */781515420 ? ({
153153
HASH: /* File */781515420,
154154
VAL: absolute_path(dep_file.VAL)
155155
}) : ({
156156
HASH: /* Dir */3405101,
157157
VAL: absolute_path(dep_file.VAL)
158-
}), file1.HASH >= 781515420 ? ({
158+
}), file1.HASH === /* File */781515420 ? ({
159159
HASH: /* File */781515420,
160160
VAL: absolute_path(file1.VAL)
161161
}) : ({

jscomp/test/gpr_1891_test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var Curry = require("../../lib/js/curry.js");
44

55
function foo(x) {
6-
if (typeof x === "number" || x.HASH !== 3505894 || x.VAL !== 3) {
6+
if (typeof x === "number" || !(x.HASH === /* Foo */3505894 && x.VAL === 3)) {
77
console.log("2");
88
} else {
99
console.log("1");
@@ -12,31 +12,31 @@ function foo(x) {
1212
}
1313

1414
function foo2(x) {
15-
if (typeof x === "number" || x.HASH !== 3505894 || x.VAL !== 3) {
15+
if (typeof x === "number" || !(x.HASH === /* Foo */3505894 && x.VAL === 3)) {
1616
return "xxx";
1717
} else {
1818
return "xxxx";
1919
}
2020
}
2121

2222
function foo3(x) {
23-
if (typeof x === "number" || x.HASH !== 3505894 || x.VAL !== 3) {
23+
if (typeof x === "number" || !(x.HASH === /* Foo */3505894 && x.VAL === 3)) {
2424
return 2;
2525
} else {
2626
return 1;
2727
}
2828
}
2929

3030
function foo4(x, h) {
31-
if (typeof x === "number" || x.HASH !== 3505894 || x.VAL !== 3) {
31+
if (typeof x === "number" || !(x.HASH === /* Foo */3505894 && x.VAL === 3)) {
3232
return ;
3333
} else {
3434
return Curry._1(h, undefined);
3535
}
3636
}
3737

3838
function foo5(x) {
39-
if (typeof x === "number" || x.HASH !== 3505894 || x.VAL !== 3) {
39+
if (typeof x === "number" || !(x.HASH === /* Foo */3505894 && x.VAL === 3)) {
4040
console.log("x");
4141
} else {
4242
console.log("hi");

0 commit comments

Comments
 (0)