Handle parentheses when destructuring.

This commit is contained in:
Sander Spies 2020-01-21 21:23:31 +01:00
parent 483f591f62
commit f104b5e512
5 changed files with 19 additions and 10 deletions

View File

@ -430,7 +430,7 @@ fun_expr:
| EUnit u -> PUnit u
| ETuple { value; region } ->
PTuple { value = Utils.nsepseq_map arg_to_pattern value; region}
| EAnnot {region; value = {inside = ETuple _ as t, colon, typ; _}} ->
| EAnnot {region; value = {inside = t, colon, typ; _}} ->
let value = { pattern = arg_to_pattern t; colon; type_expr = typ} in
PPar {
value = {

View File

@ -179,6 +179,10 @@ let rec tuple_pattern_to_typed_vars : Raw.pattern -> _ = fun pattern ->
| Raw.PVar _ -> bind_list [pattern_to_typed_var pattern]
| other -> (fail @@ wrong_pattern "parenthetical, tuple, or variable" other)
let rec unpar_pattern : Raw.pattern -> Raw.pattern = function
| PPar p -> unpar_pattern p.value.inside
| _ as p -> p
let rec simpl_type_expression : Raw.type_expr -> type_expression result = fun te ->
trace (simple_info "simplifying this type expression...") @@
match te with
@ -541,7 +545,8 @@ and simpl_fun lamb' : expr result =
(match pt with
| Raw.PTyped pt ->
begin
match pt.value.pattern with
let pt_pattern = unpar_pattern pt.value.pattern in
match pt_pattern with
| Raw.PVar _ -> params
| Raw.PTuple _ ->
[Raw.PTyped
@ -581,10 +586,10 @@ and simpl_fun lamb' : expr result =
match destruct with (* Handle tuple parameter destructuring *)
(* In this section we create a let ... in that binds the original parameters *)
| Raw.PPar pp ->
(match pp.value.inside with
(match unpar_pattern pp.value.inside with
| Raw.PTyped pt ->
let vars = pt.value in
(match vars.pattern with
(match unpar_pattern vars.pattern with
| PTuple vars ->
let let_in_binding: Raw.let_binding =
{binders = (PTuple vars, []) ;

View File

@ -1 +1,2 @@
let sum (result, i : int * int) : int = result + i
let sum (result, i : int * int) : int = result - i
let parentheses ((((result, i))) : ((int * int))) : int = result - i

View File

@ -1 +1,2 @@
let sum = ((result, i) : (int, int)) : int => result + i;
let sum = ((result, i) : (int, int)) : int => result - i;
let parentheses = (((((result, i)))) : (((int, int)))) : int => result - i;

View File

@ -1943,13 +1943,15 @@ let type_tuple_destruct () : unit result =
let tuple_param_destruct () : unit result =
let%bind program = mtype_file "./contracts/tuple_param_destruct.mligo" in
let%bind () = expect_eq program "sum" (e_tuple [e_int 10; e_int 10]) (e_int 20)
in ok ()
let%bind () = expect_eq program "sum" (e_tuple [e_int 20; e_int 10]) (e_int 10) in
let%bind () = expect_eq program "parentheses" (e_tuple [e_int 20; e_int 10]) (e_int 10) in
ok ()
let tuple_param_destruct_religo () : unit result =
let%bind program = retype_file "./contracts/tuple_param_destruct.religo" in
let%bind () = expect_eq program "sum" (e_tuple [e_int 10; e_int 10]) (e_int 20)
in ok ()
let%bind () = expect_eq program "sum" (e_tuple [e_int 20; e_int 10]) (e_int 10) in
let%bind () = expect_eq program "parentheses" (e_tuple [e_int 20; e_int 10]) (e_int 10) in
ok ()
let let_in_multi_bind () : unit result =
let%bind program = mtype_file "./contracts/let_in_multi_bind.mligo" in