Skip to content

Commit b508b0d

Browse files
authored
Merge pull request #3747 from BuckleScript/clean
add bsc -e "let a = 3"
2 parents 176ccea + 0f08413 commit b508b0d

20 files changed

+32243
-32267
lines changed

jscomp/core/js_implementation.ml

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ let interface_mliast ppf fname outputprefix =
9292
|> print_if ppf Clflags.dump_source Pprintast.signature
9393
|> after_parsing_sig ppf outputprefix
9494

95+
96+
let get_lambda = fun
97+
#if OCAML_VERSION =~ ">4.03.0" then
98+
{code = lambda}
99+
#else
100+
lambda
101+
#end -> lambda
102+
95103
let after_parsing_impl ppf outputprefix ast =
96104

97105
if !Js_config.binary_ast then
@@ -104,45 +112,37 @@ let after_parsing_impl ppf outputprefix ast =
104112
Warnings.check_fatal ()
105113
else
106114
begin
107-
108115
if Js_config.get_diagnose () then
109116
Format.fprintf Format.err_formatter "Building %s@." !Location.input_name;
110-
let modulename = Compenv.module_of_filename ppf !Location.input_name outputprefix in
117+
let modulename = Ext_filename.module_name outputprefix in
111118
Lam_compile_env.reset () ;
112119
let env = Compmisc.initial_env() in
113120
Env.set_unit_name modulename;
114-
115-
let (typedtree, coercion, finalenv, current_signature) =
116-
ast
117-
|> Typemod.type_implementation_more ?check_exists:(if !Js_config.force_cmi then None else Some ()) !Location.input_name outputprefix modulename env
118-
|> print_if ppf Clflags.dump_typedtree
119-
(fun fmt (ty,co,_,_) -> Printtyped.implementation_with_coercion fmt (ty,co))
120-
in
121-
if !Clflags.print_types || !Js_config.cmi_only then begin
122-
Warnings.check_fatal ();
123-
end else begin
124-
(typedtree, coercion)
125-
|> Translmod.transl_implementation modulename
126121

127-
|> (fun
128-
#if OCAML_VERSION =~ ">4.03.0" then
129-
{code = lambda}
130-
#else
131-
lambda
132-
#end
133-
->
134-
ignore (print_if ppf Clflags.dump_rawlambda Printlambda.lambda lambda);
135-
Lam_compile_main.lambda_as_module
136-
finalenv
137-
outputprefix lambda
138-
);
139-
140-
end;
122+
let (typedtree, coercion, finalenv, current_signature) =
123+
ast
124+
|> Typemod.type_implementation_more ?check_exists:(if !Js_config.force_cmi then None else Some ()) !Location.input_name outputprefix modulename env
125+
|> print_if ppf Clflags.dump_typedtree
126+
(fun fmt (ty,co,_,_) -> Printtyped.implementation_with_coercion fmt (ty,co))
127+
in
128+
if !Clflags.print_types || !Js_config.cmi_only then begin
129+
Warnings.check_fatal ();
130+
end else begin
131+
(typedtree, coercion)
132+
|> Translmod.transl_implementation modulename
133+
|> (fun lambda ->
134+
print_if ppf Clflags.dump_rawlambda Printlambda.lambda (get_lambda lambda)
135+
|>
136+
Lam_compile_main.lambda_as_module
137+
finalenv
138+
outputprefix
139+
);
140+
end;
141141
Stypes.dump (Some (outputprefix ^ ".annot"));
142142
end
143-
let implementation ppf sourcefile outputprefix =
143+
let implementation ppf fname outputprefix =
144144
Compmisc.init_path false;
145-
Ocaml_parse.parse_implementation ppf sourcefile
145+
Ocaml_parse.parse_implementation ppf fname
146146
|> print_if ppf Clflags.dump_parsetree Printast.implementation
147147
|> print_if ppf Clflags.dump_source Pprintast.structure
148148
|> after_parsing_impl ppf outputprefix

jscomp/core/ocaml_parse.ml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ let parse_interface ppf sourcefile =
3030
Ppx_entry.rewrite_signature (Pparse.parse_interface ~tool_name:Js_config.tool_name ppf sourcefile)
3131

3232

33-
let lazy_parse_interface ppf sourcefile =
34-
lazy (parse_interface ppf sourcefile)
3533

3634
let parse_implementation ppf sourcefile =
3735
Ppx_entry.rewrite_implementation
@@ -44,17 +42,6 @@ let parse_implementation ppf sourcefile =
4442
#end
4543
)
4644

47-
let parse_implementation_from_string str =
48-
let lb = Lexing.from_string str in
49-
let fname = "//toplevel//" in
50-
Location.input_name := fname;
51-
Location.init lb fname;
52-
Ppx_entry.rewrite_implementation (Parse.implementation lb)
53-
54-
55-
let lazy_parse_implementation ppf sourcefile =
56-
lazy (parse_implementation ppf sourcefile)
57-
5845
type valid_input =
5946
| Ml
6047
| Mli

jscomp/core/ocaml_parse.mli

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
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-
val parse_interface : Format.formatter -> string -> Parsetree.signature
25+
val parse_interface :
26+
Format.formatter ->
27+
string ->
28+
Parsetree.signature
2629

27-
val parse_implementation : Format.formatter -> string -> Parsetree.structure
30+
val parse_implementation :
31+
Format.formatter ->
32+
string ->
33+
Parsetree.structure
2834

29-
val parse_implementation_from_string : string -> Parsetree.structure
30-
val lazy_parse_interface : Format.formatter -> string -> Parsetree.signature lazy_t
31-
32-
val lazy_parse_implementation : Format.formatter -> string -> Parsetree.structure lazy_t
3335

3436
type valid_input =
3537
| Ml

jscomp/ext/ext_io.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
(** on 32 bit , there are 16M limitation *)
2727
let load_file f =
28-
Ext_pervasives.finally (open_in_bin f) close_in begin fun ic ->
28+
Ext_pervasives.finally (open_in_bin f) ~clean:close_in begin fun ic ->
2929
let n = in_channel_length ic in
3030
let s = Bytes.create n in
3131
really_input ic s 0 n;
@@ -42,10 +42,13 @@ let rev_lines_of_chann chan =
4242

4343

4444
let rev_lines_of_file file =
45-
Ext_pervasives.finally (open_in_bin file) close_in rev_lines_of_chann
46-
45+
Ext_pervasives.finally
46+
~clean:close_in
47+
(open_in_bin file) rev_lines_of_chann
48+
4749

4850
let write_file f content =
49-
Ext_pervasives.finally (open_out_bin f) close_out begin fun oc ->
51+
Ext_pervasives.finally ~clean:close_out
52+
(open_out_bin f) begin fun oc ->
5053
output_string oc content
5154
end

jscomp/ext/ext_pervasives.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
external reraise: exn -> 'a = "%reraise"
3131

32-
let finally v action f =
32+
let finally v ~clean:action f =
3333
match f v with
3434
| exception e ->
3535
action v ;
@@ -40,10 +40,10 @@ let try_it f =
4040
try ignore (f ()) with _ -> ()
4141

4242
let with_file_as_chan filename f =
43-
finally (open_out_bin filename) close_out f
43+
finally (open_out_bin filename) ~clean:close_out f
4444

4545
let with_file_as_pp filename f =
46-
finally (open_out_bin filename) close_out
46+
finally (open_out_bin filename) ~clean:close_out
4747
(fun chan ->
4848
let fmt = Format.formatter_of_out_channel chan in
4949
let v = f fmt in

jscomp/ext/ext_pervasives.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434

3535
external reraise: exn -> 'a = "%reraise"
3636

37-
val finally : 'a -> ('a -> 'c) -> ('a -> 'b) -> 'b
37+
val finally :
38+
'a ->
39+
clean:('a -> 'c) ->
40+
('a -> 'b) -> 'b
3841

3942
val try_it : (unit -> 'a) -> unit
4043

jscomp/ext/ext_pp.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ let group t i action =
9797
else
9898
let old = t.indent_level in
9999
t.indent_level <- t.indent_level + i;
100-
Ext_pervasives.finally () (fun _ -> t.indent_level <- old) action
100+
Ext_pervasives.finally ~clean:(fun _ -> t.indent_level <- old)
101+
() action
101102

102103
let vgroup = group
103104

jscomp/main/js_main.ml

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,14 @@ let intf filename =
9393
#end
9494
; process_interface_file ppf filename;;
9595

96-
let eval_string = ref ""
97-
98-
let set_eval_string s =
99-
eval_string := s
100-
10196

10297

98+
let eval (s : string) ~suffix =
99+
let tmpfile = Filename.temp_file "eval" suffix in
100+
Ext_io.write_file tmpfile s;
101+
Ext_pervasives.finally tmpfile anonymous ~clean:(fun _ ->
102+
try Sys.remove tmpfile with _ -> ()
103+
)
103104

104105
let (//) = Filename.concat
105106

@@ -186,8 +187,13 @@ let buckle_script_flags : (string * Arg.spec * string) list =
186187
" disable binary annotations (by default on)")
187188
::
188189
("-bs-eval",
189-
Arg.String set_eval_string,
190-
" (experimental) Set the string to be evaluated, note this flag will be conflicted with -bs-main"
190+
Arg.String (fun s -> eval s ~suffix:Literals.suffix_ml),
191+
" (experimental) Set the string to be evaluated in OCaml syntax"
192+
)
193+
::
194+
("-e",
195+
Arg.String (fun s -> eval s ~suffix:Literals.suffix_re),
196+
" (experimental) Set the string to be evaluated in ReasonML syntax"
191197
)
192198
::
193199
(
@@ -281,37 +287,6 @@ let buckle_script_flags : (string * Arg.spec * string) list =
281287
:: Ocaml_options.ocaml_options
282288

283289

284-
let print_if ppf flag printer arg =
285-
if !flag then Format.fprintf ppf "%a@." printer arg;
286-
arg
287-
288-
let eval ppf s =
289-
#if OCAML_VERSION =~ ">4.03.0" then
290-
Compenv.readenv ppf (Before_compile "//<toplevel>//"); (*FIXME*)
291-
#else
292-
Compenv.readenv ppf Before_compile;
293-
#end
294-
Compmisc.init_path false;
295-
Ext_ref.protect_list
296-
[Clflags.dont_write_files , true ;
297-
Clflags.annotations, false;
298-
Clflags.binary_annotations, false;
299-
] (fun _ ->
300-
Ocaml_parse.parse_implementation_from_string s
301-
(* FIXME: Note in theory, the order of applying our built in ppx
302-
and apply third party ppx should not matter, but in practice
303-
it may.
304-
We should make it more consistent.
305-
Thirdy party ppx may be buggy to drop annotations.
306-
If we always put our ppx in the beginning, it will be more robust,
307-
however, the current implementation (in the batch compilation mode)
308-
seems to apply our ppx after all ppx transformations
309-
*)
310-
|> Pparse.apply_rewriters_str ~tool_name:Js_config.tool_name
311-
|> print_if ppf Clflags.dump_parsetree Printast.implementation
312-
|> print_if ppf Clflags.dump_source Pprintast.structure
313-
|> Js_implementation.after_parsing_impl ppf "Bs_internal_eval"
314-
)
315290

316291

317292

@@ -335,11 +310,7 @@ let _ =
335310
Bs_conditional_initial.setup_env ();
336311
try
337312
Compenv.readenv ppf Before_args;
338-
Arg.parse buckle_script_flags anonymous usage;
339-
let eval_string = !eval_string in
340-
if eval_string <> "" then
341-
eval ppf eval_string
342-
313+
Arg.parse buckle_script_flags anonymous usage
343314
with x ->
344315
begin
345316
#if undefined BS_RELEASE_BUILD then

jscomp/main/jscmj_main.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let from_cmj (files : string list) (output_file : string) =
4646
Ext_pp.string f "\""
4747
in
4848
let v = open_out_bin output_file in
49-
Ext_pervasives.finally v close_out (fun v ->
49+
Ext_pervasives.finally v ~clean:close_out (fun v ->
5050
let f = Ext_pp.from_channel v in
5151
let aux file =
5252
let str = Ext_io.load_file file in
@@ -83,7 +83,7 @@ let from_cmi (files : string list) (output_file : string) =
8383
Ext_pp.string f "\""
8484
in
8585
let v = open_out_bin output_file in
86-
Ext_pervasives.finally v close_out (fun v ->
86+
Ext_pervasives.finally v ~clean:close_out (fun v ->
8787
let f = Ext_pp.from_channel v in
8888
let aux file =
8989
let cmi = Cmi_format.read_cmi file in

jscomp/syntax/ast_reason_pp.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ exception Pp_error
3232
let pp (sourcefile : string) =
3333

3434

35-
let tmpfile = Filename.temp_file "ocamlpp" "" in
35+
let tmpfile = Filename.temp_file "bspp" "" in
3636
let pp = (*TODO: check to avoid double quoting *)
3737
Ext_filename.maybe_quote
3838
(match !Js_config.refmt with

0 commit comments

Comments
 (0)