Added alternate syntax for conditionals in "then" clauses.

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

18
AST.ml
View File

@ -406,12 +406,16 @@ and conditional = {
kwd_if : kwd_if;
test : test_expr;
kwd_then : kwd_then;
ifso : instruction;
ifso : ifso;
terminator : semi option;
kwd_else : kwd_else;
ifnot : instruction
}
and ifso =
ThenInstr of instruction
| ThenBlock of (instructions * semi option) braces reg
and test_expr =
GenExpr of expr
| SetMem of set_membership reg
@ -1072,11 +1076,21 @@ and print_conditional node =
print_token kwd_if "if";
print_test_expr test;
print_token kwd_then "then";
print_instruction ifso;
print_ifso ifso;
print_terminator terminator;
print_token kwd_else "else";
print_instruction ifnot
and print_ifso = function
ThenInstr instr -> print_instruction instr
| ThenBlock {value; _} ->
let {lbrace; inside; rbrace} = value in
let instr, terminator = inside in
print_token lbrace "{";
print_instructions instr;
print_terminator terminator;
print_token rbrace "}"
and print_test_expr = function
GenExpr e -> print_expr e
| SetMem m -> print_set_membership m

View File

@ -390,12 +390,16 @@ and conditional = {
kwd_if : kwd_if;
test : test_expr;
kwd_then : kwd_then;
ifso : instruction;
ifso : ifso;
terminator : semi option;
kwd_else : kwd_else;
ifnot : instruction
}
and ifso =
ThenInstr of instruction
| ThenBlock of (instructions * semi option) braces reg
and test_expr =
GenExpr of expr
| SetMem of set_membership reg

View File

@ -580,7 +580,7 @@ proc_call:
fun_call { $1 }
conditional:
If test_expr Then instruction option(SEMI) Else instruction {
If test_expr Then ifso option(SEMI) Else instruction {
let region = cover $1 (instr_to_region $7) in
let value = {
kwd_if = $1;
@ -593,6 +593,20 @@ conditional:
in {region; value}
}
ifso:
instruction {
ThenInstr $1
}
| LBRACE series(instruction,RBRACE) {
let first, (others, terminator, closing) = $2 in
let region = cover $1 closing in
let value = {
lbrace = $1;
inside = (first, others), terminator;
rbrace = closing} in
ThenBlock {value; region}
}
test_expr:
expr { GenExpr $1 }
| set_membership { SetMem $1 }

View File

@ -28,12 +28,11 @@ entrypoint withdraw (storage store : store; const sender : address)
begin
if sender = owner then
if now (Unit) >= store.deadline then
if balance >= store.goal then
begin
if balance >= store.goal then {
store.funded := True;
// patch store with record funded = True end;
operations := [Transfer (owner, balance)]
end
operations := [Transfer (owner, balance)];
};
else fail "Below target"
else fail "Too soon"
else skip