Merge branch 'fix-tuples-reasonligo' into 'dev'

Add support for tuple destructuring in ReasonLIGO

See merge request ligolang/ligo!342
This commit is contained in:
John David Pressman 2020-01-21 22:04:27 +00:00
commit c8ea510f18
5 changed files with 40 additions and 10 deletions

View File

@ -428,8 +428,22 @@ fun_expr:
{p.value with inside = arg_to_pattern p.value.inside} {p.value with inside = arg_to_pattern p.value.inside}
in PPar {p with value} in PPar {p with value}
| EUnit u -> PUnit u | EUnit u -> PUnit u
| e -> let open! SyntaxError | ETuple { value; region } ->
in raise (Error (WrongFunctionArguments e)) PTuple { value = Utils.nsepseq_map arg_to_pattern value; region}
| EAnnot {region; value = {inside = t, colon, typ; _}} ->
let value = { pattern = arg_to_pattern t; colon; type_expr = typ} in
PPar {
value = {
lpar = Region.ghost;
rpar = Region.ghost;
inside = PTyped {region; value}
};
region
}
| e -> (
let open! SyntaxError in
raise (Error (WrongFunctionArguments e))
)
in in
let fun_args_to_pattern = function let fun_args_to_pattern = function
EAnnot { EAnnot {

View File

@ -36,7 +36,7 @@ module Errors = struct
] in ] in
error ~data title message error ~data title message
let unsuppported_let_in_function (patterns : Raw.pattern list) = let unsupported_let_in_function (patterns : Raw.pattern list) =
let title () = "unsupported 'let ... in' function" in let title () = "unsupported 'let ... in' function" in
let message () = "defining functions via 'let ... in' is not supported yet" in let message () = "defining functions via 'let ... in' is not supported yet" in
let patterns_loc = let patterns_loc =
@ -179,6 +179,10 @@ let rec tuple_pattern_to_typed_vars : Raw.pattern -> _ = fun pattern ->
| Raw.PVar _ -> bind_list [pattern_to_typed_var pattern] | Raw.PVar _ -> bind_list [pattern_to_typed_var pattern]
| other -> (fail @@ wrong_pattern "parenthetical, tuple, or variable" other) | 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 -> let rec simpl_type_expression : Raw.type_expr -> type_expression result = fun te ->
trace (simple_info "simplifying this type expression...") @@ trace (simple_info "simplifying this type expression...") @@
match te with match te with
@ -354,7 +358,7 @@ let rec simpl_expression :
(* let f p1 ps... = rhs in body *) (* let f p1 ps... = rhs in body *)
| (f, p1 :: ps) -> | (f, p1 :: ps) ->
fail @@ unsuppported_let_in_function (f :: p1 :: ps) fail @@ unsupported_let_in_function (f :: p1 :: ps)
end end
| Raw.EAnnot a -> | Raw.EAnnot a ->
let Raw.{inside=expr, _, type_expr; _}, loc = r_split a in let Raw.{inside=expr, _, type_expr; _}, loc = r_split a in
@ -541,7 +545,8 @@ and simpl_fun lamb' : expr result =
(match pt with (match pt with
| Raw.PTyped pt -> | Raw.PTyped pt ->
begin begin
match pt.value.pattern with let pt_pattern = unpar_pattern pt.value.pattern in
match pt_pattern with
| Raw.PVar _ -> params | Raw.PVar _ -> params
| Raw.PTuple _ -> | Raw.PTuple _ ->
[Raw.PTyped [Raw.PTyped
@ -581,10 +586,10 @@ and simpl_fun lamb' : expr result =
match destruct with (* Handle tuple parameter destructuring *) match destruct with (* Handle tuple parameter destructuring *)
(* In this section we create a let ... in that binds the original parameters *) (* In this section we create a let ... in that binds the original parameters *)
| Raw.PPar pp -> | Raw.PPar pp ->
(match pp.value.inside with (match unpar_pattern pp.value.inside with
| Raw.PTyped pt -> | Raw.PTyped pt ->
let vars = pt.value in let vars = pt.value in
(match vars.pattern with (match unpar_pattern vars.pattern with
| PTuple vars -> | PTuple vars ->
let let_in_binding: Raw.let_binding = let let_in_binding: Raw.let_binding =
{binders = (PTuple vars, []) ; {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

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

View File

@ -1943,8 +1943,15 @@ let type_tuple_destruct () : unit result =
let tuple_param_destruct () : unit result = let tuple_param_destruct () : unit result =
let%bind program = mtype_file "./contracts/tuple_param_destruct.mligo" in 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) let%bind () = expect_eq program "sum" (e_tuple [e_int 20; e_int 10]) (e_int 10) in
in ok () 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 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 let_in_multi_bind () : unit result =
let%bind program = mtype_file "./contracts/let_in_multi_bind.mligo" in let%bind program = mtype_file "./contracts/let_in_multi_bind.mligo" in
@ -2159,6 +2166,7 @@ let main = test_suite "Integration (End to End)" [
test "attributes (religo)" attributes_religo; test "attributes (religo)" attributes_religo;
test "let in multi-bind (mligo)" let_in_multi_bind ; test "let in multi-bind (mligo)" let_in_multi_bind ;
test "tuple param destruct (mligo)" tuple_param_destruct ; test "tuple param destruct (mligo)" tuple_param_destruct ;
test "tuple param destruct (religo)" tuple_param_destruct_religo ;
test "empty case" empty_case ; test "empty case" empty_case ;
test "empty case (mligo)" empty_case_mligo ; test "empty case (mligo)" empty_case_mligo ;
test "empty case (religo)" empty_case_religo ; test "empty case (religo)" empty_case_religo ;