@@ -193,7 +193,7 @@ let has_null_undefined_other (sw_names : Ast_untagged_variants.switch_names opti
193
193
let no_effects_const = lazy true
194
194
(* let has_effects_const = lazy false *)
195
195
196
- (* * We drop the ability of cross-compiling
196
+ (* We drop the ability of cross-compiling
197
197
the compiler has to be the same running
198
198
*)
199
199
@@ -224,6 +224,9 @@ type initialization = J.block
224
224
-: we should not do functor application inlining in a
225
225
non-toplevel, it will explode code very quickly
226
226
*)
227
+
228
+ let compile output_prefix =
229
+
227
230
let rec compile_external_field (* Like [List.empty]*)
228
231
(lamba_cxt : Lam_compile_context.t ) (id : Ident.t ) name : Js_output.t =
229
232
match Lam_compile_env. query_external_id_info id name with
@@ -249,7 +252,7 @@ let rec compile_external_field (* Like [List.empty]*)
249
252
@param args arguments
250
253
*)
251
254
252
- (* * This can not happen since this id should be already consulted by type checker
255
+ (* This can not happen since this id should be already consulted by type checker
253
256
Worst case
254
257
{[
255
258
E.array_index_by_int m pos
@@ -304,7 +307,7 @@ and compile_external_field_apply (appinfo : Lam.apply) (module_id : Ident.t)
304
307
Js_output. output_of_block_and_expression lambda_cxt.continuation args_code
305
308
expression
306
309
307
- (* *
310
+ (*
308
311
The second return values are values which need to be wrapped using
309
312
[update_dummy]
310
313
@@ -500,29 +503,29 @@ and compile_recursive_lets cxt id_args : Js_output.t =
500
503
501
504
and compile_general_cases :
502
505
'a .
503
- ('a -> Ast_untagged_variants. literal option ) ->
504
- ('a -> J. expression ) ->
505
- ('a option -> J. expression -> 'a option -> J. expression -> J. expression ) ->
506
- Lam_compile_context. t ->
507
- (?default :J.block ->
508
- ?declaration:Lam_compat.let_kind * Ident.t ->
509
- _ ->
510
- ('a * J.case_clause) list ->
511
- J.statement ) ->
512
- _ ->
513
- ('a * Lam. t ) list ->
514
- default_case ->
506
+ get_cstr_name : ('a -> Ast_untagged_variants. literal option ) ->
507
+ make_exp : ('a -> J. expression ) ->
508
+ eq_exp : ('a option -> J. expression -> 'a option -> J. expression -> J. expression ) ->
509
+ cxt : Lam_compile_context. t ->
510
+ switch : (?default :J.block -> ?declaration:Lam_compat.let_kind * Ident.t ->
511
+ _ -> ('a * J.case_clause) list -> J.statement ) ->
512
+ switch_exp : J. expression ->
513
+ cases : ('a * Lam. t ) list ->
514
+ default : default_case ->
515
515
J. block =
516
- fun (get_cstr_name : _ -> Ast_untagged_variants.literal option ) (make_exp : _ -> J.expression )
517
- (eq_exp : 'a option -> J.expression -> 'a option -> J.expression -> J.expression )
518
- (cxt : Lam_compile_context.t )
519
- (switch :
520
- ?default:J.block ->
521
- ?declaration:Lam_compat.let_kind * Ident.t ->
522
- _ ->
523
- (_ * J.case_clause) list ->
524
- J.statement ) (switch_exp : J.expression ) (cases : (_ * Lam.t) list )
525
- (default : default_case ) ->
516
+ fun (type a )
517
+ ~(get_cstr_name : a -> Ast_untagged_variants.literal option )
518
+ ~(make_exp : a -> J.expression )
519
+ ~(eq_exp : a option -> J.expression -> a option -> J.expression -> J.expression )
520
+ ~(cxt : Lam_compile_context.t )
521
+ ~(switch :
522
+ ?default:J.block ->
523
+ ?declaration:Lam_compat.let_kind * Ident.t ->
524
+ _ -> (a * J.case_clause) list -> J.statement
525
+ )
526
+ ~(switch_exp : J.expression )
527
+ ~(cases : (a * Lam.t) list )
528
+ ~(default : default_case ) ->
526
529
match (cases, default) with
527
530
| [] , Default lam -> Js_output. output_as_block (compile_lambda cxt lam)
528
531
| [] , (Complete | NonComplete ) -> []
@@ -538,6 +541,7 @@ and compile_general_cases :
538
541
morph_declare_to_assign cxt (fun cxt define ->
539
542
[
540
543
S. if_ ?declaration:define
544
+
541
545
(eq_exp None switch_exp (Some id) (make_exp id))
542
546
(Js_output. output_as_block (compile_lambda cxt lam));
543
547
])
@@ -624,22 +628,26 @@ and use_compile_literal_cases table get_name =
624
628
| Some {name; literal_type = None } , Some string_table -> Some ((String name, lam) :: string_table)
625
629
| _ , _ -> None
626
630
) table (Some [] )
627
- and compile_cases ?(untagged =false ) cxt (switch_exp : E.t ) table default get_name =
631
+ and compile_cases ?(untagged =false ) cxt (switch_exp : E.t ) table default get_name : initialization =
628
632
match use_compile_literal_cases table get_name with
629
633
| Some string_table ->
630
634
if untagged
631
635
then compile_untagged_cases cxt switch_exp string_table default
632
636
else compile_string_cases cxt switch_exp string_table default
633
637
| None ->
634
- compile_general_cases get_name
635
- (fun i -> match get_name i with
638
+ compile_general_cases
639
+ ~get_cstr_name: get_name
640
+ ~make_exp: (fun i -> match get_name i with
636
641
| None -> E. small_int i
637
642
| Some {literal_type = Some (String s )} -> E. str s
638
643
| Some {name} -> E. str name)
639
- (fun _ x _ y -> E. int_equal x y) cxt
640
- (fun ?default ?declaration e clauses ->
644
+ ~eq_exp: (fun _ x _ y -> E. int_equal x y)
645
+ ~cxt
646
+ ~switch: (fun ?default ?declaration e clauses ->
641
647
S. int_switch ?default ?declaration e clauses)
642
- switch_exp table default
648
+ ~switch_exp
649
+ ~cases: table
650
+ ~default
643
651
644
652
and compile_switch (switch_arg : Lam.t ) (sw : Lam.lambda_switch )
645
653
(lambda_cxt : Lam_compile_context.t ) =
@@ -691,6 +699,7 @@ and compile_switch (switch_arg : Lam.t) (sw : Lam.lambda_switch)
691
699
else
692
700
(* [e] will be used twice *)
693
701
let dispatch e =
702
+
694
703
let is_a_literal_case =
695
704
if block_cases <> []
696
705
then
@@ -728,22 +737,22 @@ and compile_switch (switch_arg : Lam.t) (sw : Lam.lambda_switch)
728
737
:: compile_whole { lambda_cxt with continuation = Assign id })
729
738
| EffectCall _ | Assign _ -> Js_output. make (compile_whole lambda_cxt)
730
739
731
- and compile_string_cases cxt switch_exp table default =
732
- let literal = function
740
+
741
+ and compile_string_cases cxt switch_exp cases default : initialization =
742
+ let literal = function
733
743
| literal -> E. literal literal
734
744
in
735
745
compile_general_cases
736
- (fun _ -> None )
737
- literal
738
- (fun _ x _ y -> E. string_equal x y)
739
- cxt
740
- (fun ?default ?declaration e clauses ->
746
+ ~get_cstr_name: (fun _ -> None )
747
+ ~make_exp: literal
748
+ ~eq_exp: (fun _ x _ y -> E. string_equal x y)
749
+ ~ cxt
750
+ ~switch: (fun ?default ?declaration e clauses ->
741
751
S. string_switch ?default ?declaration e clauses)
742
- switch_exp table default
743
- and compile_untagged_cases cxt switch_exp table default =
744
- let literal = function
745
- | literal -> E. literal literal
746
- in
752
+ ~switch_exp
753
+ ~cases
754
+ ~default
755
+ and compile_untagged_cases cxt switch_exp cases default =
747
756
let add_runtime_type_check (literal : Ast_untagged_variants.literal_type ) x y = match literal with
748
757
| Block IntType
749
758
| Block StringType
@@ -762,7 +771,7 @@ and compile_untagged_cases cxt switch_exp table default =
762
771
| _ -> E. string_equal x y
763
772
in
764
773
let is_array (l , _ ) = l = Ast_untagged_variants. Block Array in
765
- let body ?default ?declaration e clauses =
774
+ let switch ?default ?declaration e clauses =
766
775
let array_clauses = Ext_list. filter clauses is_array in
767
776
match array_clauses with
768
777
| [(l, {J. switch_body})] when List. length clauses > 1 ->
@@ -774,12 +783,14 @@ and compile_untagged_cases cxt switch_exp table default =
774
783
| _ ->
775
784
S. string_switch ?default ?declaration (E. typeof e) clauses in
776
785
compile_general_cases
777
- (fun _ -> None )
778
- literal
779
- mk_eq
780
- cxt
781
- body
782
- switch_exp table default
786
+ ~get_cstr_name: (fun _ -> None )
787
+ ~make_exp: E. literal
788
+ ~eq_exp: mk_eq
789
+ ~cxt
790
+ ~switch
791
+ ~switch_exp
792
+ ~cases
793
+ ~default
783
794
784
795
and compile_stringswitch l cases default (lambda_cxt : Lam_compile_context.t ) =
785
796
(* TODO might better optimization according to the number of cases
@@ -1077,7 +1088,7 @@ and compile_while (predicate : Lam.t) (body : Lam.t)
1077
1088
Js_output. output_of_block_and_expression lambda_cxt.continuation block
1078
1089
E. unit
1079
1090
1080
- (* * all non-tail
1091
+ (* all non-tail
1081
1092
TODO: check semantics should start, finish be executed each time in both
1082
1093
ocaml and js?, also check evaluation order..
1083
1094
in ocaml id is not in the scope of finish, so it should be safe here
@@ -1661,7 +1672,7 @@ and compile_prim (prim_info : Lam.prim_info)
1661
1672
let args_code : J.block = List. concat args_block in
1662
1673
let exp =
1663
1674
(* TODO: all can be done in [compile_primitive] *)
1664
- Lam_compile_primitive. translate loc lambda_cxt primitive args_expr
1675
+ Lam_compile_primitive. translate output_prefix loc lambda_cxt primitive args_expr
1665
1676
in
1666
1677
Js_output. output_of_block_and_expression lambda_cxt.continuation args_code
1667
1678
exp
@@ -1758,3 +1769,8 @@ and compile_lambda (lambda_cxt : Lam_compile_context.t) (cur_lam : Lam.t) :
1758
1769
| Ltrywith (lam , id , catch ) ->
1759
1770
(* generate documentation *)
1760
1771
compile_trywith lam id catch lambda_cxt
1772
+
1773
+ in compile_recursive_lets, compile_lambda
1774
+
1775
+ let compile_recursive_lets ~output_prefix = fst (compile output_prefix)
1776
+ let compile_lambda ~output_prefix = snd (compile output_prefix)
0 commit comments