Extended conditional syntax for "else" clauses.

New syntax: if ... then ... else { ... }
This commit is contained in:
Christian Rinderknecht 2019-03-25 17:25:10 +01:00
parent aced77e085
commit 98f9d3e417
No known key found for this signature in database
GPG Key ID: 9446816CFD267040
4 changed files with 31 additions and 26 deletions

24
AST.ml
View File

@ -406,15 +406,15 @@ and conditional = {
kwd_if : kwd_if; kwd_if : kwd_if;
test : test_expr; test : test_expr;
kwd_then : kwd_then; kwd_then : kwd_then;
ifso : ifso; ifso : if_clause;
terminator : semi option; terminator : semi option;
kwd_else : kwd_else; kwd_else : kwd_else;
ifnot : instruction ifnot : if_clause
} }
and ifso = and if_clause =
ThenInstr of instruction ClauseInstr of instruction
| ThenBlock of (instructions * semi option) braces reg | ClauseBlock of (instructions * semi option) braces reg
and test_expr = and test_expr =
GenExpr of expr GenExpr of expr
@ -759,6 +759,10 @@ let instr_to_region = function
| Single SetRemove {region; _} | Single SetRemove {region; _}
| Block {region; _} -> region | Block {region; _} -> region
let if_clause_to_region = function
ClauseInstr instr -> instr_to_region instr
| ClauseBlock {region; _} -> region
let pattern_to_region = function let pattern_to_region = function
PCons {region; _} PCons {region; _}
| PVar {region; _} | PVar {region; _}
@ -1076,14 +1080,14 @@ and print_conditional node =
print_token kwd_if "if"; print_token kwd_if "if";
print_test_expr test; print_test_expr test;
print_token kwd_then "then"; print_token kwd_then "then";
print_ifso ifso; print_if_clause ifso;
print_terminator terminator; print_terminator terminator;
print_token kwd_else "else"; print_token kwd_else "else";
print_instruction ifnot print_if_clause ifnot
and print_ifso = function and print_if_clause = function
ThenInstr instr -> print_instruction instr ClauseInstr instr -> print_instruction instr
| ThenBlock {value; _} -> | ClauseBlock {value; _} ->
let {lbrace; inside; rbrace} = value in let {lbrace; inside; rbrace} = value in
let instr, terminator = inside in let instr, terminator = inside in
print_token lbrace "{"; print_token lbrace "{";

11
AST.mli
View File

@ -390,15 +390,15 @@ and conditional = {
kwd_if : kwd_if; kwd_if : kwd_if;
test : test_expr; test : test_expr;
kwd_then : kwd_then; kwd_then : kwd_then;
ifso : ifso; ifso : if_clause;
terminator : semi option; terminator : semi option;
kwd_else : kwd_else; kwd_else : kwd_else;
ifnot : instruction ifnot : if_clause
} }
and ifso = and if_clause =
ThenInstr of instruction ClauseInstr of instruction
| ThenBlock of (instructions * semi option) braces reg | ClauseBlock of (instructions * semi option) braces reg
and test_expr = and test_expr =
GenExpr of expr GenExpr of expr
@ -651,6 +651,7 @@ val local_decl_to_region : local_decl -> Region.t
val path_to_region : path -> Region.t val path_to_region : path -> Region.t
val lhs_to_region : lhs -> Region.t val lhs_to_region : lhs -> Region.t
val rhs_to_region : rhs -> Region.t val rhs_to_region : rhs -> Region.t
val if_clause_to_region : if_clause -> Region.t
(* Printing *) (* Printing *)

View File

@ -580,8 +580,8 @@ proc_call:
fun_call { $1 } fun_call { $1 }
conditional: conditional:
If test_expr Then ifso option(SEMI) Else instruction { If test_expr Then if_clause option(SEMI) Else if_clause {
let region = cover $1 (instr_to_region $7) in let region = cover $1 (if_clause_to_region $7) in
let value = { let value = {
kwd_if = $1; kwd_if = $1;
test = $2; test = $2;
@ -593,9 +593,9 @@ conditional:
in {region; value} in {region; value}
} }
ifso: if_clause:
instruction { instruction {
ThenInstr $1 ClauseInstr $1
} }
| LBRACE series(instruction,RBRACE) { | LBRACE series(instruction,RBRACE) {
let first, (others, terminator, closing) = $2 in let first, (others, terminator, closing) = $2 in
@ -604,7 +604,7 @@ ifso:
lbrace = $1; lbrace = $1;
inside = (first, others), terminator; inside = (first, others), terminator;
rbrace = closing} in rbrace = closing} in
ThenBlock {value; region} ClauseBlock {value; region}
} }
test_expr: test_expr:

View File

@ -34,7 +34,7 @@ entrypoint withdraw (storage store : store; const sender : address)
operations := [Transfer (owner, balance)]; operations := [Transfer (owner, balance)];
}; };
else fail "Below target" else fail "Below target"
else fail "Too soon" else { fail "Too soon"; }
else skip else skip
end with (store, operations) end with (store, operations)