Extended conditional syntax for "else" clauses.
New syntax: if ... then ... else { ... }
This commit is contained in:
parent
aced77e085
commit
98f9d3e417
24
AST.ml
24
AST.ml
@ -406,15 +406,15 @@ and conditional = {
|
||||
kwd_if : kwd_if;
|
||||
test : test_expr;
|
||||
kwd_then : kwd_then;
|
||||
ifso : ifso;
|
||||
ifso : if_clause;
|
||||
terminator : semi option;
|
||||
kwd_else : kwd_else;
|
||||
ifnot : instruction
|
||||
ifnot : if_clause
|
||||
}
|
||||
|
||||
and ifso =
|
||||
ThenInstr of instruction
|
||||
| ThenBlock of (instructions * semi option) braces reg
|
||||
and if_clause =
|
||||
ClauseInstr of instruction
|
||||
| ClauseBlock of (instructions * semi option) braces reg
|
||||
|
||||
and test_expr =
|
||||
GenExpr of expr
|
||||
@ -759,6 +759,10 @@ let instr_to_region = function
|
||||
| Single SetRemove {region; _}
|
||||
| Block {region; _} -> region
|
||||
|
||||
let if_clause_to_region = function
|
||||
ClauseInstr instr -> instr_to_region instr
|
||||
| ClauseBlock {region; _} -> region
|
||||
|
||||
let pattern_to_region = function
|
||||
PCons {region; _}
|
||||
| PVar {region; _}
|
||||
@ -1076,14 +1080,14 @@ and print_conditional node =
|
||||
print_token kwd_if "if";
|
||||
print_test_expr test;
|
||||
print_token kwd_then "then";
|
||||
print_ifso ifso;
|
||||
print_if_clause ifso;
|
||||
print_terminator terminator;
|
||||
print_token kwd_else "else";
|
||||
print_instruction ifnot
|
||||
print_if_clause ifnot
|
||||
|
||||
and print_ifso = function
|
||||
ThenInstr instr -> print_instruction instr
|
||||
| ThenBlock {value; _} ->
|
||||
and print_if_clause = function
|
||||
ClauseInstr instr -> print_instruction instr
|
||||
| ClauseBlock {value; _} ->
|
||||
let {lbrace; inside; rbrace} = value in
|
||||
let instr, terminator = inside in
|
||||
print_token lbrace "{";
|
||||
|
11
AST.mli
11
AST.mli
@ -390,15 +390,15 @@ and conditional = {
|
||||
kwd_if : kwd_if;
|
||||
test : test_expr;
|
||||
kwd_then : kwd_then;
|
||||
ifso : ifso;
|
||||
ifso : if_clause;
|
||||
terminator : semi option;
|
||||
kwd_else : kwd_else;
|
||||
ifnot : instruction
|
||||
ifnot : if_clause
|
||||
}
|
||||
|
||||
and ifso =
|
||||
ThenInstr of instruction
|
||||
| ThenBlock of (instructions * semi option) braces reg
|
||||
and if_clause =
|
||||
ClauseInstr of instruction
|
||||
| ClauseBlock of (instructions * semi option) braces reg
|
||||
|
||||
and test_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 lhs_to_region : lhs -> Region.t
|
||||
val rhs_to_region : rhs -> Region.t
|
||||
val if_clause_to_region : if_clause -> Region.t
|
||||
|
||||
(* Printing *)
|
||||
|
||||
|
10
Parser.mly
10
Parser.mly
@ -580,8 +580,8 @@ proc_call:
|
||||
fun_call { $1 }
|
||||
|
||||
conditional:
|
||||
If test_expr Then ifso option(SEMI) Else instruction {
|
||||
let region = cover $1 (instr_to_region $7) in
|
||||
If test_expr Then if_clause option(SEMI) Else if_clause {
|
||||
let region = cover $1 (if_clause_to_region $7) in
|
||||
let value = {
|
||||
kwd_if = $1;
|
||||
test = $2;
|
||||
@ -593,9 +593,9 @@ conditional:
|
||||
in {region; value}
|
||||
}
|
||||
|
||||
ifso:
|
||||
if_clause:
|
||||
instruction {
|
||||
ThenInstr $1
|
||||
ClauseInstr $1
|
||||
}
|
||||
| LBRACE series(instruction,RBRACE) {
|
||||
let first, (others, terminator, closing) = $2 in
|
||||
@ -604,7 +604,7 @@ ifso:
|
||||
lbrace = $1;
|
||||
inside = (first, others), terminator;
|
||||
rbrace = closing} in
|
||||
ThenBlock {value; region}
|
||||
ClauseBlock {value; region}
|
||||
}
|
||||
|
||||
test_expr:
|
||||
|
@ -34,7 +34,7 @@ entrypoint withdraw (storage store : store; const sender : address)
|
||||
operations := [Transfer (owner, balance)];
|
||||
};
|
||||
else fail "Below target"
|
||||
else fail "Too soon"
|
||||
else { fail "Too soon"; }
|
||||
else skip
|
||||
end with (store, operations)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user