Skip to content

Commit a4e0dc9

Browse files
committed
Compiler: fix code generation for condition
1 parent 6fd97ab commit a4e0dc9

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

compiler/lib/js_output.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,6 @@ struct
787787
| Debugger_statement ->
788788
PP.string f "debugger";
789789
last_semi ()
790-
| Expression_statement (EVar _) -> last_semi ()
791790
| Expression_statement e ->
792791
(* Parentheses are required when the expression
793792
starts syntactically with "{" or "function" *)

compiler/lib/js_simpl.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ let rec if_statement_2 e loc iftrue truestop iffalse falsestop =
120120
let e = simplify_condition e in
121121
match fst iftrue, fst iffalse with
122122
(* Empty blocks *)
123-
| J.Block [], J.Block [] -> [ J.Expression_statement e, loc ]
123+
| J.Block [], J.Block [] -> (
124+
match e with
125+
| J.EVar _ -> []
126+
| _ -> [ J.Expression_statement e, loc ])
124127
| J.Block [], _ -> if_statement_2 (enot e) loc iffalse falsestop iftrue truestop
125128
| _, J.Block [] -> [ J.If_statement (e, iftrue, None), loc ]
126129
| _ -> (

compiler/tests-compiler/cond.ml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
(* Js_of_ocaml compiler
2+
* http://www.ocsigen.org/js_of_ocaml/
3+
u * Copyright (C) 2017 Hugo Heuzard
4+
* Copyright (C) 2019 Ty Overby
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, with linking exception;
9+
* either version 2.1 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19+
*)
20+
21+
open Util
22+
23+
let%expect_test "conditional" =
24+
let program =
25+
compile_and_parse
26+
{|
27+
let f a b c d e f =
28+
let x = match a,b,c,d,e,f with
29+
| true, false, false, false ,false, false -> 1
30+
| false, false, false, true, false, false -> 4
31+
| false, true, false, false, false ,false -> 2
32+
| false, false, true, false, false, false -> 3
33+
| false, false, false, false, true, false -> 5
34+
| false, false, false, false, false, true -> 6
35+
| false, false, false, false, false, false -> 100
36+
| true, true, _, _, _, _
37+
| _, true, _, true, _, _
38+
| _ -> -1
39+
in x + 2
40+
|}
41+
in
42+
print_fun_decl program (Some "f");
43+
[%expect
44+
{|
45+
function f(a,b,c,d,e,f)
46+
{var switch$0=0;
47+
if(a)
48+
{if(! b && ! c && ! d && ! e && ! f){var x=1;switch$0 = 1}}
49+
else
50+
if(b)
51+
{var switch$1=0;
52+
if(! c && ! d)
53+
if(e || f)switch$1 = 1;else{var x=2;switch$0 = 1;switch$1 = 1}}
54+
else
55+
if(c)
56+
{if(! d && ! e && ! f){var x=3;switch$0 = 1}}
57+
else
58+
if(d)
59+
{if(! e && ! f){var x=4;switch$0 = 1}}
60+
else
61+
if(e)
62+
{if(! f){var x=5;switch$0 = 1}}
63+
else
64+
if(f){var x=6;switch$0 = 1}else{var x=100;switch$0 = 1}
65+
if(! switch$0)var x=- 1;
66+
return x + 2 | 0} |}]

0 commit comments

Comments
 (0)