From 8d291b2d2e4d79ae91885085f5c79f5021cdaaa6 Mon Sep 17 00:00:00 2001 From: Christian Rinderknecht Date: Mon, 8 Jun 2020 11:59:58 +0200 Subject: [PATCH] Finished v1 of PascaLIGO pretty-printer. --- src/passes/01-parser/cameligo/Pretty.ml | 4 +- src/passes/01-parser/pascaligo/Pretty.ml | 30 ++++--- src/passes/01-parser/pascaligo/Tests/pp.ligo | 40 +++++---- src/passes/1-parser/cameligo/Tests/pp.mligo | 29 ------- src/passes/1-parser/pascaligo/Tests/pp.ligo | 91 -------------------- 5 files changed, 44 insertions(+), 150 deletions(-) delete mode 100644 src/passes/1-parser/cameligo/Tests/pp.mligo delete mode 100644 src/passes/1-parser/pascaligo/Tests/pp.ligo diff --git a/src/passes/01-parser/cameligo/Pretty.ml b/src/passes/01-parser/cameligo/Pretty.ml index 8fa298e3f..ba8451d52 100644 --- a/src/passes/01-parser/cameligo/Pretty.ml +++ b/src/passes/01-parser/cameligo/Pretty.ml @@ -72,9 +72,9 @@ and pp_pattern = function and pp_pconstr = function PNone _ -> string "None" | PSomeApp p -> pp_patt_some p -| PConstrApp a -> pp_patt_c_app a +| PConstrApp a -> pp_pconstr_app a -and pp_patt_c_app {value; _} = +and pp_pconstr_app {value; _} = match value with constr, None -> pp_ident constr | constr, Some pat -> diff --git a/src/passes/01-parser/pascaligo/Pretty.ml b/src/passes/01-parser/pascaligo/Pretty.ml index 462901e00..ecf7d0d44 100644 --- a/src/passes/01-parser/pascaligo/Pretty.ml +++ b/src/passes/01-parser/pascaligo/Pretty.ml @@ -148,7 +148,7 @@ and pp_fun_decl {value; _} = None -> empty | Some a -> hardline ^^ pp_attr_decl a in prefix 2 1 start parameters - ^^ group (nest 2 (break 1 ^^ string ": " ^^ return_t ^^ string " is")) + ^^ group (nest 2 (break 1 ^^ string ": " ^^ nest 2 return_t ^^ string " is")) ^^ body ^^ attr and pp_parameters p = pp_nsepseq ";" pp_param_decl p @@ -456,11 +456,20 @@ and pp_constr_expr = function | NoneExpr _ -> string "None" | ConstrApp a -> pp_constr_app a -and pp_some_app {value; _} = string "TODO:pp_some_app" +and pp_some_app {value; _} = + prefix 4 1 (string "Some") (pp_arguments (snd value)) -and pp_constr_app {value; _} = string "TODO:pp_constr_app" +and pp_constr_app {value; _} = + let constr, args = value in + let constr = string constr.value in + match args with + None -> constr + | Some tuple -> prefix 2 1 constr (pp_tuple_expr tuple) -and pp_field_assign {value; _} = string "TODO:pp_field_assign" + +and pp_field_assign {value; _} = + let {field_name; field_expr; _} = value in + prefix 2 1 (pp_ident field_name ^^ string " =") (pp_expr field_expr) and pp_record ne_inj = group (pp_ne_injection pp_field_assign ne_inj) @@ -471,7 +480,11 @@ and pp_projection {value; _} = let fields = separate_map sep pp_selection fields in group (pp_ident struct_name ^^ string "." ^^ break 0 ^^ fields) -and pp_update {value; _} = string "TODO:pp_update" +and pp_update {value; _} = + let {record; updates; _} = value in + let updates = group (pp_ne_injection pp_field_path_assign updates) + and record = pp_path record in + record ^^ string " with" ^^ nest 2 (break 1 ^^ updates) and pp_field_path_assign {value; _} = let {field_path; field_expr; _} = value in @@ -540,10 +553,7 @@ and pp_ne_injection_kwd = function | NEInjRecord _ -> "record" and pp_nsepseq : - 'a.string -> - ('a -> document) -> - ('a, t) Utils.nsepseq -> - document = + 'a.string -> ('a -> document) -> ('a, t) Utils.nsepseq -> document = fun sep printer elements -> let elems = Utils.nsepseq_to_list elements and sep = string sep ^^ break 1 @@ -596,7 +606,7 @@ and pp_tuple_pattern {value; _} = | e::items -> group (break 1 ^^ pp_pattern e ^^ string ",") ^^ app items in let components = - if tail = [] + if tail = [] then pp_pattern head else pp_pattern head ^^ string "," ^^ app (List.map snd tail) in string "(" ^^ nest 1 (components ^^ string ")") diff --git a/src/passes/01-parser/pascaligo/Tests/pp.ligo b/src/passes/01-parser/pascaligo/Tests/pp.ligo index da28debcd..2dd2563df 100644 --- a/src/passes/01-parser/pascaligo/Tests/pp.ligo +++ b/src/passes/01-parser/pascaligo/Tests/pp.ligo @@ -1,19 +1,23 @@ +function incr_map (const l : list (int)) : list (int) is + List.map (function (const i : int) : int is i + 1, l) + type t is timestamp * nat -> map (string, address) type u is A | B of t * int | C of int -> (string -> int) type v is record aaaaaa : ttttttt; bbbbbb : record ccccccccc : string end end -(* + function back (var store : store) : list (operation) * store is begin - var operations : list (operation) := list []; - const a : nat = 0n; - x0 := record foo = "1"; bar = 4n end; - x1 := nil; - x2 := list end; + var operations : list (operation) := list []; + const operations : list (operation) = list []; + const a : nat = 0n; + x0 := record foo = "1"; bar = 4n end; + x1 := nil; + x2 := list end; x3 := 3#4# list [5; 6]; case foo of 10n -> skip end; - if s contains x then skip else skip; +if saaa.0.1.2.a.b.b.x contains xxxxxxxxxxxxxxx[123] then skip else skip; s := set [3_000mutez; -2; 1n]; a := A; b := B (a); @@ -21,12 +25,12 @@ function back (var store : store) : list (operation) * store is d := None; e := Some (a, B (b)); z := z.1.2; - x := map [1 -> "1"; 2 -> "2"]; +x := if true then map [1 -> "1"; 2 -> "2"; 3 -> "3"; 4 -> "4"; 5 -> "5555555555555555"] else Unit; y := a.b.c[3]; a := "hello " ^ "world" ^ "!"; - r := record a = 0 end; - r := r with record a = 42 end; - patch store.backers with set [(1); f(2*3)]; + r := record aaaaaaaaaaaa = 100000000; bbbbbbb = ffffff (2, aa, x, y) + 1 end; + r := r with record aaaaaaaaaaa = 444442; bbbbbbbbb = 43 + f (z) / 234 end; + patch store.backers.8.aa.33333.5 with set [(1); f(2*3); 123124234/2345]; remove (1,2,3) from set foo.bar; remove 3 from map foo.bar; patch store.backers with map [sender -> amount]; @@ -39,7 +43,7 @@ function back (var store : store) : list (operation) * store is begin acc := 2 - (if toggle then f(x) else Unit); end; - for i := 1n to 10n + for i := 1n to 10n step 2n begin acc := acc + i; end; @@ -53,10 +57,11 @@ function back (var store : store) : list (operation) * store is | False#True#Unit#0xAA#"hi"#4#nil -> skip ] end with (operations, store, (more_stuff, and_here_too)) -*) + function claim (var store : store; const bar : t; const baz : u; var z : operations * store * (more_stuff * and_here_too)) : list (operation) * store * timestamp * nat -> map (string, address) is begin - var operations : list (operation * map (address, map (longname, domain))) := nilllllllllll; + const operations : list (operation * map (address, map (longname, domain))) = nilllllllllll; +var operations : list (operation * map (address, map (longname, domain))) := nilllllllllll; attributes ["foo"; "inline"]; if now <= store.deadline then failwith ("Too soon.") @@ -64,20 +69,20 @@ function back (var store : store) : list (operation) * store is case store.backers[sender] of None -> failwith ("Not a backer.") + | Some (0) -> skip | Some (quantity) -> if balance >= store.goal or store.funded then failwith ("Goal reached: no refund.") else begin - operations.0.foo := list [transaction (unit, sender, quantity)]; - remove sender from map store.backers + operations.0.foo := list [transaction (unit, sender, quantity); transaction (foo, bar, bazzzzzzzzzzzzzzz)]; + remove sender.0099999.fffff [fiar (abaxxasfdf)] from map store.backers.foooooo.barrrrr.01.bazzzzzzz end end end with long_function_name (operations, store, (more_stuff, (and_here_too, well_in_here_too), hello)) attributes ["inline"; "foo"] -(* function withdraw (var store : store) : list (operation) * store is begin var operations : list (operation) := list end; @@ -95,4 +100,3 @@ function withdraw (var store : store) : list (operation) * store is nil -> (operations, (store : store)) | _ -> (operations, store) end -*) diff --git a/src/passes/1-parser/cameligo/Tests/pp.mligo b/src/passes/1-parser/cameligo/Tests/pp.mligo deleted file mode 100644 index d84c270aa..000000000 --- a/src/passes/1-parser/cameligo/Tests/pp.mligo +++ /dev/null @@ -1,29 +0,0 @@ -type q = {a: int; b: {c: string}} -type r = int list -type s = (int, address) map -type t = int -type u = {a: int; b: t * char} -type v = int * (string * address) -type w = timestamp * nat -> (string, address) map -type x = A | B of t * int | C of int -> (string -> int) - -let x = 4 -let y : t = (if true then -3 + f x x else 0) - 1 -let f (x: int) y = (x : int) -let z : (t) = y -let w = - match f 3 with - None -> [] - | Some (1::[2;3]) -> [4;5]::[] -let n : nat = 0n -let a = A -let b = B a -let c = C (a, B (a)) -let d = None -let e = Some (a, B b) -let z = z.1.2 -let v = "hello" ^ "world" ^ "!" -let w = Map.literal [(1,"1"); (2,"2")] - -let r = { field = 0} -let r = { r with field = 42} diff --git a/src/passes/1-parser/pascaligo/Tests/pp.ligo b/src/passes/1-parser/pascaligo/Tests/pp.ligo deleted file mode 100644 index f4b185f69..000000000 --- a/src/passes/1-parser/pascaligo/Tests/pp.ligo +++ /dev/null @@ -1,91 +0,0 @@ -type t is timestamp * nat -> map (string, address) -type u is A | B of t * int | C of int -> (string -> int) -type v is record a : t; b : record c : string end end - -function back (var store : store) : list (operation) * store is - var operations : list (operation) := list [] - begin - const a : nat = 0n; - x0 := record foo = "1"; bar = 4n end; - x1 := nil; - x2 := list end; - x3 := 3#4# list [5; 6]; - case foo of - 10n -> skip - end; - if s contains x then skip else skip; - s := set [3_000mtz; -2; 1n]; - a := A; - b := B (a); - c := C (a, B (a)); - d := None; - e := Some (a, B (b)); - z := z.1.2; - x := map [1 -> "1"; 2 -> "2"]; - y := a.b.c[3]; - a := "hello " ^ "world" ^ "!"; - patch store.backers with set [(1); f(2*3)]; - remove (1,2,3) from set foo.bar; - remove 3 from map foo.bar; - patch store.backers with map [sender -> amount]; - if now > store.deadline and (not True) then - begin - f (x,1); - for k -> d : int * string in map m block { skip }; - for x : int in set s block { skip }; - while i < 10n - begin - acc := 2 - (if toggle then f(x) else Unit); - end; - for i := 1n to 10n - begin - acc := acc + i; - end; - failwith ("Deadline passed"); - end - else - case store.backers[sender] of [ - None -> store.0.backers[sender] := amount - | Some (_) -> skip - | B (x, C (y,z)) -> skip - | False#True#Unit#0xAA#"hi"#4#nil -> skip - ] - end with (operations, store) - -function claim (var store : store) : list (operation) * store is - var operations : list (operation) := nil - begin - if now <= store.deadline then - failwith ("Too soon.") - else - case store.backers[sender] of - None -> - failwith ("Not a backer.") - | Some (amount) -> - if balance >= store.goal or store.funded then - failwith ("Goal reached: no refund.") - else - begin - operations.0.foo := list [transaction (unit, sender, amount)]; - remove sender from map store.backers - end - end - end with (operations, store) - -function withdraw (var store : store) : list (operation) * store is - var operations : list (operation) := list end - begin - if sender = owner then - if now >= store.deadline then - if balance >= store.goal then { -// store.funded := True; - patch store with record funded = True; a = b end; - operations := list [Transfer (owner, balance)]; - }; - else failwith ("Below target.") - else failwith ("Too soon."); - else skip - end with case (foo: bar) of - nil -> (operations, (store : store)) - | _ -> (operations, store) - end