From 23993a448889178ab4a9bcb02183aa9ac2389f42 Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Fri, 18 Oct 2019 17:36:33 +0200 Subject: [PATCH 1/5] clean big_map tests --- src/test/contracts/big_map.ligo | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/test/contracts/big_map.ligo b/src/test/contracts/big_map.ligo index 8fb705ab5..75d68b27d 100644 --- a/src/test/contracts/big_map.ligo +++ b/src/test/contracts/big_map.ligo @@ -1,30 +1,21 @@ type storage_ is big_map(int, int) * unit function main(const p : unit; const s : storage_) : list(operation) * storage_ is - var r : big_map(int, int) := s.0 ; var toto : option (int) := Some(0); block { - toto := r[23]; - r[2] := 444; - s.0 := r; + toto := s.0[23]; + s.0[2] := 444; } with ((nil: list(operation)), s) function set_ (var n : int ; var m : storage_) : storage_ is block { - var tmp : big_map(int,int) := m.0 ; - tmp[23] := n ; - m.0 := tmp ; + m.0[23] := n ; } with m function rm (var m : storage_) : storage_ is block { - var tmp : big_map(int,int) := m.0 ; - remove 42 from map tmp; - m.0 := tmp; + remove 42 from map m.0; } with m function gf (const m : storage_) : int is begin skip end with get_force(23, m.0) -function get (const m : storage_) : option(int) is - begin - skip - end with m.0[42] \ No newline at end of file +function get (const m : storage_) : option(int) is begin skip end with m.0[42] \ No newline at end of file From 5d040220c051e2cc64a3bc5cb8d1c6cb4f75ddc1 Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Fri, 18 Oct 2019 17:52:26 +0200 Subject: [PATCH 2/5] WIP: add test --- src/test/contracts/big_map.ligo | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/test/contracts/big_map.ligo b/src/test/contracts/big_map.ligo index 75d68b27d..8afc707b4 100644 --- a/src/test/contracts/big_map.ligo +++ b/src/test/contracts/big_map.ligo @@ -18,4 +18,18 @@ function rm (var m : storage_) : storage_ is block { function gf (const m : storage_) : int is begin skip end with get_force(23, m.0) -function get (const m : storage_) : option(int) is begin skip end with m.0[42] \ No newline at end of file +function get (const m : storage_) : option(int) is begin skip end with m.0[42] + +function mutimaps (const m : storage_; const n : storage_) : storage_ is block +{ + var foo : big_map(int,int) := m.0 ; + foo[42] := 0 ; + n.0[42] := get_force(42, foo) ; +} with n + +const empty_big_map : big_map(int,int) = big_map end + +const map1 : big_map(int,int) = big_map + 23 -> 0 ; + 42 -> 0 ; +end \ No newline at end of file From cefac0d8e74868194e88f6d490230d5d0ec42e0c Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Mon, 21 Oct 2019 13:04:28 +0200 Subject: [PATCH 3/5] add big_map injection --- src/passes/1-parser/pascaligo/AST.ml | 2 ++ src/passes/1-parser/pascaligo/AST.mli | 1 + src/passes/1-parser/pascaligo/Parser.mly | 1 + src/passes/1-parser/pascaligo/ParserLog.ml | 3 ++- src/passes/2-simplify/pascaligo.ml | 14 +++++++++++++- 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/passes/1-parser/pascaligo/AST.ml b/src/passes/1-parser/pascaligo/AST.ml index 97eab1c20..985ae277e 100644 --- a/src/passes/1-parser/pascaligo/AST.ml +++ b/src/passes/1-parser/pascaligo/AST.ml @@ -490,6 +490,7 @@ and closing = and map_expr = MapLookUp of map_lookup reg | MapInj of binding reg injection reg +| BigMapInj of binding reg injection reg and map_lookup = { path : path; @@ -647,6 +648,7 @@ and tuple_expr_to_region {region; _} = region and map_expr_to_region = function MapLookUp {region; _} | MapInj {region; _} -> region +| BigMapInj {region; _} -> region and set_expr_to_region = function SetInj {region; _} diff --git a/src/passes/1-parser/pascaligo/AST.mli b/src/passes/1-parser/pascaligo/AST.mli index a41fb005f..cf8fa3321 100644 --- a/src/passes/1-parser/pascaligo/AST.mli +++ b/src/passes/1-parser/pascaligo/AST.mli @@ -481,6 +481,7 @@ and closing = and map_expr = MapLookUp of map_lookup reg | MapInj of binding reg injection reg +| BigMapInj of binding reg injection reg and map_lookup = { path : path; diff --git a/src/passes/1-parser/pascaligo/Parser.mly b/src/passes/1-parser/pascaligo/Parser.mly index 38b86357b..d3dc32568 100644 --- a/src/passes/1-parser/pascaligo/Parser.mly +++ b/src/passes/1-parser/pascaligo/Parser.mly @@ -877,6 +877,7 @@ set_expr: map_expr: map_lookup { MapLookUp $1 } | injection(Map,binding) { MapInj $1 } +| injection(BigMap,binding) { BigMapInj $1 } map_lookup: path brackets(expr) { diff --git a/src/passes/1-parser/pascaligo/ParserLog.ml b/src/passes/1-parser/pascaligo/ParserLog.ml index e09149bce..2f0fe6268 100644 --- a/src/passes/1-parser/pascaligo/ParserLog.ml +++ b/src/passes/1-parser/pascaligo/ParserLog.ml @@ -434,6 +434,7 @@ and print_case_clause_expr buffer {value; _} = and print_map_expr buffer = function MapLookUp {value; _} -> print_map_lookup buffer value | MapInj inj -> print_injection buffer "map" print_binding inj +| BigMapInj inj -> print_injection buffer "big_map" print_binding inj and print_set_expr buffer = function SetInj inj -> print_injection buffer "set" print_expr inj @@ -1445,7 +1446,7 @@ and pp_map_expr buffer ~pad = function MapLookUp {value; _} -> pp_node buffer ~pad "MapLookUp"; pp_map_lookup buffer ~pad value -| MapInj {value; _} -> +| MapInj {value; _} | BigMapInj {value; _} -> pp_node buffer ~pad "MapInj"; pp_injection pp_binding buffer ~pad value diff --git a/src/passes/2-simplify/pascaligo.ml b/src/passes/2-simplify/pascaligo.ml index d094f3819..3928d8b5f 100644 --- a/src/passes/2-simplify/pascaligo.ml +++ b/src/passes/2-simplify/pascaligo.ml @@ -399,7 +399,7 @@ let rec simpl_expression (t:Raw.expr) : expr result = let%bind cases = simpl_cases lst in return @@ e_matching ~loc e cases ) - | EMap (MapInj mi) -> ( + | EMap (MapInj mi) -> ( let (mi , loc) = r_split mi in let%bind lst = let lst = List.map get_value @@ pseq_to_list mi.elements in @@ -411,6 +411,18 @@ let rec simpl_expression (t:Raw.expr) : expr result = bind_map_list aux lst in return @@ e_map ~loc lst ) + | EMap (BigMapInj mi) -> ( + let (mi , loc) = r_split mi in + let%bind lst = + let lst = List.map get_value @@ pseq_to_list mi.elements in + let aux : Raw.binding -> (expression * expression) result = + fun b -> + let%bind src = simpl_expression b.source in + let%bind dst = simpl_expression b.image in + ok (src, dst) in + bind_map_list aux lst in + return @@ e_big_map ~loc lst + ) | EMap (MapLookUp lu) -> ( let (lu , loc) = r_split lu in let%bind path = match lu.path with From 2a2c708b548ccdaf4e359a05993ffdf3f651af6b Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Tue, 22 Oct 2019 11:55:03 +0200 Subject: [PATCH 4/5] adding Big_map.* operators for cameligo --- src/passes/3-self_ast_simplified/literals.ml | 28 ++++++++++++++++++++ src/passes/operators/operators.ml | 9 +++++++ 2 files changed, 37 insertions(+) diff --git a/src/passes/3-self_ast_simplified/literals.ml b/src/passes/3-self_ast_simplified/literals.ml index 5d7be25b6..154851601 100644 --- a/src/passes/3-self_ast_simplified/literals.ml +++ b/src/passes/3-self_ast_simplified/literals.ml @@ -4,6 +4,27 @@ open Trace let peephole_expression : expression -> expression result = fun e -> let return expression = ok { e with expression } in match e.expression with + | E_constant ("BIG_MAP_LITERAL" , lst) -> ( + let%bind elt = + trace_option (simple_error "big_map literal expects a single parameter") @@ + List.to_singleton lst + in + let%bind lst = + trace (simple_error "big_map literal expects a list as parameter") @@ + get_e_list elt.expression + in + let aux = fun (e : expression) -> + trace (simple_error "big_map literal expects a list of pairs as parameter") @@ + let%bind tpl = get_e_tuple e.expression in + let%bind (a , b) = + trace_option (simple_error "of pairs") @@ + List.to_pair tpl + in + ok (a , b) + in + let%bind pairs = bind_map_list aux lst in + return @@ E_big_map pairs + ) | E_constant ("MAP_LITERAL" , lst) -> ( let%bind elt = trace_option (simple_error "map literal expects a single parameter") @@ @@ -25,6 +46,13 @@ let peephole_expression : expression -> expression result = fun e -> let%bind pairs = bind_map_list aux lst in return @@ E_map pairs ) + | E_constant ("BIG_MAP_EMPTY" , lst) -> ( + let%bind () = + trace_strong (simple_error "BIG_MAP_EMPTY expects no parameter") @@ + Assert.assert_list_empty lst + in + return @@ E_big_map [] + ) | E_constant ("MAP_EMPTY" , lst) -> ( let%bind () = trace_strong (simple_error "MAP_EMPTY expects no parameter") @@ diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index 65b55c18e..13510fedc 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -85,6 +85,7 @@ module Simplify = struct ("list_iter" , "LIST_ITER") ; ("list_fold" , "LIST_FOLD") ; ("list_map" , "LIST_MAP") ; + (*ici*) ("map_iter" , "MAP_ITER") ; ("map_map" , "MAP_MAP") ; ("map_fold" , "MAP_FOLD") ; @@ -167,6 +168,14 @@ module Simplify = struct ("Map.literal" , "MAP_LITERAL" ) ; ("Map.size" , "SIZE" ) ; + ("Big_map.find_opt" , "MAP_FIND_OPT") ; + ("Big_map.find" , "MAP_FIND") ; + ("Big_map.update" , "MAP_UPDATE") ; + ("Big_map.add" , "MAP_ADD") ; + ("Big_map.remove" , "MAP_REMOVE") ; + ("Big_map.literal" , "BIG_MAP_LITERAL" ) ; + ("Big_map.empty" , "BIG_MAP_EMPTY" ) ; + ("String.length", "SIZE") ; ("String.size", "SIZE") ; ("String.slice", "SLICE") ; From e6ee915f1e385329a7ef159d0e46318d60203bae Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Tue, 22 Oct 2019 11:55:36 +0200 Subject: [PATCH 5/5] updating tests (we don't need the big_map to be in a pair anymore) --- src/test/contracts/big_map.ligo | 31 ++++++++++++++++--------------- src/test/contracts/big_map.mligo | 25 +++++++++++++++++-------- src/test/integration_tests.ml | 2 +- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/test/contracts/big_map.ligo b/src/test/contracts/big_map.ligo index 8afc707b4..3d504aa75 100644 --- a/src/test/contracts/big_map.ligo +++ b/src/test/contracts/big_map.ligo @@ -1,4 +1,5 @@ type storage_ is big_map(int, int) * unit +type foo is big_map(int, int) function main(const p : unit; const s : storage_) : list(operation) * storage_ is var toto : option (int) := Some(0); @@ -8,28 +9,28 @@ function main(const p : unit; const s : storage_) : list(operation) * storage_ i } with ((nil: list(operation)), s) -function set_ (var n : int ; var m : storage_) : storage_ is block { - m.0[23] := n ; +function set_ (var n : int ; var m : foo) : foo is block { + m[23] := n ; } with m -function rm (var m : storage_) : storage_ is block { - remove 42 from map m.0; +function rm (var m : foo) : foo is block { + remove 42 from map m; } with m -function gf (const m : storage_) : int is begin skip end with get_force(23, m.0) +function gf (const m : foo) : int is begin skip end with get_force(23, m) -function get (const m : storage_) : option(int) is begin skip end with m.0[42] - -function mutimaps (const m : storage_; const n : storage_) : storage_ is block -{ - var foo : big_map(int,int) := m.0 ; - foo[42] := 0 ; - n.0[42] := get_force(42, foo) ; -} with n +function get (const m : foo) : option(int) is begin skip end with m[42] const empty_big_map : big_map(int,int) = big_map end -const map1 : big_map(int,int) = big_map +const big_map1 : big_map(int,int) = big_map 23 -> 0 ; 42 -> 0 ; -end \ No newline at end of file +end + +function mutimaps (const m : foo ; const n : foo) : foo is block +{ + var bar : foo := m ; + bar[42] := 0 ; + n[42] := get_force(42, bar) ; +} with n \ No newline at end of file diff --git a/src/test/contracts/big_map.mligo b/src/test/contracts/big_map.mligo index d032fad8c..52a366fe6 100644 --- a/src/test/contracts/big_map.mligo +++ b/src/test/contracts/big_map.mligo @@ -1,12 +1,21 @@ -type storage_ = ((int, int) big_map * unit) +type foo = (int, int) big_map -let set_ (n : int) (m : storage_) : storage_ = - (Map.update 23 (Some(n)) m.(0), ()) +let set_ (n : int) (m : foo) : foo = Big_map.update 23 (Some(n)) m -let rm (m : storage_) : storage_ = - (Map.remove 42 m.(0), ()) +let rm (m : foo) : foo = Big_map.remove 42 m -let gf (m : storage_) : int = Map.find 23 m.(0) +let gf (m : foo) : int = Big_map.find 23 m -let get (m: storage_): int option = - Map.find_opt 42 m.(0) \ No newline at end of file +let get (m: foo): int option = Big_map.find_opt 42 m + +let empty_map : foo = Big_map.empty + +let map1 : foo = Big_map.literal + [ (23 , 0) ; (42, 0) ] + +let map1 : foo = Big_map.literal + [ (23 , 0) ; (42, 0) ] + +let mutimaps (m : foo) (n : foo) : foo = + let bar : foo = Big_map.update 42 (Some(0)) m in + Big_map.update 42 (get(bar)) n \ No newline at end of file diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 401f446ce..98af59a7f 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -522,7 +522,7 @@ let big_map_ type_f path : unit result = let ez lst = let open Ast_simplified.Combinators in let lst' = List.map (fun (x, y) -> e_int x, e_int y) lst in - e_pair (e_typed_big_map lst' t_int t_int) (e_unit ()) + (e_typed_big_map lst' t_int t_int) in let%bind () = let make_input = fun n -> ez [(23, n) ; (42, 4)] in