Merge branch 'feature/pascaligo-pattern-parens' into 'dev'

Add parentheses in Pascaligo constructor patterns

See merge request ligolang/ligo!48
This commit is contained in:
Matej Šima 2019-06-24 17:26:56 +00:00
commit 86396933a2
14 changed files with 79 additions and 59 deletions

View File

@ -31,7 +31,7 @@ title: Cheat Sheet
|Assignment on an existing variable <br/></br>*⚠️ This feature is not supported at the top-level scope, you can use it e.g. within functions. Works for Records and Maps as well.*| ```age := 18;```, ```p.age := 21``` | |Assignment on an existing variable <br/></br>*⚠️ This feature is not supported at the top-level scope, you can use it e.g. within functions. Works for Records and Maps as well.*| ```age := 18;```, ```p.age := 21``` |
|Annotations| ```("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address)```| |Annotations| ```("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address)```|
|Variants|<pre><code>type action is<br/>&#124; Increment of int<br/>&#124; Decrement of int</code></pre>| |Variants|<pre><code>type action is<br/>&#124; Increment of int<br/>&#124; Decrement of int</code></pre>|
|Variant *(pattern)* matching|<pre><code>const a: action = Increment(5);<br/>case a of<br/>&#124; Increment n -> n + 1<br/>&#124; Decrement n -> n - 1<br/>end</code></pre>| |Variant *(pattern)* matching|<pre><code>const a: action = Increment(5);<br/>case a of<br/>&#124; Increment(n) -> n + 1<br/>&#124; Decrement(n) -> n - 1<br/>end</code></pre>|
|Records|<pre><code>type person is record<br/>&nbsp;&nbsp;age: int ;<br/>&nbsp;&nbsp;name: string ;<br/>end<br/><br/>const john : person = record<br/>&nbsp;&nbsp;age = 18;<br/>&nbsp;&nbsp;name = "John Doe";<br/>end<br/><br/>const name: string = john.name;</code></pre>| |Records|<pre><code>type person is record<br/>&nbsp;&nbsp;age: int ;<br/>&nbsp;&nbsp;name: string ;<br/>end<br/><br/>const john : person = record<br/>&nbsp;&nbsp;age = 18;<br/>&nbsp;&nbsp;name = "John Doe";<br/>end<br/><br/>const name: string = john.name;</code></pre>|
|Maps|<pre><code>type prices is map(nat, tez);<br/><br/>const prices : prices = map<br/>&nbsp;&nbsp;10n -> 60mtz;<br/>&nbsp;&nbsp;50n -> 30mtz;<br/>&nbsp;&nbsp;100n -> 10mtz;<br/>end<br/><br/>const price: option(tez) = prices[50n];</code></pre>| |Maps|<pre><code>type prices is map(nat, tez);<br/><br/>const prices : prices = map<br/>&nbsp;&nbsp;10n -> 60mtz;<br/>&nbsp;&nbsp;50n -> 30mtz;<br/>&nbsp;&nbsp;100n -> 10mtz;<br/>end<br/><br/>const price: option(tez) = prices[50n];</code></pre>|
|Contracts & Accounts|<pre><code>const destinationAddress : address = "tz1...";<br/>const contract : contract(unit) = get_contract(destinationAddress);</code></pre>| |Contracts & Accounts|<pre><code>const destinationAddress : address = "tz1...";<br/>const contract : contract(unit) = get_contract(destinationAddress);</code></pre>|

View File

@ -37,8 +37,8 @@ type action is
function main (const action: action ; const counter: int) : (list(operation) * int) is function main (const action: action ; const counter: int) : (list(operation) * int) is
block {skip} with ((nil : list(operation)), block {skip} with ((nil : list(operation)),
case action of case action of
| Increment number -> counter + number | Increment(number) -> counter + number
| Decrement number -> counter - number | Decrement(number) -> counter - number
end) end)
``` ```

View File

@ -34,8 +34,8 @@ type action is
function main (const p : action ; const s : int) : (list(operation) * int) is function main (const p : action ; const s : int) : (list(operation) * int) is
block {skip} with ((nil : list(operation)), block {skip} with ((nil : list(operation)),
case p of case p of
| Increment n -> s + n | Increment(n) -> s + n
| Decrement n -> s - n | Decrement(n) -> s - n
end) end)
``` ```

View File

@ -40,7 +40,7 @@ class HomeSplash extends React.Component {
<div id="tab-group-3-content-4" className="tab-pane active" data-group="group_3" tabIndex="-1"> <div id="tab-group-3-content-4" className="tab-pane active" data-group="group_3" tabIndex="-1">
<div> <div>
<span> <span>
<pre><code className="hljs css language-Pascal">// variant defining pseudo multi-entrypoint actions<br />type action is<br />| Increment of int<br />| Decrement of int<br /><br />function add (const a : int ; const b : int) : int is<br /> block {'{ skip }'} with a + b<br /><br />function subtract (const a : int ; const b : int) : int is<br /> block {'{ skip }'} with a - b<br /><br />// real entrypoint that re-routes the flow based on the action provided<br />function main (const p : action ; const s : int) : (list(operation) * int) is<br /> block {'{ skip }'} with ((nil : list(operation)),<br /> case p of<br /> | Increment n -&gt; add(s, n)<br /> | Decrement n -&gt; subtract(s, n)<br /> end)<br /></code></pre> <pre><code className="hljs css language-Pascal">// variant defining pseudo multi-entrypoint actions<br />type action is<br />| Increment of int<br />| Decrement of int<br /><br />function add (const a : int ; const b : int) : int is<br /> block {'{ skip }'} with a + b<br /><br />function subtract (const a : int ; const b : int) : int is<br /> block {'{ skip }'} with a - b<br /><br />// real entrypoint that re-routes the flow based on the action provided<br />function main (const p : action ; const s : int) : (list(operation) * int) is<br /> block {'{ skip }'} with ((nil : list(operation)),<br /> case p of<br /> | Increment(n) -&gt; add(s, n)<br /> | Decrement(n) -&gt; subtract(s, n)<br /> end)<br /></code></pre>
</span> </span>
</div> </div>
</div> </div>

View File

@ -92,7 +92,7 @@ function buy_single(const action : action_buy_single ; const s : storage_type) :
function main(const action : action ; const s : storage_type) : (list(operation) * storage_type) is function main(const action : action ; const s : storage_type) : (list(operation) * storage_type) is
block {skip} with block {skip} with
case action of case action of
| Buy_single bs -> buy_single (bs , s) | Buy_single (bs) -> buy_single (bs , s)
| Sell_single as -> sell_single (as , s) | Sell_single (as) -> sell_single (as , s)
| Transfer_single at -> transfer_single (at , s) | Transfer_single (at) -> transfer_single (at , s)
end end

View File

@ -11,6 +11,6 @@ function decrement(const i : int ; const n : int) : int is
function main (const p : action ; const s : int) : (list(operation) * int) is function main (const p : action ; const s : int) : (list(operation) * int) is
block {skip} with ((nil : list(operation)), block {skip} with ((nil : list(operation)),
case p of case p of
| Increment n -> increment(s , n) | Increment (n) -> increment (s, n)
| Decrement n -> decrement(s , n) | Decrement (n) -> decrement (s, n)
end) end)

View File

@ -12,7 +12,7 @@ function match_option (const o : option(int)) : int is
begin begin
case o of case o of
| None -> skip | None -> skip
| Some(s) -> result := s | Some (s) -> result := s
end end
end with result end with result
@ -27,5 +27,5 @@ function match_expr_option (const o : option(int)) : int is
begin skip end with begin skip end with
case o of case o of
| None -> 42 | None -> 42
| Some(s) -> s | Some (s) -> s
end end

View File

@ -5,6 +5,6 @@ type action is
function main (const p : action ; const s : int) : (list(operation) * int) is function main (const p : action ; const s : int) : (list(operation) * int) is
block {skip} with ((nil : list(operation)), block {skip} with ((nil : list(operation)),
case p of case p of
| Increment n -> s + n | Increment (n) -> s + n
| Decrement n -> s - n | Decrement (n) -> s - n
end) end)

View File

@ -13,6 +13,6 @@ function subtract (const a : int ; const b : int) : int is
function main (const p : action ; const s : int) : (list(operation) * int) is function main (const p : action ; const s : int) : (list(operation) * int) is
block {skip} with ((nil : list(operation)), block {skip} with ((nil : list(operation)),
case p of case p of
| Increment n -> add(s, n) | Increment(n) -> add(s, n)
| Decrement n -> subtract(s, n) | Decrement(n) -> subtract(s, n)
end) end)

View File

@ -632,7 +632,7 @@ and arguments = tuple_injection
and pattern = and pattern =
PCons of (pattern, cons) nsepseq reg PCons of (pattern, cons) nsepseq reg
| PConstr of (constr * pattern reg) reg | PConstr of (constr * tuple_pattern option) reg
| PVar of Lexer.lexeme reg | PVar of Lexer.lexeme reg
| PWild of wild | PWild of wild
| PInt of (Lexer.lexeme * Z.t) reg | PInt of (Lexer.lexeme * Z.t) reg
@ -644,7 +644,9 @@ and pattern =
| PNone of c_None | PNone of c_None
| PSome of (c_Some * pattern par reg) reg | PSome of (c_Some * pattern par reg) reg
| PList of list_pattern | PList of list_pattern
| PTuple of (pattern, comma) nsepseq par reg | PTuple of tuple_pattern
and tuple_pattern = (pattern, comma) nsepseq par reg
and list_pattern = and list_pattern =
Sugar of pattern injection reg Sugar of pattern injection reg

View File

@ -616,7 +616,7 @@ and arguments = tuple_injection
and pattern = and pattern =
PCons of (pattern, cons) nsepseq reg PCons of (pattern, cons) nsepseq reg
| PConstr of (constr * pattern reg) reg | PConstr of (constr * tuple_pattern option) reg
| PVar of Lexer.lexeme reg | PVar of Lexer.lexeme reg
| PWild of wild | PWild of wild
| PInt of (Lexer.lexeme * Z.t) reg | PInt of (Lexer.lexeme * Z.t) reg
@ -628,7 +628,9 @@ and pattern =
| PNone of c_None | PNone of c_None
| PSome of (c_Some * pattern par reg) reg | PSome of (c_Some * pattern par reg) reg
| PList of list_pattern | PList of list_pattern
| PTuple of (pattern, comma) nsepseq par reg | PTuple of tuple_pattern
and tuple_pattern = (pattern, comma) nsepseq par reg
and list_pattern = and list_pattern =
Sugar of pattern injection reg Sugar of pattern injection reg

View File

@ -1052,22 +1052,22 @@ pattern:
in PCons {region; value=$1}} in PCons {region; value=$1}}
core_pattern: core_pattern:
var { PVar $1 } var { PVar $1 }
| WILD { PWild $1 } | WILD { PWild $1 }
| Int { PInt $1 } | Int { PInt $1 }
| String { PString $1 } | String { PString $1 }
| C_Unit { PUnit $1 } | C_Unit { PUnit $1 }
| C_False { PFalse $1 } | C_False { PFalse $1 }
| C_True { PTrue $1 } | C_True { PTrue $1 }
| C_None { PNone $1 } | C_None { PNone $1 }
| list_patt { PList $1 } | list_pattern { PList $1 }
| tuple_patt { PTuple $1 } | tuple_pattern { PTuple $1 }
| constr_patt { PConstr $1 } | constr_pattern { PConstr $1 }
| C_Some par(core_pattern) { | C_Some par(core_pattern) {
let region = cover $1 $2.region let region = cover $1 $2.region
in PSome {region; value = $1,$2}} in PSome {region; value = $1,$2}}
list_patt: list_pattern:
injection(List,core_pattern) { Sugar $1 } injection(List,core_pattern) { Sugar $1 }
| Nil { PNil $1 } | Nil { PNil $1 }
| par(cons_pattern) { Raw $1 } | par(cons_pattern) { Raw $1 }
@ -1075,15 +1075,14 @@ list_patt:
cons_pattern: cons_pattern:
core_pattern CONS pattern { $1,$2,$3 } core_pattern CONS pattern { $1,$2,$3 }
tuple_patt: tuple_pattern:
par(nsepseq(core_pattern,COMMA)) { $1 } par(nsepseq(core_pattern,COMMA)) { $1 }
constr_patt: constr_pattern:
Constr core_pattern { Constr tuple_pattern {
let second = let region = cover $1.region $2.region
let region = pattern_to_region $2 in in {region; value = $1, Some $2}
{region; value=$2} }
in | Constr {
let region = cover $1.region second.region in {region=$1.region; value = $1, None}
let value = ($1 , second) in }
{region; value}}

View File

@ -681,8 +681,10 @@ and print_pattern = function
and print_constr_pattern {value; _} = and print_constr_pattern {value; _} =
let (constr, args) = value in let (constr, args) = value in
print_constr constr ; print_constr constr;
print_pattern args.value ; match args with
None -> ()
| Some tuple -> print_ptuple tuple
and print_psome {value; _} = and print_psome {value; _} =
let c_Some, patterns = value in let c_Some, patterns = value in

View File

@ -15,6 +15,17 @@ let pseq_to_list = function
let get_value : 'a Raw.reg -> 'a = fun x -> x.value let get_value : 'a Raw.reg -> 'a = fun x -> x.value
module Errors = struct module Errors = struct
let unsupported_cst_constr p =
let title () = "constant constructor" in
let message () =
Format.asprintf "constant constructors are not supported yet" in
let pattern_loc = Raw.pattern_to_region p in
let data = [
("pattern_loc",
fun () -> Format.asprintf "%a" Location.pp_lift @@ pattern_loc)
] in
error ~data title message
let unsupported_ass_None region = let unsupported_ass_None region =
let title () = "assignment of None" in let title () = "assignment of None" in
let message () = let message () =
@ -848,33 +859,37 @@ and simpl_cases : type a . (Raw.pattern * a) list -> a matching result = fun t -
let get_var (t:Raw.pattern) = let get_var (t:Raw.pattern) =
match t with match t with
| PVar v -> ok v.value | PVar v -> ok v.value
| p -> fail @@ unsupported_non_var_pattern p | p -> fail @@ unsupported_non_var_pattern p in
in let get_tuple (t: Raw.pattern) =
let get_tuple (t:Raw.pattern) = match t with match t with
| PCons v -> npseq_to_list v.value | PCons v -> npseq_to_list v.value
| PTuple v -> npseq_to_list v.value.inside | PTuple v -> npseq_to_list v.value.inside
| x -> [ x ] | x -> [ x ] in
in let get_single (t: Raw.pattern) =
let get_single (t:Raw.pattern) =
let t' = get_tuple t in let t' = get_tuple t in
let%bind () = let%bind () =
trace_strong (unsupported_tuple_pattern t) @@ trace_strong (unsupported_tuple_pattern t) @@
Assert.assert_list_size t' 1 in Assert.assert_list_size t' 1 in
ok (List.hd t') in ok (List.hd t') in
let get_constr (t:Raw.pattern) = match t with let get_constr (t: Raw.pattern) =
| PConstr v -> match t with
let%bind var = get_single (snd v.value).value >>? get_var in | PConstr v -> (
ok ((fst v.value).value , var) let (const , pat_opt) = v.value in
| _ -> fail @@ only_constructors t let%bind pat =
in trace_option (unsupported_cst_constr t) @@
pat_opt in
let%bind single_pat = get_single (PTuple pat) in
let%bind var = get_var single_pat in
ok (const.value , var)
)
| _ -> fail @@ only_constructors t in
let%bind patterns = let%bind patterns =
let aux (x , y) = let aux (x , y) =
let xs = get_tuple x in let xs = get_tuple x in
trace_strong (unsupported_tuple_pattern x) @@ trace_strong (unsupported_tuple_pattern x) @@
Assert.assert_list_size xs 1 >>? fun () -> Assert.assert_list_size xs 1 >>? fun () ->
ok (List.hd xs , y) ok (List.hd xs , y)
in in bind_map_list aux t in
bind_map_list aux t in
match patterns with match patterns with
| [(PFalse _ , f) ; (PTrue _ , t)] | [(PFalse _ , f) ; (PTrue _ , t)]
| [(PTrue _ , t) ; (PFalse _ , f)] -> | [(PTrue _ , t) ; (PFalse _ , f)] ->