| 1 |
open Core |
| 2 |
open Ppxlib |
| 3 |
|
| 4 |
let loc = Location.none |
| 5 |
let print_expr expr = Pprintast.string_of_expression expr |> print_string |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
let assert_zero_alloc f = |
| 11 |
let allocations_before = Gc.major_plus_minor_words () in |
| 12 |
let r = f () in |
| 13 |
let allocations_after = Gc.major_plus_minor_words () in |
| 14 |
let allocations = allocations_after - allocations_before in |
| 15 |
[%test_result: int] allocations ~expect:0; |
| 16 |
r |
| 17 |
;; |
| 18 |
|
| 19 |
let check_some n = |
| 20 |
match n with |
| 21 |
| Some () -> () |
| 22 |
| None -> failwith "must be some" |
| 23 |
;; |
| 24 |
|
| 25 |
let%expect_test "while%bindl trivial test" = |
| 26 |
let i = ref 0 in |
| 27 |
let (r : unit option) = |
| 28 |
assert_zero_alloc (fun () -> |
| 29 |
while%bindl.Local_option |
| 30 |
incr i; |
| 31 |
Some (!i <= 5) |
| 32 |
do |
| 33 |
Some () |
| 34 |
done) |
| 35 |
in |
| 36 |
check_some r [@nontail] |
| 37 |
;; |
| 38 |
|
| 39 |
let%expect_test "let%bindl expansion" = |
| 40 |
Ppx_let_expander.expand |
| 41 |
Ppx_let_expander.bind |
| 42 |
Ppx_let_expander.Extension_kind.default |
| 43 |
~modul:None |
| 44 |
~locality:`local |
| 45 |
[%expr |
| 46 |
let PATTERN1 = return EXPRESSION1 |
| 47 |
and PATTERN2 = return EXPRESSION2 in |
| 48 |
return EXPRESSION3] |
| 49 |
|> print_expr; |
| 50 |
[%expect |
| 51 |
{| |
| 52 |
let __let_syntax__001_ = return EXPRESSION1[@@ppxlib.do_not_enter_value ] |
| 53 |
and __let_syntax__002_ = return EXPRESSION2[@@ppxlib.do_not_enter_value ] in |
| 54 |
Let_syntax.bind (Let_syntax.both __let_syntax__001_ __let_syntax__002_) |
| 55 |
~f:(fun (PATTERN1, PATTERN2) -> |
| 56 |
local_ let __nontail__004_ = return EXPRESSION3 in __nontail__004_) |
| 57 |
|}] |
| 58 |
;; |
| 59 |
|
| 60 |
let%expect_test "let%mapl expansion" = |
| 61 |
Ppx_let_expander.expand |
| 62 |
Ppx_let_expander.map |
| 63 |
Ppx_let_expander.Extension_kind.default |
| 64 |
~modul:None |
| 65 |
~locality:`local |
| 66 |
[%expr |
| 67 |
let PATTERN1 = return EXPRESSION1 |
| 68 |
and PATTERN2 = return EXPRESSION2 in |
| 69 |
return EXPRESSION3] |
| 70 |
|> print_expr; |
| 71 |
[%expect |
| 72 |
{| |
| 73 |
let __let_syntax__005_ = return EXPRESSION1[@@ppxlib.do_not_enter_value ] |
| 74 |
and __let_syntax__006_ = return EXPRESSION2[@@ppxlib.do_not_enter_value ] in |
| 75 |
Let_syntax.map (Let_syntax.both __let_syntax__005_ __let_syntax__006_) |
| 76 |
~f:(fun (PATTERN1, PATTERN2) -> |
| 77 |
local_ let __nontail__008_ = return EXPRESSION3 in __nontail__008_) |
| 78 |
|}] |
| 79 |
;; |
| 80 |
|
| 81 |
let something_to_tail_call () = Some () |
| 82 |
|
| 83 |
let%expect_test "make sure let%bindl and let%mapl work well together" = |
| 84 |
let r : unit option option option = |
| 85 |
assert_zero_alloc (fun () -> |
| 86 |
match%bindl.Local_option Some () with |
| 87 |
| () -> |
| 88 |
let open Local_option.Let_syntax in |
| 89 |
let%bindl () = Some () |
| 90 |
and () = Some () in |
| 91 |
let%mapl () = Some () |
| 92 |
and () = Some () in |
| 93 |
let%bindl () = Some () in |
| 94 |
let%mapl () = Some () in |
| 95 |
something_to_tail_call () [@nontail]) |
| 96 |
in |
| 97 |
match r with |
| 98 |
| Some (Some x) -> check_some x [@nontail] |
| 99 |
| _ -> failwith "how?" |
| 100 |
;; |
| 101 |
|
| 102 |
let%expect_test "bind4" = |
| 103 |
let r : unit option = |
| 104 |
assert_zero_alloc (fun () -> |
| 105 |
let%bindln.Local_option () = Some () |
| 106 |
and () = Some () |
| 107 |
and () = Some () |
| 108 |
and () = Some () in |
| 109 |
Some ()) |
| 110 |
in |
| 111 |
check_some r [@nontail] |
| 112 |
;; |
| 113 |
|
| 114 |
let%expect_test "map4" = |
| 115 |
let r : unit option = |
| 116 |
assert_zero_alloc (fun () -> |
| 117 |
let%mapln.Local_option () = Some () |
| 118 |
and () = Some () |
| 119 |
and () = Some () |
| 120 |
and () = Some () in |
| 121 |
()) |
| 122 |
in |
| 123 |
check_some r [@nontail] |
| 124 |
;; |
| 125 |
|
| 126 |
let%expect_test "match%bindl" = |
| 127 |
let r : unit option = |
| 128 |
assert_zero_alloc (fun () -> |
| 129 |
match%bindl.Local_option Some `hello with |
| 130 |
| `hello -> Some ()) |
| 131 |
in |
| 132 |
check_some r [@nontail] |
| 133 |
;; |
| 134 |
|
| 135 |
let%expect_test "if%bindl" = |
| 136 |
let r : unit option = |
| 137 |
assert_zero_alloc (fun () -> |
| 138 |
if%bindl.Local_option Some true then Some () else failwith "impossible") |
| 139 |
in |
| 140 |
check_some r [@nontail] |
| 141 |
;; |
| 142 |
|
| 143 |
let%expect_test "if%mapl" = |
| 144 |
let r : unit option = |
| 145 |
assert_zero_alloc (fun () -> |
| 146 |
if%mapl.Local_option Some true then () else failwith "impossible") |
| 147 |
in |
| 148 |
check_some r [@nontail] |
| 149 |
;; |