Skip to content

Commit 795becf

Browse files
committed
Doc comments and changelog.
1 parent 54fc0b5 commit 795becf

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- Fix variant cast to int. https://github.com/rescript-lang/rescript-compiler/pull/7058
4141
- Fix comments formatted away in function without arguments. https://github.com/rescript-lang/rescript-compiler/pull/7095
4242
- Fix genType JSX component compilation. https://github.com/rescript-lang/rescript-compiler/pull/7107
43+
- Fix and clean up boolean and/or optimizations. https://github.com/rescript-lang/rescript-compiler/pull/7134
4344

4445
#### :nail_care: Polish
4546

compiler/core/js_exp_make.ml

+32-4
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,28 @@ let bin ?comment (op : J.binop) (e0 : t) (e1 : t) : t =
665665
let string_of_expression = ref (fun _ -> "")
666666
let debug = false
667667

668+
(**
669+
[simplify_and e1 e2] attempts to simplify the boolean AND expression [e1 && e2].
670+
Returns [Some simplified] if simplification is possible, [None] otherwise.
671+
672+
Basic simplification rules:
673+
- [false && e] -> [false]
674+
- [true && e] -> [e]
675+
- [e && false] -> [false]
676+
- [e && true] -> [e]
677+
- [(a && b) && e] -> If either [a && e] or [b && e] can be simplified,
678+
creates new AND expression with simplified parts: [(a' && b')]
679+
- [(a || b) && e] -> If either [a && e] or [b && e] can be simplified,
680+
creates new OR expression with simplified parts: [(a' || b')]
681+
682+
Type check optimizations:
683+
- [(typeof x === "boolean") && (x === true/false)] -> [x === true/false]
684+
- [(typeof x === "string" || Array.isArray(x)) && (x === boolean/null/undefined)] -> [false]
685+
686+
Note: The function preserves the semantics of the original expression while
687+
attempting to reduce it to a simpler form. If no simplification is possible,
688+
returns [None].
689+
*)
668690
let rec simplify_and (e1 : t) (e2 : t) : t option =
669691
if debug then
670692
Printf.eprintf "simplify_and %s %s\n" (!string_of_expression e1)
@@ -771,6 +793,16 @@ let rec simplify_and (e1 : t) (e2 : t) : t option =
771793
Some false_
772794
| _ -> None
773795

796+
(**
797+
[simplify_or e1 e2] attempts to simplify the boolean OR expression [e1 || e2].
798+
Returns [Some simplified] if simplification is possible, [None] otherwise.
799+
800+
Basic simplification rules:
801+
- [true || e] -> [true]
802+
- [e || true] -> [true]
803+
- [false || e] -> [e]
804+
- [e || false] -> [e]
805+
*)
774806
and simplify_or (e1 : t) (e2 : t) : t option =
775807
if debug then
776808
Printf.eprintf "simplify_or %s %s\n" (!string_of_expression e1)
@@ -817,10 +849,6 @@ let or_ ?comment (e1 : t) (e2 : t) =
817849
| Some e -> e
818850
| None -> {expression_desc = Bin (Or, e1, e2); comment})
819851

820-
(* return a value of type boolean *)
821-
(* TODO:
822-
when comparison with Int
823-
it is right that !(x > 3 ) -> x <= 3 *)
824852
let not (e : t) : t =
825853
match e.expression_desc with
826854
| Number (Int {i; _}) -> bool (i = 0l)

0 commit comments

Comments
 (0)