Merge branch 'feature/pascaligo-remove-case-as-expression' into 'dev'

Feature/pascaligo add conditional as an expression

See merge request ligolang/ligo!144
This commit is contained in:
Christian Rinderknecht 2019-10-22 08:42:59 +00:00
commit 9ad0859a4c
7 changed files with 100 additions and 18 deletions

View File

@ -338,6 +338,16 @@ and record_patch = {
record_inj : record_expr
}
and cond_expr = {
kwd_if : kwd_if;
test : expr;
kwd_then : kwd_then;
ifso : expr;
terminator : semi option;
kwd_else : kwd_else;
ifnot : expr
}
and conditional = {
kwd_if : kwd_if;
test : expr;
@ -437,7 +447,8 @@ and collection =
(* Expressions *)
and expr =
| ECase of expr case reg
ECase of expr case reg
| ECond of cond_expr reg
| EAnnot of annot_expr reg
| ELogic of logic_expr
| EArith of arith_expr
@ -628,6 +639,7 @@ let rec expr_to_region = function
| EBytes {region; _}
| EUnit region
| ECase {region;_}
| ECond {region; _}
| EPar {region; _} -> region
and tuple_expr_to_region {region; _} = region

View File

@ -329,6 +329,16 @@ and record_patch = {
record_inj : field_assign reg injection reg
}
and cond_expr = {
kwd_if : kwd_if;
test : expr;
kwd_then : kwd_then;
ifso : expr;
terminator : semi option;
kwd_else : kwd_else;
ifnot : expr
}
and conditional = {
kwd_if : kwd_if;
test : expr;
@ -429,6 +439,7 @@ and collection =
and expr =
ECase of expr case reg
| ECond of cond_expr reg
| EAnnot of annot_expr reg
| ELogic of logic_expr
| EArith of arith_expr

View File

@ -518,7 +518,7 @@ proc_call:
conditional:
If expr Then if_clause option(SEMI) Else if_clause {
let region = cover $1 (if_clause_to_region $7) in
let value = {
let value : conditional = {
kwd_if = $1;
test = $2;
kwd_then = $3;
@ -661,14 +661,28 @@ interactive_expr:
expr:
case(expr) { ECase ($1 expr_to_region) }
| cond_expr { $1 }
| disj_expr { $1 }
cond_expr:
If expr Then expr option(SEMI) Else expr {
let region = cover $1 (expr_to_region $7) in
let value : cond_expr = {
kwd_if = $1;
test = $2;
kwd_then = $3;
ifso = $4;
terminator = $5;
kwd_else = $6;
ifnot = $7}
in ECond {region; value} }
disj_expr:
disj_expr Or conj_expr {
let start = expr_to_region $1
and stop = expr_to_region $3 in
let region = cover start stop
and value = {arg1 = $1; op = $2; arg2 = $3} in
and value = {arg1=$1; op=$2; arg2=$3} in
ELogic (BoolExpr (Or {region; value}))
}
| conj_expr { $1 }
@ -678,7 +692,7 @@ conj_expr:
let start = expr_to_region $1
and stop = expr_to_region $3 in
let region = cover start stop
and value = {arg1 = $1; op = $2; arg2 = $3}
and value = {arg1=$1; op=$2; arg2=$3}
in ELogic (BoolExpr (And {region; value}))
}
| set_membership { $1 }

View File

@ -266,16 +266,23 @@ and print_instruction buffer = function
| MapRemove {value; _} -> print_map_remove buffer value
| SetRemove {value; _} -> print_set_remove buffer value
and print_conditional buffer node =
let {kwd_if; test; kwd_then; ifso; terminator;
kwd_else; ifnot} = node in
print_token buffer kwd_if "if";
print_expr buffer test;
print_token buffer kwd_then "then";
print_if_clause buffer ifso;
print_terminator buffer terminator;
print_token buffer kwd_else "else";
print_if_clause buffer ifnot
and print_cond_expr buffer (node: cond_expr) =
print_token buffer node.kwd_if "if";
print_expr buffer node.test;
print_token buffer node.kwd_then "then";
print_expr buffer node.ifso;
print_terminator buffer node.terminator;
print_token buffer node.kwd_else "else";
print_expr buffer node.ifnot
and print_conditional buffer (node: conditional) =
print_token buffer node.kwd_if "if";
print_expr buffer node.test;
print_token buffer node.kwd_then "then";
print_if_clause buffer node.ifso;
print_terminator buffer node.terminator;
print_token buffer node.kwd_else "else";
print_if_clause buffer node.ifnot
and print_if_clause buffer = function
ClauseInstr instr -> print_instruction buffer instr
@ -383,6 +390,7 @@ and print_bind_to buffer = function
and print_expr buffer = function
ECase {value;_} -> print_case_expr buffer value
| ECond {value;_} -> print_cond_expr buffer value
| EAnnot {value;_} -> print_annot_expr buffer value
| ELogic e -> print_logic_expr buffer e
| EArith e -> print_arith_expr buffer e
@ -931,7 +939,22 @@ and pp_instruction buffer ~pad:(_,pc as pad) = function
pp_node buffer ~pad "SetRemove";
pp_set_remove buffer ~pad value
and pp_conditional buffer ~pad:(_,pc) cond =
and pp_cond_expr buffer ~pad:(_,pc) (cond: cond_expr) =
let () =
let _, pc as pad = mk_pad 3 0 pc in
pp_node buffer ~pad "<condition>";
pp_expr buffer ~pad:(mk_pad 1 0 pc) cond.test in
let () =
let _, pc as pad = mk_pad 3 1 pc in
pp_node buffer ~pad "<true>";
pp_expr buffer ~pad:(mk_pad 1 0 pc) cond.ifso in
let () =
let _, pc as pad = mk_pad 3 2 pc in
pp_node buffer ~pad "<false>";
pp_expr buffer ~pad:(mk_pad 1 0 pc) cond.ifnot
in ()
and pp_conditional buffer ~pad:(_,pc) (cond: conditional) =
let () =
let _, pc as pad = mk_pad 3 0 pc in
pp_node buffer ~pad "<condition>";
@ -1269,6 +1292,9 @@ and pp_expr buffer ~pad:(_,pc as pad) = function
ECase {value; _} ->
pp_node buffer ~pad "ECase";
pp_case pp_expr buffer ~pad value
| ECond {value; _} ->
pp_node buffer ~pad "ECond";
pp_cond_expr buffer ~pad value
| EAnnot {value; _} ->
pp_node buffer ~pad "EAnnot";
pp_annotated buffer ~pad value

View File

@ -379,6 +379,12 @@ let rec simpl_expression (t:Raw.expr) : expr result =
| ELogic l -> simpl_logic_expression l
| EList l -> simpl_list_expression l
| ESet s -> simpl_set_expression s
| ECond c ->
let (c , loc) = r_split c in
let%bind expr = simpl_expression c.test in
let%bind match_true = simpl_expression c.ifso in
let%bind match_false = simpl_expression c.ifnot in
return @@ e_matching expr ~loc (Match_bool {match_true; match_false})
| ECase c -> (
let (c , loc) = r_split c in
let%bind e = simpl_expression c.expr in

View File

@ -8,3 +8,9 @@ function main (const i : int) : int is
else
result := 0
end with result
function foo (const b : bool) : int is
var x : int := 41 ;
begin
x := 1 + (if b then x else main(x)) ;
end with x

View File

@ -597,9 +597,16 @@ let list () : unit result =
let condition () : unit result =
let%bind program = type_file "./contracts/condition.ligo" in
let make_input = e_int in
let make_expected = fun n -> e_int (if n = 2 then 42 else 0) in
expect_eq_n program "main" make_input make_expected
let%bind _ =
let make_input = e_int in
let make_expected = fun n -> e_int (if n = 2 then 42 else 0) in
expect_eq_n program "main" make_input make_expected
in
let%bind _ =
let make_expected = fun b -> e_int (if b then 42 else 1) in
expect_eq_b program "foo" make_expected
in
ok ()
let condition_simple () : unit result =
let%bind program = type_file "./contracts/condition-simple.ligo" in