Proto: script translator code indentation cleanup.

This commit is contained in:
Benjamin Canou 2016-11-10 14:32:30 +01:00
parent 472258b1bf
commit 0f91192769

View File

@ -707,25 +707,31 @@ and parse_instr
let return : bef judgement -> bef judgement tzresult Lwt.t = return in let return : bef judgement -> bef judgement tzresult Lwt.t = return in
let check_item_ty got exp pos n = let check_item_ty got exp pos n =
ty_eq got exp |> record_trace (Bad_stack_item (pos, n)) |> Lwt.return in ty_eq got exp |> record_trace (Bad_stack_item (pos, n)) |> Lwt.return in
(* TODO: macros *)
match script_instr, stack_ty with match script_instr, stack_ty with
(* stack ops *) (* stack ops *)
| Prim (_, "drop", []), Item_t (_, rest) -> | Prim (_, "drop", []),
Item_t (_, rest) ->
return (Typed (Drop, rest)) return (Typed (Drop, rest))
| Prim (_, "dup", []), Item_t (v, rest) -> | Prim (_, "dup", []),
Item_t (v, rest) ->
return (Typed (Dup, Item_t (v, Item_t (v, rest)))) return (Typed (Dup, Item_t (v, Item_t (v, rest))))
| Prim (_, "swap", []), Item_t (v, Item_t (w, rest)) -> | Prim (_, "swap", []),
Item_t (v, Item_t (w, rest)) ->
return (Typed (Swap, Item_t (w, Item_t (v, rest)))) return (Typed (Swap, Item_t (w, Item_t (v, rest))))
| Prim (_, "push", [ td ]), rest -> | Prim (_, "push", [ td ]),
stack ->
parse_tagged_data ctxt td >>=? fun (Ex (t, v)) -> parse_tagged_data ctxt td >>=? fun (Ex (t, v)) ->
return (Typed (Const v, Item_t (t, rest))) return (Typed (Const v, Item_t (t, stack)))
(* options *) (* options *)
| Prim (_, "some", []), Item_t (t, rest) -> | Prim (_, "some", []),
Item_t (t, rest) ->
return (Typed (Cons_some, Item_t (Option_t t, rest))) return (Typed (Cons_some, Item_t (Option_t t, rest)))
| Prim (_, "none", [ t ]), rest -> | Prim (_, "none", [ t ]),
stack ->
parse_ty t >>=? fun (Ex t) -> parse_ty t >>=? fun (Ex t) ->
return (Typed (Cons_none t, Item_t (Option_t t, rest))) return (Typed (Cons_none t, Item_t (Option_t t, stack)))
| Prim (loc, "if_none", [ bt ; bf ]), Item_t (Option_t t, rest) -> | Prim (loc, "if_none", [ bt ; bf ]),
Item_t (Option_t t, rest) ->
expect_sequence_parameter loc Instr "if_none" 0 bt >>=? fun () -> expect_sequence_parameter loc Instr "if_none" 0 bt >>=? fun () ->
expect_sequence_parameter loc Instr "if_none" 1 bf >>=? fun () -> expect_sequence_parameter loc Instr "if_none" 1 bf >>=? fun () ->
parse_instr ?storage_type ctxt bt rest >>=? fun btr -> parse_instr ?storage_type ctxt bt rest >>=? fun btr ->
@ -733,20 +739,26 @@ and parse_instr
let branch ibt ibf = If_none (ibt, ibf) in let branch ibt ibf = If_none (ibt, ibf) in
merge_branches loc btr bfr { branch } merge_branches loc btr bfr { branch }
(* pairs *) (* pairs *)
| Prim (_, "pair", []), Item_t (a, Item_t (b, rest)) -> | Prim (_, "pair", []),
Item_t (a, Item_t (b, rest)) ->
return (Typed (Cons_pair, Item_t (Pair_t(a, b), rest))) return (Typed (Cons_pair, Item_t (Pair_t(a, b), rest)))
| Prim (_, "car", []), Item_t (Pair_t (a, _), rest) -> | Prim (_, "car", []),
Item_t (Pair_t (a, _), rest) ->
return (Typed (Car, Item_t (a, rest))) return (Typed (Car, Item_t (a, rest)))
| Prim (_, "cdr", []), Item_t (Pair_t (_, b), rest) -> | Prim (_, "cdr", []),
Item_t (Pair_t (_, b), rest) ->
return (Typed (Cdr, Item_t (b, rest))) return (Typed (Cdr, Item_t (b, rest)))
(* unions *) (* unions *)
| Prim (_, "left", [ tr ]), Item_t (tl, rest) -> | Prim (_, "left", [ tr ]),
Item_t (tl, rest) ->
parse_ty tr >>=? fun (Ex tr) -> parse_ty tr >>=? fun (Ex tr) ->
return (Typed (Left, Item_t (Union_t (tl, tr), rest))) return (Typed (Left, Item_t (Union_t (tl, tr), rest)))
| Prim (_, "right", [ tl ]), Item_t (tr, rest) -> | Prim (_, "right", [ tl ]),
Item_t (tr, rest) ->
parse_ty tl >>=? fun (Ex tl) -> parse_ty tl >>=? fun (Ex tl) ->
return (Typed (Right, Item_t (Union_t (tl, tr), rest))) return (Typed (Right, Item_t (Union_t (tl, tr), rest)))
| Prim (loc, "if_left", [ bt ; bf ]), Item_t (Union_t (tl, tr), rest) -> | Prim (loc, "if_left", [ bt ; bf ]),
Item_t (Union_t (tl, tr), rest) ->
expect_sequence_parameter loc Instr "if_left" 0 bt >>=? fun () -> expect_sequence_parameter loc Instr "if_left" 0 bt >>=? fun () ->
expect_sequence_parameter loc Instr "if_left" 1 bf >>=? fun () -> expect_sequence_parameter loc Instr "if_left" 1 bf >>=? fun () ->
parse_instr ?storage_type ctxt bt (Item_t (tl, rest)) >>=? fun btr -> parse_instr ?storage_type ctxt bt (Item_t (tl, rest)) >>=? fun btr ->
@ -754,77 +766,93 @@ and parse_instr
let branch ibt ibf = If_left (ibt, ibf) in let branch ibt ibf = If_left (ibt, ibf) in
merge_branches loc btr bfr { branch } merge_branches loc btr bfr { branch }
(* lists *) (* lists *)
| Prim (_, "nil", [ t ]), rest -> | Prim (_, "nil", [ t ]),
stack ->
parse_ty t >>=? fun (Ex t) -> parse_ty t >>=? fun (Ex t) ->
return (Typed (Nil, Item_t (List_t t, rest))) return (Typed (Nil, Item_t (List_t t, stack)))
| Prim (loc, "cons", []), Item_t (tv, Item_t (List_t t, rest)) -> | Prim (loc, "cons", []),
Item_t (tv, Item_t (List_t t, rest)) ->
trace trace
(Bad_stack_item (loc, 2)) (Bad_stack_item (loc, 2))
(Lwt.return (ty_eq t tv)) >>=? fun (Eq _) -> (Lwt.return (ty_eq t tv)) >>=? fun (Eq _) ->
return (Typed (Cons_list, Item_t (List_t t, rest))) return (Typed (Cons_list, Item_t (List_t t, rest)))
| Prim (loc, "if_cons", [ bt ; bf ]), Item_t (List_t t, rest) -> | Prim (loc, "if_cons", [ bt ; bf ]),
Item_t (List_t t, rest) ->
expect_sequence_parameter loc Instr "if_cons" 0 bt >>=? fun () -> expect_sequence_parameter loc Instr "if_cons" 0 bt >>=? fun () ->
expect_sequence_parameter loc Instr "if_cons" 1 bf >>=? fun () -> expect_sequence_parameter loc Instr "if_cons" 1 bf >>=? fun () ->
parse_instr ?storage_type ctxt bt (Item_t (t, Item_t (List_t t, rest))) >>=? fun btr -> parse_instr ?storage_type ctxt bt (Item_t (t, Item_t (List_t t, rest))) >>=? fun btr ->
parse_instr ?storage_type ctxt bf rest >>=? fun bfr -> parse_instr ?storage_type ctxt bf rest >>=? fun bfr ->
let branch ibt ibf = If_cons (ibt, ibf) in let branch ibt ibf = If_cons (ibt, ibf) in
merge_branches loc btr bfr { branch } merge_branches loc btr bfr { branch }
| Prim (loc, "iter", []), Item_t (Lambda_t (param, Void_t), Item_t (List_t elt, rest)) -> | Prim (loc, "iter", []),
Item_t (Lambda_t (param, Void_t), Item_t (List_t elt, rest)) ->
check_item_ty elt param loc 2 >>=? fun (Eq _) -> check_item_ty elt param loc 2 >>=? fun (Eq _) ->
return (Typed (List_iter, rest)) return (Typed (List_iter, rest))
| Prim (loc, "map", []), Item_t (Lambda_t (param, ret), Item_t (List_t elt, rest)) -> | Prim (loc, "map", []),
Item_t (Lambda_t (param, ret), Item_t (List_t elt, rest)) ->
check_item_ty elt param loc 2 >>=? fun (Eq _) -> check_item_ty elt param loc 2 >>=? fun (Eq _) ->
return (Typed (List_map, Item_t (List_t ret, rest))) return (Typed (List_map, Item_t (List_t ret, rest)))
| Prim (loc, "reduce", []), Item_t (Lambda_t (Pair_t (pelt, pr), r), | Prim (loc, "reduce", []),
Item_t (Lambda_t (Pair_t (pelt, pr), r),
Item_t (List_t elt, Item_t (init, rest))) -> Item_t (List_t elt, Item_t (init, rest))) ->
check_item_ty r pr loc 1 >>=? fun (Eq _) -> check_item_ty r pr loc 1 >>=? fun (Eq _) ->
check_item_ty elt pelt loc 2 >>=? fun (Eq _) -> check_item_ty elt pelt loc 2 >>=? fun (Eq _) ->
check_item_ty init r loc 3 >>=? fun (Eq _) -> check_item_ty init r loc 3 >>=? fun (Eq _) ->
return (Typed (List_reduce, Item_t (r, rest))) return (Typed (List_reduce, Item_t (r, rest)))
(* sets *) (* sets *)
| Prim (_, "empty_set", [ t ]), rest -> | Prim (_, "empty_set", [ t ]),
rest ->
parse_comparable_ty t >>=? fun (Ex t) -> parse_comparable_ty t >>=? fun (Ex t) ->
return (Typed (Empty_set t, Item_t (Set_t t, rest))) return (Typed (Empty_set t, Item_t (Set_t t, rest)))
| Prim (loc, "iter", []), Item_t (Lambda_t (param, Void_t), Item_t (Set_t elt, rest)) -> | Prim (loc, "iter", []),
Item_t (Lambda_t (param, Void_t), Item_t (Set_t elt, rest)) ->
let elt = ty_of_comparable_ty elt in let elt = ty_of_comparable_ty elt in
check_item_ty elt param loc 2 >>=? fun (Eq _) -> check_item_ty elt param loc 2 >>=? fun (Eq _) ->
return (Typed (Set_iter, rest)) return (Typed (Set_iter, rest))
| Prim (loc, "map", []), Item_t (Lambda_t (param, ret), Item_t (Set_t elt, rest)) -> | Prim (loc, "map", []),
Item_t (Lambda_t (param, ret), Item_t (Set_t elt, rest)) ->
let elt = ty_of_comparable_ty elt in let elt = ty_of_comparable_ty elt in
trace (Bad_stack_item (loc, 1)) (Lwt.return (comparable_ty_of_ty ret)) >>=? fun ret -> trace (Bad_stack_item (loc, 1)) (Lwt.return (comparable_ty_of_ty ret)) >>=? fun ret ->
check_item_ty elt param loc 2 >>=? fun (Eq _) -> check_item_ty elt param loc 2 >>=? fun (Eq _) ->
return (Typed (Set_map ret, Item_t (Set_t ret, rest))) return (Typed (Set_map ret, Item_t (Set_t ret, rest)))
| Prim (loc, "reduce", []), Item_t (Lambda_t (Pair_t (pelt, pr), r), | Prim (loc, "reduce", []),
Item_t (Lambda_t (Pair_t (pelt, pr), r),
Item_t (Set_t elt, Item_t (init, rest))) -> Item_t (Set_t elt, Item_t (init, rest))) ->
let elt = ty_of_comparable_ty elt in let elt = ty_of_comparable_ty elt in
check_item_ty r pr loc 1 >>=? fun (Eq _) -> check_item_ty r pr loc 1 >>=? fun (Eq _) ->
check_item_ty elt pelt loc 2 >>=? fun (Eq _) -> check_item_ty elt pelt loc 2 >>=? fun (Eq _) ->
check_item_ty init r loc 3 >>=? fun (Eq _) -> check_item_ty init r loc 3 >>=? fun (Eq _) ->
return (Typed (Set_reduce, Item_t (r, rest))) return (Typed (Set_reduce, Item_t (r, rest)))
| Prim (loc, "mem", []), Item_t (v, Item_t (Set_t elt, rest)) -> | Prim (loc, "mem", []),
Item_t (v, Item_t (Set_t elt, rest)) ->
let elt = ty_of_comparable_ty elt in let elt = ty_of_comparable_ty elt in
check_item_ty elt v loc 2 >>=? fun (Eq _) -> check_item_ty elt v loc 2 >>=? fun (Eq _) ->
return (Typed (Set_mem, Item_t (Bool_t, rest))) return (Typed (Set_mem, Item_t (Bool_t, rest)))
| Prim (loc, "update", []), Item_t (v, Item_t (Bool_t, Item_t (Set_t elt, rest))) -> | Prim (loc, "update", []),
Item_t (v, Item_t (Bool_t, Item_t (Set_t elt, rest))) ->
let elt = ty_of_comparable_ty elt in let elt = ty_of_comparable_ty elt in
check_item_ty elt v loc 3 >>=? fun (Eq _) -> check_item_ty elt v loc 3 >>=? fun (Eq _) ->
return (Typed (Set_update, rest)) return (Typed (Set_update, rest))
(* maps *) (* maps *)
| Prim (_, "empty_map", [ tk ; tv ]), rest -> | Prim (_, "empty_map", [ tk ; tv ]),
stack ->
parse_comparable_ty tk >>=? fun (Ex tk) -> parse_comparable_ty tk >>=? fun (Ex tk) ->
parse_ty tv >>=? fun (Ex tv) -> parse_ty tv >>=? fun (Ex tv) ->
return (Typed (Empty_map (tk, tv), Item_t (Map_t (tk, tv), rest))) return (Typed (Empty_map (tk, tv), Item_t (Map_t (tk, tv), stack)))
| Prim (loc, "iter", []), Item_t (Lambda_t (Pair_t (pk, pv), Void_t), Item_t (Map_t (k, v), rest)) -> | Prim (loc, "iter", []),
Item_t (Lambda_t (Pair_t (pk, pv), Void_t), Item_t (Map_t (k, v), rest)) ->
let k = ty_of_comparable_ty k in let k = ty_of_comparable_ty k in
check_item_ty pk k loc 2 >>=? fun (Eq _) -> check_item_ty pk k loc 2 >>=? fun (Eq _) ->
check_item_ty pv v loc 2 >>=? fun (Eq _) -> check_item_ty pv v loc 2 >>=? fun (Eq _) ->
return (Typed (Map_iter, rest)) return (Typed (Map_iter, rest))
| Prim (loc, "map", []), Item_t (Lambda_t (Pair_t (pk, pv), ret), Item_t (Map_t (ck, v), rest)) -> | Prim (loc, "map", []),
Item_t (Lambda_t (Pair_t (pk, pv), ret), Item_t (Map_t (ck, v), rest)) ->
let k = ty_of_comparable_ty ck in let k = ty_of_comparable_ty ck in
check_item_ty pk k loc 2 >>=? fun (Eq _) -> check_item_ty pk k loc 2 >>=? fun (Eq _) ->
check_item_ty pv v loc 2 >>=? fun (Eq _) -> check_item_ty pv v loc 2 >>=? fun (Eq _) ->
return (Typed (Map_map, Item_t (Map_t (ck, ret), rest))) return (Typed (Map_map, Item_t (Map_t (ck, ret), rest)))
| Prim (loc, "reduce", []), Item_t (Lambda_t (Pair_t (Pair_t (pk, pv), pr), r), | Prim (loc, "reduce", []),
Item_t (Lambda_t (Pair_t (Pair_t (pk, pv), pr), r),
Item_t (Map_t (ck, v), Item_t (init, rest))) -> Item_t (Map_t (ck, v), Item_t (init, rest))) ->
let k = ty_of_comparable_ty ck in let k = ty_of_comparable_ty ck in
check_item_ty pk k loc 2 >>=? fun (Eq _) -> check_item_ty pk k loc 2 >>=? fun (Eq _) ->
@ -832,34 +860,43 @@ and parse_instr
check_item_ty r pr loc 1 >>=? fun (Eq _) -> check_item_ty r pr loc 1 >>=? fun (Eq _) ->
check_item_ty init r loc 3 >>=? fun (Eq _) -> check_item_ty init r loc 3 >>=? fun (Eq _) ->
return (Typed (Map_reduce, Item_t (r, rest))) return (Typed (Map_reduce, Item_t (r, rest)))
| Prim (loc, "mem", []), Item_t (vk, Item_t (Map_t (ck, _), rest)) -> | Prim (loc, "mem", []),
Item_t (vk, Item_t (Map_t (ck, _), rest)) ->
let k = ty_of_comparable_ty ck in let k = ty_of_comparable_ty ck in
check_item_ty vk k loc 1 >>=? fun (Eq _) -> check_item_ty vk k loc 1 >>=? fun (Eq _) ->
return (Typed (Map_mem, Item_t (Bool_t, rest))) return (Typed (Map_mem, Item_t (Bool_t, rest)))
| Prim (loc, "get", []), Item_t (vk, Item_t (Map_t (ck, elt), rest)) -> | Prim (loc, "get", []),
Item_t (vk, Item_t (Map_t (ck, elt), rest)) ->
let k = ty_of_comparable_ty ck in let k = ty_of_comparable_ty ck in
check_item_ty vk k loc 1 >>=? fun (Eq _) -> check_item_ty vk k loc 1 >>=? fun (Eq _) ->
return (Typed (Map_get, Item_t (Option_t elt, rest))) return (Typed (Map_get, Item_t (Option_t elt, rest)))
| Prim (loc, "update", []), Item_t (vk, Item_t (Option_t vv, Item_t (Map_t (ck, v), rest))) -> | Prim (loc, "update", []),
Item_t (vk, Item_t (Option_t vv, Item_t (Map_t (ck, v), rest))) ->
let k = ty_of_comparable_ty ck in let k = ty_of_comparable_ty ck in
check_item_ty vk k loc 1 >>=? fun (Eq _) -> check_item_ty vk k loc 1 >>=? fun (Eq _) ->
check_item_ty vv v loc 2 >>=? fun (Eq _) -> check_item_ty vv v loc 2 >>=? fun (Eq _) ->
return (Typed (Map_update, rest)) return (Typed (Map_update, rest))
(* reference cells *) (* reference cells *)
| Prim (_, "ref", []), Item_t (t, rest) -> | Prim (_, "ref", []),
Item_t (t, rest) ->
return (Typed (Ref, Item_t (Ref_t t, rest))) return (Typed (Ref, Item_t (Ref_t t, rest)))
| Prim (_, "deref", []), Item_t (Ref_t t, rest) -> | Prim (_, "deref", []),
Item_t (Ref_t t, rest) ->
return (Typed (Deref, Item_t (t, rest))) return (Typed (Deref, Item_t (t, rest)))
| Prim (loc, "set", []), Item_t (Ref_t t, Item_t (tv, rest)) -> | Prim (loc, "set", []),
Item_t (Ref_t t, Item_t (tv, rest)) ->
check_item_ty tv t loc 2 >>=? fun (Eq _) -> check_item_ty tv t loc 2 >>=? fun (Eq _) ->
return (Typed (Set, rest)) return (Typed (Set, rest))
(* control *) (* control *)
| Seq (_, []), rest -> | Seq (_, []),
return (Typed (Nop, rest)) stack ->
| Seq (_, [ single ]), stack_ty -> return (Typed (Nop, stack))
parse_instr ?storage_type ctxt single stack_ty | Seq (_, [ single ]),
| Seq (loc, hd :: tl), stack_ty -> stack ->
parse_instr ?storage_type ctxt hd stack_ty >>=? begin function parse_instr ?storage_type ctxt single stack
| Seq (loc, hd :: tl),
stack ->
parse_instr ?storage_type ctxt hd stack >>=? begin function
| Failed _ -> | Failed _ ->
fail (Fail_not_in_tail_position loc) fail (Fail_not_in_tail_position loc)
| Typed (ihd, trans) -> | Typed (ihd, trans) ->
@ -870,35 +907,40 @@ and parse_instr
| Typed (itl, aft) -> | Typed (itl, aft) ->
return (Typed (Seq (ihd, itl), aft)) return (Typed (Seq (ihd, itl), aft))
end end
| Prim (loc, "if", [ bt ; bf ]), Item_t (Bool_t, rest) -> | Prim (loc, "if", [ bt ; bf ]),
Item_t (Bool_t, rest) ->
expect_sequence_parameter loc Instr "if" 0 bt >>=? fun () -> expect_sequence_parameter loc Instr "if" 0 bt >>=? fun () ->
expect_sequence_parameter loc Instr "if" 1 bf >>=? fun () -> expect_sequence_parameter loc Instr "if" 1 bf >>=? fun () ->
parse_instr ?storage_type ctxt bt rest >>=? fun btr -> parse_instr ?storage_type ctxt bt rest >>=? fun btr ->
parse_instr ?storage_type ctxt bf rest >>=? fun bfr -> parse_instr ?storage_type ctxt bf rest >>=? fun bfr ->
let branch ibt ibf = If (ibt, ibf) in let branch ibt ibf = If (ibt, ibf) in
merge_branches loc btr bfr { branch } merge_branches loc btr bfr { branch }
| Prim (loc, "loop", [ body ]), Item_t (Bool_t, rest) -> | Prim (loc, "loop", [ body ]),
(Item_t (Bool_t, rest) as stack) ->
expect_sequence_parameter loc Instr "loop" 0 body >>=? fun () -> expect_sequence_parameter loc Instr "loop" 0 body >>=? fun () ->
parse_instr ?storage_type ctxt body rest >>=? begin function parse_instr ?storage_type ctxt body rest >>=? begin function
| Typed (ibody, aftbody) -> | Typed (ibody, aftbody) ->
trace trace
(Unmatched_branches (loc, Stack_ty aftbody, Stack_ty stack_ty)) (Unmatched_branches (loc, Stack_ty aftbody, Stack_ty stack))
(Lwt.return (stack_ty_eq 0 aftbody stack_ty)) >>=? fun (Eq _) -> (Lwt.return (stack_ty_eq 0 aftbody stack)) >>=? fun (Eq _) ->
return (Typed (Loop ibody, rest)) return (Typed (Loop ibody, rest))
| Failed { instr } -> | Failed { instr } ->
let ibody = instr (Item_t (Bool_t, rest)) in let ibody = instr (Item_t (Bool_t, rest)) in
return (Typed (Loop ibody, rest)) return (Typed (Loop ibody, rest))
end end
| Prim (loc, "lambda", [ arg ; ret ; code ]), rest -> | Prim (loc, "lambda", [ arg ; ret ; code ]),
stack ->
parse_ty arg >>=? fun (Ex arg) -> parse_ty arg >>=? fun (Ex arg) ->
parse_ty ret >>=? fun (Ex ret) -> parse_ty ret >>=? fun (Ex ret) ->
expect_sequence_parameter loc Instr "lambda" 2 code >>=? fun () -> expect_sequence_parameter loc Instr "lambda" 2 code >>=? fun () ->
parse_lambda ctxt arg ret code >>=? fun (lambda) -> parse_lambda ctxt arg ret code >>=? fun lambda ->
return (Typed (Lambda lambda, Item_t (Lambda_t (arg, ret), rest))) return (Typed (Lambda lambda, Item_t (Lambda_t (arg, ret), stack)))
| Prim (loc, "exec", []), Item_t (arg, Item_t (Lambda_t (param, ret), rest)) -> | Prim (loc, "exec", []),
Item_t (arg, Item_t (Lambda_t (param, ret), rest)) ->
check_item_ty arg param loc 1 >>=? fun (Eq _) -> check_item_ty arg param loc 1 >>=? fun (Eq _) ->
return (Typed (Exec, Item_t (ret, rest))) return (Typed (Exec, Item_t (ret, rest)))
| Prim (loc, "dip", [ code ]), Item_t (v, rest) -> | Prim (loc, "dip", [ code ]),
Item_t (v, rest) ->
expect_sequence_parameter loc Instr "dip" 0 code >>=? fun () -> expect_sequence_parameter loc Instr "dip" 0 code >>=? fun () ->
parse_instr ctxt code rest >>=? begin function parse_instr ctxt code rest >>=? begin function
| Typed (instr, aft_rest) -> | Typed (instr, aft_rest) ->
@ -906,154 +948,204 @@ and parse_instr
| Failed _ -> | Failed _ ->
fail (Fail_not_in_tail_position loc) fail (Fail_not_in_tail_position loc)
end end
| Prim (loc, "fail", []), _ -> | Prim (loc, "fail", []),
_ ->
let instr _ = Fail loc in let instr _ = Fail loc in
return (Failed { instr }) return (Failed { instr })
| Prim (_, "nop", []), rest -> | Prim (_, "nop", []),
return (Typed (Nop, rest)) stack ->
return (Typed (Nop, stack))
(* timestamp operations *) (* timestamp operations *)
| Prim (loc, "add", []), Item_t (Timestamp_t, Item_t (Int_t kind, rest)) -> | Prim (loc, "add", []),
Item_t (Timestamp_t, Item_t (Int_t kind, rest)) ->
trace (Bad_stack_item (loc, 2)) (Lwt.return (unsigned_int_kind kind)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 2)) (Lwt.return (unsigned_int_kind kind)) >>=? fun (Eq _) ->
return (Typed (Add_timestamp_to_seconds (kind, loc), Item_t (Timestamp_t, rest))) return (Typed (Add_timestamp_to_seconds (kind, loc), Item_t (Timestamp_t, rest)))
| Prim (loc, "add", []), Item_t (Int_t kind, Item_t (Timestamp_t, rest)) -> | Prim (loc, "add", []),
Item_t (Int_t kind, Item_t (Timestamp_t, rest)) ->
trace trace
(Bad_stack_item (loc, 1)) (Bad_stack_item (loc, 1))
(Lwt.return (unsigned_int_kind kind)) >>=? fun (Eq _) -> (Lwt.return (unsigned_int_kind kind)) >>=? fun (Eq _) ->
return (Typed (Add_seconds_to_timestamp (kind, loc), Item_t (Timestamp_t, rest))) return (Typed (Add_seconds_to_timestamp (kind, loc), Item_t (Timestamp_t, rest)))
(* string operations *) (* string operations *)
| Prim (_, "concat", []), Item_t (String_t, Item_t (String_t, rest)) -> | Prim (_, "concat", []),
Item_t (String_t, Item_t (String_t, rest)) ->
return (Typed (Concat, Item_t (String_t, rest))) return (Typed (Concat, Item_t (String_t, rest)))
(* currency operations *) (* currency operations *)
| Prim (_, "add", []), Item_t (Tez_t, Item_t (Tez_t, rest)) -> | Prim (_, "add", []),
Item_t (Tez_t, Item_t (Tez_t, rest)) ->
return (Typed (Add_tez, Item_t (Tez_t, rest))) return (Typed (Add_tez, Item_t (Tez_t, rest)))
| Prim (_, "sub", []), Item_t (Tez_t, Item_t (Tez_t, rest)) -> | Prim (_, "sub", []),
Item_t (Tez_t, Item_t (Tez_t, rest)) ->
return (Typed (Sub_tez, Item_t (Tez_t, rest))) return (Typed (Sub_tez, Item_t (Tez_t, rest)))
| Prim (loc, "mul", []), Item_t (Tez_t, Item_t (Int_t kind, rest)) -> | Prim (loc, "mul", []),
Item_t (Tez_t, Item_t (Int_t kind, rest)) ->
trace (Bad_stack_item (loc, 2)) (Lwt.return (unsigned_int_kind kind)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 2)) (Lwt.return (unsigned_int_kind kind)) >>=? fun (Eq _) ->
return (Typed (Mul_tez kind, Item_t (Tez_t, rest))) return (Typed (Mul_tez kind, Item_t (Tez_t, rest)))
| Prim (loc, "mul", []), Item_t (Int_t kind, Item_t (Tez_t, rest)) -> | Prim (loc, "mul", []),
Item_t (Int_t kind, Item_t (Tez_t, rest)) ->
trace trace
(Bad_stack_item (loc, 1)) (Bad_stack_item (loc, 1))
(Lwt.return (unsigned_int_kind kind)) >>=? fun (Eq _) -> (Lwt.return (unsigned_int_kind kind)) >>=? fun (Eq _) ->
return (Typed (Mul_tez' kind, Item_t (Tez_t, rest))) return (Typed (Mul_tez' kind, Item_t (Tez_t, rest)))
(* boolean operations *) (* boolean operations *)
| Prim (_, "or", []), Item_t (Bool_t, Item_t (Bool_t, rest)) -> | Prim (_, "or", []),
Item_t (Bool_t, Item_t (Bool_t, rest)) ->
return (Typed (Or, Item_t (Bool_t, rest))) return (Typed (Or, Item_t (Bool_t, rest)))
| Prim (_, "and", []), Item_t (Bool_t, Item_t (Bool_t, rest)) -> | Prim (_, "and", []),
Item_t (Bool_t, Item_t (Bool_t, rest)) ->
return (Typed (And, Item_t (Bool_t, rest))) return (Typed (And, Item_t (Bool_t, rest)))
| Prim (_, "xor", []), Item_t (Bool_t, Item_t (Bool_t, rest)) -> | Prim (_, "xor", []),
Item_t (Bool_t, Item_t (Bool_t, rest)) ->
return (Typed (Xor, Item_t (Bool_t, rest))) return (Typed (Xor, Item_t (Bool_t, rest)))
| Prim (_, "not", []), Item_t (Bool_t, rest) -> | Prim (_, "not", []),
Item_t (Bool_t, rest) ->
return (Typed (Not, Item_t (Bool_t, rest))) return (Typed (Not, Item_t (Bool_t, rest)))
(* integer operations *) (* integer operations *)
| Prim (loc, "checked_abs", []), Item_t (Int_t k, rest) -> | Prim (loc, "checked_abs", []),
Item_t (Int_t k, rest) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (signed_int_kind k)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (signed_int_kind k)) >>=? fun (Eq _) ->
return (Typed (Checked_abs_int (k, loc), Item_t (Int_t k, rest))) return (Typed (Checked_abs_int (k, loc), Item_t (Int_t k, rest)))
| Prim (loc, "checked_neg", []), Item_t (Int_t k, rest) -> | Prim (loc, "checked_neg", []),
Item_t (Int_t k, rest) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (signed_int_kind k)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (signed_int_kind k)) >>=? fun (Eq _) ->
return (Typed (Checked_neg_int (k, loc), Item_t (Int_t k, rest))) return (Typed (Checked_neg_int (k, loc), Item_t (Int_t k, rest)))
| Prim (loc, "checked_add", []), Item_t (Int_t kl, Item_t (Int_t kr, rest)) -> | Prim (loc, "checked_add", []),
Item_t (Int_t kl, Item_t (Int_t kr, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) ->
return (Typed (Checked_add_int (kl, loc), Item_t (Int_t kl, rest))) return (Typed (Checked_add_int (kl, loc), Item_t (Int_t kl, rest)))
| Prim (loc, "checked_sub", []), Item_t (Int_t kl, Item_t (Int_t kr, rest)) -> | Prim (loc, "checked_sub", []),
Item_t (Int_t kl, Item_t (Int_t kr, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) ->
return (Typed (Checked_sub_int (kl, loc), Item_t (Int_t kl, rest))) return (Typed (Checked_sub_int (kl, loc), Item_t (Int_t kl, rest)))
| Prim (loc, "checked_mul", []), Item_t (Int_t kl, Item_t (Int_t kr, rest)) -> | Prim (loc, "checked_mul", []),
Item_t (Int_t kl, Item_t (Int_t kr, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) ->
return (Typed (Checked_mul_int (kl, loc), Item_t (Int_t kl, rest))) return (Typed (Checked_mul_int (kl, loc), Item_t (Int_t kl, rest)))
| Prim (loc, "abs", []), Item_t (Int_t k, rest) -> | Prim (loc, "abs", []),
Item_t (Int_t k, rest) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (signed_int_kind k)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (signed_int_kind k)) >>=? fun (Eq _) ->
return (Typed (Abs_int k, Item_t (Int_t k, rest))) return (Typed (Abs_int k, Item_t (Int_t k, rest)))
| Prim (loc, "neg", []), Item_t (Int_t k, rest) -> | Prim (loc, "neg", []),
Item_t (Int_t k, rest) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (signed_int_kind k)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (signed_int_kind k)) >>=? fun (Eq _) ->
return (Typed (Neg_int k, Item_t (Int_t k, rest))) return (Typed (Neg_int k, Item_t (Int_t k, rest)))
| Prim (loc, "add", []), Item_t (Int_t kl, Item_t (Int_t kr, rest)) -> | Prim (loc, "add", []),
Item_t (Int_t kl, Item_t (Int_t kr, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) ->
return (Typed (Add_int kl, Item_t (Int_t kl, rest))) return (Typed (Add_int kl, Item_t (Int_t kl, rest)))
| Prim (loc, "sub", []), Item_t (Int_t kl, Item_t (Int_t kr, rest)) -> | Prim (loc, "sub", []),
Item_t (Int_t kl, Item_t (Int_t kr, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) ->
return (Typed (Sub_int kl, Item_t (Int_t kl, rest))) return (Typed (Sub_int kl, Item_t (Int_t kl, rest)))
| Prim (loc, "mul", []), Item_t (Int_t kl, Item_t (Int_t kr, rest)) -> | Prim (loc, "mul", []),
Item_t (Int_t kl, Item_t (Int_t kr, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) ->
return (Typed (Mul_int kl, Item_t (Int_t kl, rest))) return (Typed (Mul_int kl, Item_t (Int_t kl, rest)))
| Prim (loc, "div", []), Item_t (Int_t kl, Item_t (Int_t kr, rest)) -> | Prim (loc, "div", []),
Item_t (Int_t kl, Item_t (Int_t kr, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) ->
return (Typed (Div_int (kl, loc), Item_t (Int_t kl, rest))) return (Typed (Div_int (kl, loc), Item_t (Int_t kl, rest)))
| Prim (loc, "mod", []), Item_t (Int_t kl, Item_t (Int_t kr, rest)) -> | Prim (loc, "mod", []),
Item_t (Int_t kl, Item_t (Int_t kr, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) ->
return (Typed (Mod_int (kl, loc), Item_t (Int_t kl, rest))) return (Typed (Mod_int (kl, loc), Item_t (Int_t kl, rest)))
| Prim (loc, "lsl", []), Item_t (Int_t k, Item_t (Int_t Uint8, rest)) -> | Prim (loc, "lsl", []),
Item_t (Int_t k, Item_t (Int_t Uint8, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (unsigned_int_kind k)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (unsigned_int_kind k)) >>=? fun (Eq _) ->
return (Typed (Lsl_int k, Item_t (Int_t k, rest))) return (Typed (Lsl_int k, Item_t (Int_t k, rest)))
| Prim (loc, "lsr", []), Item_t (Int_t k, Item_t (Int_t Uint8, rest)) -> | Prim (loc, "lsr", []),
Item_t (Int_t k, Item_t (Int_t Uint8, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (unsigned_int_kind k)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (unsigned_int_kind k)) >>=? fun (Eq _) ->
return (Typed (Lsr_int k, Item_t (Int_t k, rest))) return (Typed (Lsr_int k, Item_t (Int_t k, rest)))
| Prim (loc, "or", []), Item_t (Int_t kl, Item_t (Int_t kr, rest)) -> | Prim (loc, "or", []),
Item_t (Int_t kl, Item_t (Int_t kr, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (unsigned_int_kind kl)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (unsigned_int_kind kl)) >>=? fun (Eq _) ->
trace (Bad_stack_item (loc, 2)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 2)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) ->
return (Typed (Or_int kl, Item_t (Int_t kl, rest))) return (Typed (Or_int kl, Item_t (Int_t kl, rest)))
| Prim (loc, "and", []), Item_t (Int_t kl, Item_t (Int_t kr, rest)) -> | Prim (loc, "and", []),
Item_t (Int_t kl, Item_t (Int_t kr, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (unsigned_int_kind kl)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (unsigned_int_kind kl)) >>=? fun (Eq _) ->
trace (Bad_stack_item (loc, 2)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 2)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) ->
return (Typed (And_int kl, Item_t (Int_t kl, rest))) return (Typed (And_int kl, Item_t (Int_t kl, rest)))
| Prim (loc, "xor", []), Item_t (Int_t kl, Item_t (Int_t kr, rest)) -> | Prim (loc, "xor", []),
Item_t (Int_t kl, Item_t (Int_t kr, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (unsigned_int_kind kl)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (unsigned_int_kind kl)) >>=? fun (Eq _) ->
trace (Bad_stack_item (loc, 2)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 2)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) ->
return (Typed (Xor_int kl, Item_t (Int_t kl, rest))) return (Typed (Xor_int kl, Item_t (Int_t kl, rest)))
| Prim (loc, "not", []), Item_t (Int_t k, rest) -> | Prim (loc, "not", []),
Item_t (Int_t k, rest) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (unsigned_int_kind k)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (unsigned_int_kind k)) >>=? fun (Eq _) ->
return (Typed (Not_int k, Item_t (Int_t k, rest))) return (Typed (Not_int k, Item_t (Int_t k, rest)))
(* comparison *) (* comparison *)
| Prim (loc, "compare", []), Item_t (Int_t kl, Item_t (Int_t kr, rest)) -> | Prim (loc, "compare", []),
Item_t (Int_t kl, Item_t (Int_t kr, rest)) ->
trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) -> trace (Bad_stack_item (loc, 1)) (Lwt.return (int_kind_eq kl kr)) >>=? fun (Eq _) ->
return (Typed (Compare (Int_key kl), Item_t (Int_t Int64, rest))) return (Typed (Compare (Int_key kl), Item_t (Int_t Int64, rest)))
| Prim (_, "compare", []), Item_t (Bool_t, Item_t (Bool_t, rest)) -> | Prim (_, "compare", []),
Item_t (Bool_t, Item_t (Bool_t, rest)) ->
return (Typed (Compare Bool_key, Item_t (Int_t Int64, rest))) return (Typed (Compare Bool_key, Item_t (Int_t Int64, rest)))
| Prim (_, "compare", []), Item_t (String_t, Item_t (String_t, rest)) -> | Prim (_, "compare", []),
Item_t (String_t, Item_t (String_t, rest)) ->
return (Typed (Compare String_key, Item_t (Int_t Int64, rest))) return (Typed (Compare String_key, Item_t (Int_t Int64, rest)))
| Prim (_, "compare", []), Item_t (Tez_t, Item_t (Tez_t, rest)) -> | Prim (_, "compare", []),
Item_t (Tez_t, Item_t (Tez_t, rest)) ->
return (Typed (Compare Tez_key, Item_t (Int_t Int64, rest))) return (Typed (Compare Tez_key, Item_t (Int_t Int64, rest)))
| Prim (_, "compare", []), Item_t (Key_t, Item_t (Key_t, rest)) -> | Prim (_, "compare", []),
Item_t (Key_t, Item_t (Key_t, rest)) ->
return (Typed (Compare Key_key, Item_t (Int_t Int64, rest))) return (Typed (Compare Key_key, Item_t (Int_t Int64, rest)))
| Prim (_, "compare", []), Item_t (Timestamp_t, Item_t (Timestamp_t, rest)) -> | Prim (_, "compare", []),
Item_t (Timestamp_t, Item_t (Timestamp_t, rest)) ->
return (Typed (Compare Timestamp_key, Item_t (Int_t Int64, rest))) return (Typed (Compare Timestamp_key, Item_t (Int_t Int64, rest)))
(* comparators *) (* comparators *)
| Prim (_, "eq", []), Item_t (Int_t Int64, rest) -> | Prim (_, "eq", []),
Item_t (Int_t Int64, rest) ->
return (Typed (Eq, Item_t (Bool_t, rest))) return (Typed (Eq, Item_t (Bool_t, rest)))
| Prim (_, "neq", []), Item_t (Int_t Int64, rest) -> | Prim (_, "neq", []),
Item_t (Int_t Int64, rest) ->
return (Typed (Neq, Item_t (Bool_t, rest))) return (Typed (Neq, Item_t (Bool_t, rest)))
| Prim (_, "lt", []), Item_t (Int_t Int64, rest) -> | Prim (_, "lt", []),
Item_t (Int_t Int64, rest) ->
return (Typed (Lt, Item_t (Bool_t, rest))) return (Typed (Lt, Item_t (Bool_t, rest)))
| Prim (_, "gt", []), Item_t (Int_t Int64, rest) -> | Prim (_, "gt", []),
Item_t (Int_t Int64, rest) ->
return (Typed (Gt, Item_t (Bool_t, rest))) return (Typed (Gt, Item_t (Bool_t, rest)))
| Prim (_, "le", []), Item_t (Int_t Int64, rest) -> | Prim (_, "le", []),
Item_t (Int_t Int64, rest) ->
return (Typed (Le, Item_t (Bool_t, rest))) return (Typed (Le, Item_t (Bool_t, rest)))
| Prim (_, "ge", []), Item_t (Int_t Int64, rest) -> | Prim (_, "ge", []),
Item_t (Int_t Int64, rest) ->
return (Typed (Ge, Item_t (Bool_t, rest))) return (Typed (Ge, Item_t (Bool_t, rest)))
(* casts *) (* casts *)
| Prim (loc, "checked_cast", [ t ]), stack_ty -> | Prim (loc, "checked_cast", [ t ]),
parse_ty t >>=? fun (Ex ty) -> begin match ty, stack_ty with stack ->
| Int_t kt, Item_t (Int_t kf, rest) -> parse_ty t >>=? fun (Ex ty) -> begin match ty, stack with
| Int_t kt,
Item_t (Int_t kf, rest) ->
return (Typed (Checked_int_of_int (kf, kt, loc), Item_t (Int_t kt, rest))) return (Typed (Checked_int_of_int (kf, kt, loc), Item_t (Int_t kt, rest)))
| ty, Item_t (ty', _) -> | ty, Item_t (ty', _) ->
fail (Undefined_cast (loc, Ty ty', Ty ty)) fail (Undefined_cast (loc, Ty ty', Ty ty))
| _, Empty_t -> | _, Empty_t ->
fail (Bad_stack (loc, 1, Stack_ty stack_ty)) fail (Bad_stack (loc, 1, Stack_ty stack))
end end
| Prim (loc, "cast", [ t ]), stack_ty -> | Prim (loc, "cast", [ t ]),
parse_ty t >>=? fun (Ex ty) -> begin match ty,stack_ty with stack ->
parse_ty t >>=? fun (Ex ty) -> begin match ty, stack with
| Int_t kt, Item_t (Int_t kf, rest) -> | Int_t kt, Item_t (Int_t kf, rest) ->
return (Typed (Int_of_int (kf, kt), Item_t (Int_t kt, rest))) return (Typed (Int_of_int (kf, kt), Item_t (Int_t kt, rest)))
| ty, Item_t (ty', _) -> | ty, Item_t (ty', _) ->
fail (Undefined_cast (loc, Ty ty', Ty ty)) fail (Undefined_cast (loc, Ty ty', Ty ty))
| _, Empty_t -> | _, Empty_t ->
fail (Bad_stack (loc, 1, Stack_ty stack_ty)) fail (Bad_stack (loc, 1, Stack_ty stack))
end end
(* protocol *) (* protocol *)
| Prim (_, "manager", []), Item_t (Contract_t _, rest) -> | Prim (_, "manager", []),
Item_t (Contract_t _, rest) ->
return (Typed (Manager, Item_t (Key_t, rest))) return (Typed (Manager, Item_t (Key_t, rest)))
| Prim (loc, "transfer_tokens", []), | Prim (loc, "transfer_tokens", []),
Item_t (p, Item_t (Tez_t, Item_t (Contract_t (cp, cr), Item_t (storage, Empty_t)))) -> Item_t (p, Item_t
(Tez_t, Item_t
(Contract_t (cp, cr), Item_t
(storage, Empty_t)))) ->
check_item_ty p cp loc 1 >>=? fun (Eq _) -> check_item_ty p cp loc 1 >>=? fun (Eq _) ->
begin match storage_type with begin match storage_type with
| Some storage_type -> | Some storage_type ->
@ -1063,32 +1155,48 @@ and parse_instr
fail (Transfer_in_lambda loc) fail (Transfer_in_lambda loc)
end end
| Prim (_, "create_account", []), | Prim (_, "create_account", []),
Item_t (Key_t, Item_t (Option_t Key_t, Item_t (Bool_t, Item_t (Tez_t, rest)))) -> Item_t
(Key_t, Item_t
(Option_t Key_t, Item_t
(Bool_t, Item_t
(Tez_t, rest)))) ->
return (Typed (Create_account, Item_t (Contract_t (Void_t, Void_t), rest))) return (Typed (Create_account, Item_t (Contract_t (Void_t, Void_t), rest)))
| Prim (loc, "create_contract", []), | Prim (loc, "create_contract", []),
Item_t (Key_t, Item_t (Option_t Key_t, Item_t (Bool_t, Item_t (Tez_t, Item_t
Item_t (Lambda_t (Pair_t (Pair_t (Tez_t, p), gp), Pair_t (r, gr)), (Key_t, Item_t
Item_t (ginit, rest)))))) -> (Option_t Key_t, Item_t
(Bool_t, Item_t
(Tez_t, Item_t
(Lambda_t (Pair_t (Pair_t (Tez_t, p), gp),
Pair_t (r, gr)), Item_t
(ginit, rest)))))) ->
check_item_ty gp gr loc 5 >>=? fun (Eq _) -> check_item_ty gp gr loc 5 >>=? fun (Eq _) ->
check_item_ty ginit gp loc 6 >>=? fun (Eq _) -> check_item_ty ginit gp loc 6 >>=? fun (Eq _) ->
return (Typed (Create_contract (gp, p, r), return (Typed (Create_contract (gp, p, r),
Item_t (Contract_t (p, r), rest))) Item_t (Contract_t (p, r), rest)))
| Prim (_, "now", []), rest -> | Prim (_, "now", []),
return (Typed (Now, Item_t (Timestamp_t, rest))) stack ->
| Prim (_, "amount", []), rest -> return (Typed (Now, Item_t (Timestamp_t, stack)))
return (Typed (Amount, Item_t (Tez_t, rest))) | Prim (_, "amount", []),
| Prim (_, "balance", []), rest -> stack ->
return (Typed (Balance, Item_t (Tez_t, rest))) return (Typed (Amount, Item_t (Tez_t, stack)))
| Prim (_, "check_signature", []), Item_t (Key_t, Item_t (Pair_t (Signature_t, String_t), rest)) -> | Prim (_, "balance", []),
stack ->
return (Typed (Balance, Item_t (Tez_t, stack)))
| Prim (_, "check_signature", []),
Item_t (Key_t, Item_t (Pair_t (Signature_t, String_t), rest)) ->
return (Typed (Check_signature, Item_t (Bool_t, rest))) return (Typed (Check_signature, Item_t (Bool_t, rest)))
| Prim (_, "h", []), Item_t (t, rest) -> | Prim (_, "h", []),
Item_t (t, rest) ->
return (Typed (H t, Item_t (String_t, rest))) return (Typed (H t, Item_t (String_t, rest)))
| Prim (_, "steps_to_quota", []), rest -> | Prim (_, "steps_to_quota", []),
return (Typed (Steps_to_quota, Item_t (Int_t Uint32, rest))) stack ->
| Prim (_, "source", [ ta; tb ]), rest -> return (Typed (Steps_to_quota, Item_t (Int_t Uint32, stack)))
| Prim (_, "source", [ ta; tb ]),
stack ->
parse_ty ta >>=? fun (Ex ta) -> parse_ty ta >>=? fun (Ex ta) ->
parse_ty tb >>=? fun (Ex tb) -> parse_ty tb >>=? fun (Ex tb) ->
return (Typed (Source (ta, tb), Item_t (Contract_t (ta, tb), rest))) return (Typed (Source (ta, tb), Item_t (Contract_t (ta, tb), stack)))
(* Primitive parsing errors *) (* Primitive parsing errors *)
| Prim (loc, ("drop" | "dup" | "swap" | "some" | Prim (loc, ("drop" | "dup" | "swap" | "some"
| "pair" | "car" | "cdr" | "cons" | "pair" | "car" | "cdr" | "cons"
@ -1132,28 +1240,34 @@ and parse_instr
| "neq" | "lt" | "gt" | "le" | "ge" as name), []), | "neq" | "lt" | "gt" | "le" | "ge" as name), []),
Item_t (t, _) -> Item_t (t, _) ->
fail (Undefined_unop (loc, name, Ty t)) fail (Undefined_unop (loc, name, Ty t))
| Prim (loc, ("reduce" | "update"), []), _ -> | Prim (loc, ("reduce" | "update"), []),
fail (Bad_stack (loc, 3, Stack_ty stack_ty)) stack ->
| Prim (loc, "create_contract", []), _ -> fail (Bad_stack (loc, 3, Stack_ty stack))
fail (Bad_stack (loc, 6, Stack_ty stack_ty)) | Prim (loc, "create_contract", []),
| Prim (loc, "create_account", []), _ -> stack ->
fail (Bad_stack (loc, 4, Stack_ty stack_ty)) fail (Bad_stack (loc, 6, Stack_ty stack))
| Prim (loc, "transfer_tokens", []), _ -> | Prim (loc, "create_account", []),
fail (Bad_stack (loc, 3, Stack_ty stack_ty)) stack ->
fail (Bad_stack (loc, 4, Stack_ty stack))
| Prim (loc, "transfer_tokens", []),
stack ->
fail (Bad_stack (loc, 3, Stack_ty stack))
| Prim (loc, ("drop" | "dup" | "car" | "cdr" | "some" | "h" | "dip" | Prim (loc, ("drop" | "dup" | "car" | "cdr" | "some" | "h" | "dip"
| "if_none" | "left" | "right" | "if_left" | "if" | "if_none" | "left" | "right" | "if_left" | "if"
| "loop" | "if_cons" | "ref" | "deref" | "manager" | "loop" | "if_cons" | "ref" | "deref" | "manager"
| "neg" | "abs" | "not" | "floor" | "ceil" | "isnan" | "nanan" | "neg" | "abs" | "not" | "floor" | "ceil" | "isnan" | "nanan"
| "eq" | "neq" | "lt" | "gt" | "le" | "ge"), _), _ -> | "eq" | "neq" | "lt" | "gt" | "le" | "ge"), _),
fail (Bad_stack (loc, 1, Stack_ty stack_ty)) stack ->
fail (Bad_stack (loc, 1, Stack_ty stack))
| Prim (loc, ("swap" | "pair" | "cons" | "set" | "incr" | "decr" | Prim (loc, ("swap" | "pair" | "cons" | "set" | "incr" | "decr"
| "map" | "iter" | "get" | "mem" | "exec" | "map" | "iter" | "get" | "mem" | "exec"
| "check_signature" | "add" | "sub" | "mul" | "check_signature" | "add" | "sub" | "mul"
| "div" | "mod" | "and" | "or" | "xor" | "div" | "mod" | "and" | "or" | "xor"
| "lsl" | "lsr" | "concat" | "lsl" | "lsr" | "concat"
| "checked_abs" | "checked_neg" | "checked_add" | "checked_abs" | "checked_neg" | "checked_add"
| "checked_sub" | "checked_mul" | "compare"), _), _ -> | "checked_sub" | "checked_mul" | "compare"), _),
fail (Bad_stack (loc, 2, Stack_ty stack_ty)) stack ->
fail (Bad_stack (loc, 2, Stack_ty stack))
(* Generic parsing errors *) (* Generic parsing errors *)
| Prim (loc, prim, _), _ -> | Prim (loc, prim, _), _ ->
fail @@ Invalid_primitive (loc, Instr, prim) fail @@ Invalid_primitive (loc, Instr, prim)