From 99defca21a05aeeb1c0c69f4d9a16d4fa8fd0742 Mon Sep 17 00:00:00 2001 From: Christian Rinderknecht Date: Mon, 1 Apr 2019 20:59:37 +0200 Subject: [PATCH] Added alternate syntax for "case" constructs (both expression and instruction). New syntax: case ... of [ ... ] --- src/ligo/ligo-parser/AST.ml | 28 +++++++-------- src/ligo/ligo-parser/AST.mli | 4 +-- src/ligo/ligo-parser/Parser.mly | 36 ++++++++++++++------ src/ligo/ligo-parser/Tests/crowdfunding.ligo | 4 +-- 4 files changed, 43 insertions(+), 29 deletions(-) diff --git a/src/ligo/ligo-parser/AST.ml b/src/ligo/ligo-parser/AST.ml index cf2de7662..03c0af8d1 100644 --- a/src/ligo/ligo-parser/AST.ml +++ b/src/ligo/ligo-parser/AST.ml @@ -419,12 +419,12 @@ and set_membership = { } and 'a case = { - kwd_case : kwd_case; - expr : expr; - kwd_of : kwd_of; - lead_vbar : vbar option; - cases : ('a case_clause reg, vbar) nsepseq reg; - kwd_end : kwd_end + kwd_case : kwd_case; + expr : expr; + opening : opening; + lead_vbar : vbar option; + cases : ('a case_clause reg, vbar) nsepseq reg; + closing : closing } and 'a case_clause = { @@ -1111,14 +1111,14 @@ and print_if_clause = function print_token rbrace "}" and print_case_instr (node : instruction case) = - let {kwd_case; expr; kwd_of; - lead_vbar; cases; kwd_end} = node in + let {kwd_case; expr; opening; + lead_vbar; cases; closing} = node in print_token kwd_case "case"; print_expr expr; - print_token kwd_of "of"; + print_opening "of" opening; print_token_opt lead_vbar "|"; print_cases_instr cases; - print_token kwd_end "end" + print_closing closing and print_token_opt = function None -> fun _ -> () @@ -1222,14 +1222,14 @@ and print_expr = function | EPar e -> print_par_expr e and print_case_expr (node : expr case) = - let {kwd_case; expr; kwd_of; - lead_vbar; cases; kwd_end} = node in + let {kwd_case; expr; opening; + lead_vbar; cases; closing} = node in print_token kwd_case "case"; print_expr expr; - print_token kwd_of "of"; + print_opening "of" opening; print_token_opt lead_vbar "|"; print_cases_expr cases; - print_token kwd_end "end" + print_closing closing and print_cases_expr {value; _} = print_nsepseq "|" print_case_clause_expr value diff --git a/src/ligo/ligo-parser/AST.mli b/src/ligo/ligo-parser/AST.mli index cf8ac5ddc..306ceb94d 100644 --- a/src/ligo/ligo-parser/AST.mli +++ b/src/ligo/ligo-parser/AST.mli @@ -405,10 +405,10 @@ and set_membership = { and 'a case = { kwd_case : kwd_case; expr : expr; - kwd_of : kwd_of; + opening : opening; lead_vbar : vbar option; cases : ('a case_clause reg, vbar) nsepseq reg; - kwd_end : kwd_end + closing : closing } and 'a case_clause = { diff --git a/src/ligo/ligo-parser/Parser.mly b/src/ligo/ligo-parser/Parser.mly index ca79c8934..ae0e8f088 100644 --- a/src/ligo/ligo-parser/Parser.mly +++ b/src/ligo/ligo-parser/Parser.mly @@ -686,18 +686,22 @@ case_instr: let value = { kwd_case = $1; expr = $2; - kwd_of = $3; + opening = Kwd $3; lead_vbar = $4; cases = $5; - kwd_end = $6} - in {region; value}} -(*| Case expr LBRACKET option(VBAR) case_instr RBRACKET { - let region = cover $1 $6 in - let value = { - k - } + closing = End $6} + in {region; value} } - *) +| Case expr Of LBRACKET option(VBAR) cases_instr RBRACKET { + let region = cover $1 $7 in + let value = { + kwd_case = $1; + expr = $2; + opening = KwdBracket ($3,$4); + lead_vbar = $5; + cases = $6; + closing = RBracket $7} + in {region; value}} cases_instr: nsepseq(case_clause_instr,VBAR) { @@ -790,10 +794,20 @@ case_expr: let value : expr case = { kwd_case = $1; expr = $2; - kwd_of = $3; + opening = Kwd $3; lead_vbar = $4; cases = $5; - kwd_end = $6} + closing = End $6} + in ECase {region; value}} +| Case expr Of LBRACKET option(VBAR) cases_expr RBRACKET { + let region = cover $1 $7 in + let value = { + kwd_case = $1; + expr = $2; + opening = KwdBracket ($3,$4); + lead_vbar = $5; + cases = $6; + closing = RBracket $7} in ECase {region; value}} cases_expr: diff --git a/src/ligo/ligo-parser/Tests/crowdfunding.ligo b/src/ligo/ligo-parser/Tests/crowdfunding.ligo index 34d45595a..834f319ca 100644 --- a/src/ligo/ligo-parser/Tests/crowdfunding.ligo +++ b/src/ligo/ligo-parser/Tests/crowdfunding.ligo @@ -17,11 +17,11 @@ entrypoint contribute (storage store : store; if now > store.deadline then fail "Deadline passed"; else - case store.backers[sender] of + case store.backers[sender] of [ None -> store.backers[sender] := Some (amount) // None -> patch store.backers with map sender -> amount end | _ -> skip - end + ] } with (store, operations) entrypoint withdraw (storage store : store; const sender : address)