Skip to content

Commit 040d41b

Browse files
committed
Merge pull request #82 from bloomberg/try_curry
Try curry
2 parents b108b85 + 5f00a09 commit 040d41b

File tree

141 files changed

+3952
-2441
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+3952
-2441
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ ocaml/man
4848
jscomp/bench/*.js
4949
*.bak
5050
.vscode
51+
*.jsx
5152
osc
5253
jscomp/pre_load.js

jscomp/compiler.mllib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ js_fun_env
7373
js_pass_flatten_and_mark_dead
7474
js_pass_scope
7575
js_call_info
76-
76+
js_pass_debug
7777
js_of_lam_float_record
7878
js_of_lam_record
7979
js_of_lam_tuple

jscomp/config_util.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ let find_cmj file =
4444
-> Lazy.force v
4545
| exception Not_found
4646
->
47-
Ext_log.warn __LOC__ "@[%s not found @]@." file ;
47+
Ext_log.warn __LOC__ "@[%s not found @]" file ;
4848
Js_cmj_format.dummy (); (* FIXME *)
4949
end
5050
end

jscomp/ext_log.ml

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,42 @@
2121

2222

2323

24-
type ('a,'b) logging =
25-
('a -> 'b, Format.formatter, unit, unit, unit, unit) format6 -> 'a -> 'b
2624

27-
let err str f v =
28-
Format.fprintf Format.err_formatter ("%s " ^^ f) str v
25+
type 'a logging = ('a, Format.formatter, unit, unit, unit, unit) format6 -> 'a
2926

30-
let ierr b str f v =
27+
let err str f =
28+
Format.fprintf Format.err_formatter ("%s " ^^ f) str
29+
30+
let ierr b str f =
3131
if b then
32-
Format.fprintf Format.err_formatter ("%s " ^^ f) str v
32+
Format.fprintf Format.err_formatter ("%s " ^^ f) str
3333
else
34-
Format.ifprintf Format.err_formatter ("%s " ^^ f) str v
34+
Format.ifprintf Format.err_formatter ("%s " ^^ f) str
3535

36-
let warn str f v =
37-
Format.fprintf Format.err_formatter ("WARN: %s " ^^ f) str v
36+
let warn str f =
37+
Format.fprintf Format.err_formatter ("WARN: %s " ^^ f ^^ "@.") str
3838

3939

4040

41-
let iwarn b str f v =
41+
let iwarn b str f =
4242
if b then
43-
Format.fprintf Format.err_formatter ("WARN: %s " ^^ f) str v
43+
Format.fprintf Format.err_formatter ("WARN: %s " ^^ f) str
4444
else
45-
Format.ifprintf Format.err_formatter ("WARN: %s " ^^ f) str v
45+
Format.ifprintf Format.err_formatter ("WARN: %s " ^^ f) str
4646

47-
let dwarn str f v =
47+
(* TODO: add {[@.]} later for all *)
48+
let dwarn str f =
4849
if Lam_current_unit.is_same_file () then
49-
Format.fprintf Format.err_formatter ("WARN: %s " ^^ f) str v
50+
Format.fprintf Format.err_formatter ("WARN: %s " ^^ f ^^ "@.") str
5051
else
51-
Format.ifprintf Format.err_formatter ("WARN: %s " ^^ f) str v
52+
Format.ifprintf Format.err_formatter ("WARN: %s " ^^ f ^^ "@.") str
5253

53-
let info str f v =
54-
Format.fprintf Format.err_formatter ("INFO: %s " ^^ f) str v
54+
let info str f =
55+
Format.fprintf Format.err_formatter ("INFO: %s " ^^ f) str
5556

56-
let iinfo b str f v =
57+
let iinfo b str f =
5758
if b then
58-
Format.fprintf Format.err_formatter ("INFO: %s " ^^ f) str v
59+
Format.fprintf Format.err_formatter ("INFO: %s " ^^ f) str
5960
else
60-
Format.fprintf Format.err_formatter ("INFO: %s " ^^ f) str v
61+
Format.fprintf Format.err_formatter ("INFO: %s " ^^ f) str
6162

jscomp/ext_log.mli

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,14 @@
2929
*)
3030

3131

32-
type ('a,'b) logging = ('a -> 'b, Format.formatter, unit, unit, unit, unit) format6 -> 'a -> 'b
33-
34-
(* FIXM: below does not work
35-
{[
36-
err __LOC__ "hi"
37-
]}
38-
39-
*)
40-
val err : string -> ('a,'b) logging
41-
val ierr : bool -> string -> ('a,'b) logging
42-
val warn : string -> ('a,'b) logging
43-
val iwarn : bool -> string -> ('a,'b) logging
44-
val dwarn : string -> ('a,'b) logging
45-
val info : string -> ('a,'b) logging
46-
val iinfo : bool -> string -> ('a,'b) logging
32+
33+
type 'a logging = ('a, Format.formatter, unit, unit, unit, unit) format6 -> 'a
34+
35+
36+
val err : string -> 'a logging
37+
val ierr : bool -> string -> 'a logging
38+
val warn : string -> 'a logging
39+
val iwarn : bool -> string -> 'a logging
40+
val dwarn : string -> 'a logging
41+
val info : string -> 'a logging
42+
val iinfo : bool -> string -> 'a logging

jscomp/js_config.ml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,32 @@ let runtime_set = String_set.of_list [
9797
"caml_utils.js";
9898
"caml_exceptions.js";
9999
(* "caml_io.js"; *)
100-
"curry.js";
100+
"caml_curry.js";
101101
"caml_file.js";
102102
"caml_lexer.js";
103103
"caml_string.js"
104104
]
105+
106+
107+
let prim = "Caml_primitive"
108+
109+
let exceptions = "Caml_exceptions"
110+
111+
let io = "Caml_io"
112+
113+
let sys = "Caml_sys"
114+
115+
let lex_parse = "Caml_lexer"
116+
117+
let obj_runtime = "Caml_obj_runtime"
118+
119+
let array = "Caml_array"
120+
121+
let format = "Caml_format"
122+
123+
let string = "Caml_string"
124+
125+
let float = "Caml_float"
126+
127+
let oo = "Caml_oo"
128+
let curry = "Caml_curry"

jscomp/js_config.mli

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,29 @@ val set_env : env -> unit
3030
val runtime_set : String_set.t
3131
val stdlib_set : String_set.t
3232

33+
val prim : string
34+
35+
val exceptions : string
36+
37+
val io : string
38+
39+
val oo : string
40+
41+
val sys : string
42+
43+
val lex_parse : string
44+
45+
val obj_runtime : string
46+
47+
val array : string
48+
49+
val format : string
50+
51+
val string : string
52+
53+
val float : string
54+
55+
val curry : string
56+
57+
3358

jscomp/js_dump.ml

Lines changed: 88 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ module L = struct
8787
let define = "define"
8888
let break = "break"
8989
let strict_directive = "'use strict';"
90+
91+
let curry = "curry" (* curry arbitrary args *)
9092
end
9193
let return_indent = (String.length L.return / Ext_pp.indent_length)
9294

@@ -393,12 +395,27 @@ and
393395
| Call (e, el, info) ->
394396
let action () =
395397
P.group f 1 (fun _ ->
396-
let () =
397-
match info with
398-
| {arity = NA } -> ipp_comment f (Some "!")
399-
| _ -> () in
400-
let cxt = expression 15 cxt f e in
401-
P.paren_group f 1 (fun _ -> arguments cxt f el ) )
398+
match info, el with
399+
| {arity = Full }, _
400+
| _, [] ->
401+
let cxt = expression 15 cxt f e in
402+
P.paren_group f 1 (fun _ -> arguments cxt f el )
403+
404+
| _ , _ ->
405+
(* ipp_comment f (Some "!") *)
406+
P.string f Js_config.curry;
407+
P.string f L.dot;
408+
let len = List.length el in
409+
if 1 <= len && len <= 8 then
410+
begin
411+
P.string f (Printf.sprintf "app%d" len);
412+
P.paren_group f 1 (fun _ -> arguments cxt f (e::el))
413+
end
414+
else
415+
begin
416+
P.string f L.curry;
417+
P.paren_group f 1 (fun _ -> arguments cxt f [ e ; E.arr Mutable el])
418+
end)
402419
in
403420
if l > 15 then P.paren_group f 1 action
404421
else action ()
@@ -1300,46 +1317,46 @@ let exports cxt f (idents : Ident.t list) =
13001317
outer_cxt
13011318

13021319

1303-
let node_program f ( {program ; modules ; } : J.deps_program) =
1304-
let cxt = Ext_pp_scope.empty in
1305-
(* Node style *)
1306-
let requires cxt f (modules : (Ident.t * string) list ) =
1307-
P.newline f ;
1308-
(* the context used to print the following program *)
1309-
let outer_cxt, reversed_list, margin =
1310-
List.fold_left
1311-
(fun (cxt, acc, len) (id,s) ->
1312-
let str, cxt = str_of_ident cxt id in
1313-
cxt, ((str,s) :: acc), (max len (String.length str))
1314-
)
1315-
(cxt, [], 0) modules in
1316-
P.force_newline f ;
1317-
Ext_list.rev_iter (fun (s,file) ->
1318-
P.string f L.var;
1319-
P.space f ;
1320-
P.string f s ;
1321-
P.nspace f (margin - String.length s + 1) ;
1322-
P.string f L.eq;
1323-
P.space f;
1324-
P.string f L.require;
1325-
P.paren_group f 0 @@ (fun _ ->
1326-
pp_string f ~utf:true ~quote:(best_string_quote s) file );
1327-
semi f ;
1328-
P.newline f ;
1329-
) reversed_list;
1330-
outer_cxt
1331-
in
1332-
1333-
let cxt = requires cxt f modules in
1320+
(* Node style *)
1321+
let requires cxt f (modules : (Ident.t * string) list ) =
1322+
P.newline f ;
1323+
(* the context used to print the following program *)
1324+
let outer_cxt, reversed_list, margin =
1325+
List.fold_left
1326+
(fun (cxt, acc, len) (id,s) ->
1327+
let str, cxt = str_of_ident cxt id in
1328+
cxt, ((str,s) :: acc), (max len (String.length str))
1329+
)
1330+
(cxt, [], 0) modules in
1331+
P.force_newline f ;
1332+
Ext_list.rev_iter (fun (s,file) ->
1333+
P.string f L.var;
1334+
P.space f ;
1335+
P.string f s ;
1336+
P.nspace f (margin - String.length s + 1) ;
1337+
P.string f L.eq;
1338+
P.space f;
1339+
P.string f L.require;
1340+
P.paren_group f 0 @@ (fun _ ->
1341+
pp_string f ~utf:true ~quote:(best_string_quote s) file );
1342+
semi f ;
1343+
P.newline f ;
1344+
) reversed_list;
1345+
outer_cxt
13341346

1347+
let program f cxt ( x : J.program ) =
13351348
let () = P.force_newline f in
1336-
let cxt = statement_list true cxt f program.block in
1349+
let cxt = statement_list true cxt f x.block in
13371350
let () = P.force_newline f in
1338-
exports cxt f program.exports
1351+
exports cxt f x.exports
13391352

1353+
let node_program f ( x : J.deps_program) =
1354+
let cxt = requires ( Ext_pp_scope.empty) f x.modules in
1355+
program f cxt x.program
1356+
13401357

13411358
let amd_program f
1342-
( {program ; modules ; _} : J.deps_program)
1359+
( x : J.deps_program)
13431360
=
13441361
P.newline f ;
13451362
let cxt = Ext_pp_scope.empty in
@@ -1352,7 +1369,7 @@ let amd_program f
13521369
P.string f L.comma ;
13531370
P.space f;
13541371
pp_string f ~utf:true ~quote:(best_string_quote s) s;
1355-
) modules ;
1372+
) x.modules ;
13561373
P.string f "]";
13571374
P.string f L.comma;
13581375
P.newline f;
@@ -1365,33 +1382,30 @@ let amd_program f
13651382
P.string f L.comma;
13661383
P.space f ;
13671384
ident cxt f id
1368-
) cxt modules
1385+
) cxt x.modules
13691386
in
13701387
P.string f ")";
1371-
P.brace_vgroup f 1 @@ (fun _ ->
1388+
let v = P.brace_vgroup f 1 @@ (fun _ ->
13721389
let () = P.string f L.strict_directive in
1373-
let () = P.newline f in
1374-
let cxt = statement_list true cxt f program.block in
1375-
(* FIXME AMD : use {[ function xx ]} or {[ var x = function ..]} *)
1376-
P.newline f;
1377-
P.force_newline f;
1378-
ignore (exports cxt f program.exports));
1390+
program f cxt x.program
1391+
) in
13791392
P.string f ")";
1393+
v
13801394
;;
13811395

1382-
let pp_program ( program : J.deps_program) (f : Ext_pp.t) =
1396+
let pp_deps_program ( program : J.deps_program) (f : Ext_pp.t) =
13831397
begin
13841398
P.string f "// Generated CODE, PLEASE EDIT WITH CARE";
13851399
P.newline f;
13861400
P.string f L.strict_directive;
13871401
P.newline f ;
1388-
(match Js_config.get_env () with
1402+
ignore (match Js_config.get_env () with
13891403
| Browser ->
1390-
ignore (node_program f program)
1404+
(node_program f program)
13911405
| NodeJS ->
13921406
begin match Sys.getenv "OCAML_AMD_MODULE" with
13931407
| exception Not_found ->
1394-
ignore (node_program f program)
1408+
(node_program f program)
13951409
(* amd_program f program *)
13961410
| _ -> amd_program f program
13971411
end ) ;
@@ -1403,7 +1417,25 @@ let pp_program ( program : J.deps_program) (f : Ext_pp.t) =
14031417
P.newline f;
14041418
P.flush f ()
14051419
end
1406-
let dump_program
1407-
(program : J.deps_program)
1420+
1421+
let dump_program (x : J.program) oc =
1422+
ignore (program (P.from_channel oc) Ext_pp_scope.empty x )
1423+
1424+
let dump_deps_program
1425+
x
14081426
(oc : out_channel) =
1409-
pp_program program (P.from_channel oc)
1427+
pp_deps_program x (P.from_channel oc)
1428+
1429+
let string_of_block block
1430+
=
1431+
let buffer = Buffer.create 50 in
1432+
begin
1433+
let f = P.from_buffer buffer in
1434+
let _scope = statement_list true Ext_pp_scope.empty f block in
1435+
P.flush f ();
1436+
Buffer.contents buffer
1437+
end
1438+
1439+
1440+
1441+

0 commit comments

Comments
 (0)