161 lines
5.4 KiB
OCaml
Raw Normal View History

2019-09-24 13:54:34 +02:00
open Ast_simplified
open Trace
open Proto_alpha_utils
2019-09-24 13:54:34 +02:00
2020-01-02 15:11:02 +01:00
module Errors = struct
2020-01-06 22:52:30 +01:00
let bad_format e () =
let title = (thunk ("Badly formatted literal")) in
let message () = Format.asprintf "%a" Ast_simplified.PP.expression e in
2020-01-02 15:11:02 +01:00
let data = [
2020-01-06 22:52:30 +01:00
("location" , fun () -> Format.asprintf "%a" Location.pp e.location)
2020-01-02 15:11:02 +01:00
] in
error ~data title message ()
2020-01-06 22:52:30 +01:00
2020-01-05 17:27:09 +01:00
let bad_empty_arity cst loc () =
let cst_name = thunk @@ Format.asprintf "%a" Stage_common.PP.constant cst in
let title = thunk @@ "Wrong "^(cst_name ())^" literal arity" in
let message = thunk @@ (cst_name ())^" literal expects no parameter" in
let data = [
("location" , fun () -> Format.asprintf "%a" Location.pp loc) ;
] in
error ~data title message ()
let bad_single_arity cst loc () =
let cst_name = thunk @@ Format.asprintf "%a" Stage_common.PP.constant cst in
let title = thunk @@ "Wrong "^(cst_name ())^" literal arity" in
let message = thunk @@ (cst_name ())^" literal expects a single parameter" in
let data = [
("location" , fun () -> Format.asprintf "%a" Location.pp loc) ;
] in
error ~data title message ()
let bad_map_param_type cst loc () =
let cst_name = thunk @@ Format.asprintf "%a" Stage_common.PP.constant cst in
let title = thunk @@ "Wrong "^(cst_name ())^" literal parameter type" in
let message = thunk @@ (cst_name ())^" literal expects a list of pairs as parameter" in
let data = [
("location" , fun () -> Format.asprintf "%a" Location.pp loc) ;
] in
error ~data title message ()
let bad_set_param_type cst loc () =
let cst_name = thunk @@ Format.asprintf "%a" Stage_common.PP.constant cst in
let title = thunk @@ "Wrong "^(cst_name ())^" literal parameter type" in
let message = thunk @@ (cst_name ())^" literal expects a list as parameter" in
let data = [
("location" , fun () -> Format.asprintf "%a" Location.pp loc) ;
] in
error ~data title message ()
2020-01-02 15:11:02 +01:00
end
open Errors
2019-09-24 13:54:34 +02:00
let peephole_expression : expression -> expression result = fun e ->
2019-12-04 18:30:52 +01:00
let return expression_content = ok { e with expression_content } in
match e.expression_content with
2020-01-06 22:52:30 +01:00
| E_literal (Literal_key_hash s) as l -> (
let open Tezos_crypto in
let%bind (_pkh:Crypto.Signature.public_key_hash) =
Trace.trace_tzresult (bad_format e) @@
Signature.Public_key_hash.of_b58check s in
return l
)
2020-01-02 15:11:02 +01:00
| E_literal (Literal_address s) as l -> (
let open Memory_proto_alpha in
let%bind (_contract:Protocol.Alpha_context.Contract.t) =
2020-01-06 22:52:30 +01:00
Trace.trace_alpha_tzresult (bad_format e) @@
Protocol.Alpha_context.Contract.of_b58check s in
2020-01-02 15:11:02 +01:00
return l
)
2020-01-13 11:37:10 +01:00
| E_literal (Literal_signature s) as l -> (
let open Tezos_crypto in
let%bind (_sig:Crypto.Signature.t) =
Trace.trace_tzresult (bad_format e) @@
Signature.of_b58check s in
return l
)
| E_literal (Literal_key s) as l -> (
let open Tezos_crypto in
let%bind (_k:Crypto.Signature.public_key) =
Trace.trace_tzresult (bad_format e) @@
Signature.Public_key.of_b58check s in
return l
)
2019-12-04 18:30:52 +01:00
| E_constant {cons_name=C_BIG_MAP_LITERAL as cst; arguments=lst} -> (
let%bind elt =
2020-01-05 17:27:09 +01:00
trace_option (bad_single_arity cst e.location) @@
List.to_singleton lst
in
let%bind lst =
2020-01-05 17:27:09 +01:00
trace_strong (bad_map_param_type cst e.location) @@
2019-12-04 18:30:52 +01:00
get_e_list elt.expression_content
in
2019-12-04 18:30:52 +01:00
let aux = fun (e : expression) ->
2020-01-05 17:27:09 +01:00
trace_strong (bad_map_param_type cst e.location) @@
2019-12-04 18:30:52 +01:00
let%bind tpl = get_e_tuple e.expression_content in
let%bind (a , b) =
trace_option (simple_error "of pairs") @@
List.to_pair tpl
in
ok (a , b)
in
let%bind pairs = bind_map_list aux lst in
return @@ E_big_map pairs
)
2019-12-04 18:30:52 +01:00
| E_constant {cons_name=C_MAP_LITERAL as cst; arguments=lst} -> (
2019-09-24 13:54:34 +02:00
let%bind elt =
2020-01-05 17:27:09 +01:00
trace_option (bad_single_arity cst e.location) @@
2019-09-24 13:54:34 +02:00
List.to_singleton lst
in
let%bind lst =
2020-01-05 17:27:09 +01:00
trace_strong (bad_map_param_type cst e.location) @@
2019-12-04 18:30:52 +01:00
get_e_list elt.expression_content
2019-09-24 13:54:34 +02:00
in
2019-12-04 18:30:52 +01:00
let aux = fun (e : expression) ->
2020-01-05 17:27:09 +01:00
trace_strong (bad_map_param_type cst e.location) @@
2019-12-04 18:30:52 +01:00
let%bind tpl = get_e_tuple e.expression_content in
2019-09-24 13:54:34 +02:00
let%bind (a , b) =
trace_option (simple_error "of pairs") @@
List.to_pair tpl
in
ok (a , b)
in
let%bind pairs = bind_map_list aux lst in
return @@ E_map pairs
)
2019-12-04 18:30:52 +01:00
| E_constant {cons_name=C_BIG_MAP_EMPTY as cst; arguments=lst} -> (
let%bind () =
2020-01-05 17:27:09 +01:00
trace_strong (bad_empty_arity cst e.location) @@
Assert.assert_list_empty lst
in
return @@ E_big_map []
)
2019-12-04 18:30:52 +01:00
| E_constant {cons_name=C_MAP_EMPTY as cst; arguments=lst} -> (
2019-09-24 13:54:34 +02:00
let%bind () =
2020-01-05 17:27:09 +01:00
trace_strong (bad_empty_arity cst e.location) @@
2019-09-24 13:54:34 +02:00
Assert.assert_list_empty lst
in
return @@ E_map []
)
2019-12-04 18:30:52 +01:00
| E_constant {cons_name=C_SET_LITERAL as cst; arguments=lst} -> (
2019-09-24 14:00:43 +02:00
let%bind elt =
2020-01-05 17:27:09 +01:00
trace_option (bad_single_arity cst e.location) @@
2019-09-24 14:00:43 +02:00
List.to_singleton lst
in
let%bind lst =
2020-01-05 17:27:09 +01:00
trace_strong (bad_set_param_type cst e.location) @@
2019-12-04 18:30:52 +01:00
get_e_list elt.expression_content
2019-09-24 14:00:43 +02:00
in
return @@ E_set lst
)
2019-12-04 18:30:52 +01:00
| E_constant {cons_name=C_SET_EMPTY as cst; arguments=lst} -> (
2019-09-24 14:00:43 +02:00
let%bind () =
2020-01-05 17:27:09 +01:00
trace_strong (bad_empty_arity cst e.location) @@
2019-09-24 14:00:43 +02:00
Assert.assert_list_empty lst
in
return @@ E_set []
)
2019-09-24 13:54:34 +02:00
| e -> return e