Skip to content

Commit 02f058f

Browse files
committed
WIP
1 parent 9f82e75 commit 02f058f

File tree

6 files changed

+72
-61
lines changed

6 files changed

+72
-61
lines changed

compiler/lib/javascript.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ module Num : sig
4242

4343
val is_neg : t -> bool
4444

45+
val is_int : t -> bool
46+
4547
(** Arithmetic *)
4648

4749
val add : t -> t -> t
@@ -134,6 +136,11 @@ end = struct
134136

135137
let is_neg s = Char.equal s.[0] '-'
136138

139+
let is_int s =
140+
String.for_all s ~f:(function
141+
| '0' .. '9' | '-' -> true
142+
| _ -> false)
143+
137144
let neg s =
138145
match String.drop_prefix s ~prefix:"-" with
139146
| None -> "-" ^ s

compiler/lib/javascript.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ module Num : sig
4343

4444
val is_neg : t -> bool
4545

46+
val is_int : t -> bool
47+
4648
(** Arithmetic *)
4749

4850
val add : t -> t -> t

compiler/lib/js_traverse.ml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,13 +1705,18 @@ class simpl =
17051705
match e with
17061706
| EBin (Plus, e1, e2) -> (
17071707
match e1, e2 with
1708-
| _, ENum n when Num.is_neg n -> EBin (Minus, e1, ENum (Num.neg n))
1709-
| ENum n, _ when Num.is_neg n -> EBin (Minus, e2, ENum (Num.neg n))
1710-
| ENum zero, (ENum _ as x) when Num.is_zero zero -> x
1711-
| (ENum _ as x), ENum zero when Num.is_zero zero -> x
1708+
| ENum n1, ENum n2 when Num.is_int n1 && Num.is_int n2 -> ENum (Num.add n1 n2)
1709+
| _, ENum n when Num.is_neg n ->
1710+
m#expression (EBin (Minus, e1, ENum (Num.neg n)))
1711+
| ENum n, _ when Num.is_neg n ->
1712+
m#expression (EBin (Minus, e2, ENum (Num.neg n)))
1713+
| ENum zero, x when Num.is_zero zero -> x
1714+
| x, ENum zero when Num.is_zero zero -> x
17121715
| _ -> e)
17131716
| EBin (Minus, e1, e2) -> (
17141717
match e1, e2 with
1718+
| EBin (Minus, e0, ENum n1), ENum n2 when Num.is_int n1 && Num.is_int n2 ->
1719+
EBin (Minus, e0, ENum (Num.add n1 n2))
17151720
| _, ENum n when Num.is_neg n -> EBin (Plus, e1, ENum (Num.neg n))
17161721
| (ENum _ as x), ENum zero when Num.is_zero zero -> x
17171722
| _ -> e)

compiler/tests-compiler/gh1354.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ with Exit ->
6767
catch(_e_){
6868
var _c_ = caml_wrap_exception(_e_);
6969
if(_c_ !== Stdlib[3]) throw caml_maybe_attach_backtrace(_c_, 0);
70-
caml_call2(Stdlib_Printf[3], _d_, _b_ + 0 | 0);
70+
caml_call2(Stdlib_Printf[3], _d_, _b_ | 0);
7171
var Test = [0];
7272
runtime.caml_register_global(3, Test, "Test");
7373
0;
@@ -148,14 +148,14 @@ with Exit ->
148148
throw caml_maybe_attach_backtrace(Stdlib[3], 1);
149149
}
150150
catch(_k_){
151-
caml_call3(Stdlib_Printf[3], _h_, _g_ + 0 | 0, _b_);
151+
caml_call3(Stdlib_Printf[3], _h_, _g_ | 0, _b_);
152152
throw caml_maybe_attach_backtrace(Stdlib[3], 1);
153153
}
154154
}
155155
catch(_j_){
156156
var _d_ = caml_wrap_exception(_j_);
157157
if(_d_ !== Stdlib[3]) throw caml_maybe_attach_backtrace(_d_, 0);
158-
caml_call3(Stdlib_Printf[3], _e_, _c_ + 0 | 0, _b_);
158+
caml_call3(Stdlib_Printf[3], _e_, _c_ | 0, _b_);
159159
var Test = [0];
160160
runtime.caml_register_global(4, Test, "Test");
161161
0;

compiler/tests-compiler/gh747.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ end
334334
114: /*<<test.ml:66:6>>*/ return caml_call2(Stdlib_Printf[1], outchan, _d_) /*<<test.ml:73:10>>*/ ;
335335
115: var
336336
116: a = /*<<test.ml:64:2>>*/ backtrace[1],
337-
117: _f_ = /*<<test.ml:69:6>>*/ a.length - 1 - 1 | 0,
337+
117: _f_ = /*<<test.ml:69:6>>*/ a.length - 2 | 0,
338338
118: _e_ = 0;
339339
119: if(_f_ >= 0){
340340
120: var i = _e_;

0 commit comments

Comments
 (0)