Michelson: allow annotations on CMP... macros

This commit is contained in:
Alain Mebsout 2018-05-18 22:46:56 +02:00 committed by Benjamin Canou
parent 68dcae1a11
commit 263b2d717f

View File

@ -356,55 +356,58 @@ let expand_duuuuup original =
| _ -> ok None | _ -> ok None
let expand_compare original = let expand_compare original =
let cmp loc is = let cmp loc is annot =
let is = let is =
List.map (fun i -> Prim (loc, i, [], [])) is in match List.rev_map (fun i -> Prim (loc, i, [], [])) is with
| Prim (loc, i, args, _) :: r -> List.rev (Prim (loc, i, args, annot) :: r)
| is -> List.rev is
in
ok (Some (Seq (loc, is))) in ok (Some (Seq (loc, is))) in
let ifcmp loc is l r = let ifcmp loc is l r annot =
let is = let is =
List.map (fun i -> Prim (loc, i, [], [])) is @ List.map (fun i -> Prim (loc, i, [], [])) is @
[ Prim (loc, "IF", [ l ; r ], []) ] in [ Prim (loc, "IF", [ l ; r ], annot) ] in
ok (Some (Seq (loc, is))) in ok (Some (Seq (loc, is))) in
match original with match original with
| Prim (loc, "CMPEQ", [], []) -> | Prim (loc, "CMPEQ", [], annot) ->
cmp loc [ "COMPARE" ; "EQ" ] cmp loc [ "COMPARE" ; "EQ" ] annot
| Prim (loc, "CMPNEQ", [], []) -> | Prim (loc, "CMPNEQ", [], annot) ->
cmp loc [ "COMPARE" ; "NEQ" ] cmp loc [ "COMPARE" ; "NEQ" ] annot
| Prim (loc, "CMPLT", [], []) -> | Prim (loc, "CMPLT", [], annot) ->
cmp loc [ "COMPARE" ; "LT" ] cmp loc [ "COMPARE" ; "LT" ] annot
| Prim (loc, "CMPGT", [], []) -> | Prim (loc, "CMPGT", [], annot) ->
cmp loc [ "COMPARE" ; "GT" ] cmp loc [ "COMPARE" ; "GT" ] annot
| Prim (loc, "CMPLE", [], []) -> | Prim (loc, "CMPLE", [], annot) ->
cmp loc [ "COMPARE" ; "LE" ] cmp loc [ "COMPARE" ; "LE" ] annot
| Prim (loc, "CMPGE", [], []) -> | Prim (loc, "CMPGE", [], annot) ->
cmp loc [ "COMPARE" ; "GE" ] cmp loc [ "COMPARE" ; "GE" ] annot
| Prim (_, ("CMPEQ" | "CMPNEQ" | "CMPLT" | Prim (_, ("CMPEQ" | "CMPNEQ" | "CMPLT"
| "CMPGT" | "CMPLE" | "CMPGE" as str), args, []) -> | "CMPGT" | "CMPLE" | "CMPGE" as str), args, []) ->
error (Invalid_arity (str, List.length args, 0)) error (Invalid_arity (str, List.length args, 0))
| Prim (loc, "IFCMPEQ", [ l ; r ], []) -> | Prim (loc, "IFCMPEQ", [ l ; r ], annot) ->
ifcmp loc [ "COMPARE" ; "EQ" ] l r ifcmp loc [ "COMPARE" ; "EQ" ] l r annot
| Prim (loc, "IFCMPNEQ", [ l ; r ], []) -> | Prim (loc, "IFCMPNEQ", [ l ; r ], annot) ->
ifcmp loc [ "COMPARE" ; "NEQ" ] l r ifcmp loc [ "COMPARE" ; "NEQ" ] l r annot
| Prim (loc, "IFCMPLT", [ l ; r ], []) -> | Prim (loc, "IFCMPLT", [ l ; r ], annot) ->
ifcmp loc [ "COMPARE" ; "LT" ] l r ifcmp loc [ "COMPARE" ; "LT" ] l r annot
| Prim (loc, "IFCMPGT", [ l ; r ], []) -> | Prim (loc, "IFCMPGT", [ l ; r ], annot) ->
ifcmp loc [ "COMPARE" ; "GT" ] l r ifcmp loc [ "COMPARE" ; "GT" ] l r annot
| Prim (loc, "IFCMPLE", [ l ; r ], []) -> | Prim (loc, "IFCMPLE", [ l ; r ], annot) ->
ifcmp loc [ "COMPARE" ; "LE" ] l r ifcmp loc [ "COMPARE" ; "LE" ] l r annot
| Prim (loc, "IFCMPGE", [ l ; r ], []) -> | Prim (loc, "IFCMPGE", [ l ; r ], annot) ->
ifcmp loc [ "COMPARE" ; "GE" ] l r ifcmp loc [ "COMPARE" ; "GE" ] l r annot
| Prim (loc, "IFEQ", [ l ; r ], []) -> | Prim (loc, "IFEQ", [ l ; r ], annot) ->
ifcmp loc [ "EQ" ] l r ifcmp loc [ "EQ" ] l r annot
| Prim (loc, "IFNEQ", [ l ; r ], []) -> | Prim (loc, "IFNEQ", [ l ; r ], annot) ->
ifcmp loc [ "NEQ" ] l r ifcmp loc [ "NEQ" ] l r annot
| Prim (loc, "IFLT", [ l ; r ], []) -> | Prim (loc, "IFLT", [ l ; r ], annot) ->
ifcmp loc [ "LT" ] l r ifcmp loc [ "LT" ] l r annot
| Prim (loc, "IFGT", [ l ; r ], []) -> | Prim (loc, "IFGT", [ l ; r ], annot) ->
ifcmp loc [ "GT" ] l r ifcmp loc [ "GT" ] l r annot
| Prim (loc, "IFLE", [ l ; r ], []) -> | Prim (loc, "IFLE", [ l ; r ], annot) ->
ifcmp loc [ "LE" ] l r ifcmp loc [ "LE" ] l r annot
| Prim (loc, "IFGE", [ l ; r ], []) -> | Prim (loc, "IFGE", [ l ; r ], annot) ->
ifcmp loc [ "GE" ] l r ifcmp loc [ "GE" ] l r annot
| Prim (_, ("IFCMPEQ" | "IFCMPNEQ" | "IFCMPLT" | Prim (_, ("IFCMPEQ" | "IFCMPNEQ" | "IFCMPLT"
| "IFCMPGT" | "IFCMPLE" | "IFCMPGE" | "IFCMPGT" | "IFCMPLE" | "IFCMPGE"
| "IFEQ" | "IFNEQ" | "IFLT" | "IFEQ" | "IFNEQ" | "IFLT"
@ -413,9 +416,7 @@ let expand_compare original =
| Prim (_, ("IFCMPEQ" | "IFCMPNEQ" | "IFCMPLT" | Prim (_, ("IFCMPEQ" | "IFCMPNEQ" | "IFCMPLT"
| "IFCMPGT" | "IFCMPLE" | "IFCMPGE" | "IFCMPGT" | "IFCMPLE" | "IFCMPGE"
| "IFEQ" | "IFNEQ" | "IFLT" | "IFEQ" | "IFNEQ" | "IFLT"
| "IFGT" | "IFLE" | "IFGE" | "IFGT" | "IFLE" | "IFGE" as str), [], _ :: _) ->
| "CMPEQ" | "CMPNEQ" | "CMPLT"
| "CMPGT" | "CMPLE" | "CMPGE" as str), [], _ :: _) ->
error (Unexpected_macro_annotation str) error (Unexpected_macro_annotation str)
| _ -> ok None | _ -> ok None
@ -746,66 +747,66 @@ let unexpand_unpaaiair expanded =
let unexpand_compare expanded = let unexpand_compare expanded =
match expanded with match expanded with
| Seq (loc, [ Prim (_, "COMPARE", [], []) ; | Seq (loc, [ Prim (_, "COMPARE", [], _) ;
Prim (_, "EQ", [], []) ]) -> Prim (_, "EQ", [], annot) ]) ->
Some (Prim (loc, "CMPEQ", [], [])) Some (Prim (loc, "CMPEQ", [], annot))
| Seq (loc, [ Prim (_, "COMPARE", [], []) ; | Seq (loc, [ Prim (_, "COMPARE", [], _) ;
Prim (_, "NEQ", [], []) ]) -> Prim (_, "NEQ", [], annot) ]) ->
Some (Prim (loc, "CMPNEQ", [], [])) Some (Prim (loc, "CMPNEQ", [], annot))
| Seq (loc, [ Prim (_, "COMPARE", [], []) ; | Seq (loc, [ Prim (_, "COMPARE", [], _) ;
Prim (_, "LT", [], []) ]) -> Prim (_, "LT", [], annot) ]) ->
Some (Prim (loc, "CMPLT", [], [])) Some (Prim (loc, "CMPLT", [], annot))
| Seq (loc, [ Prim (_, "COMPARE", [], []) ; | Seq (loc, [ Prim (_, "COMPARE", [], _) ;
Prim (_, "GT", [], []) ]) -> Prim (_, "GT", [], annot) ]) ->
Some (Prim (loc, "CMPGT", [], [])) Some (Prim (loc, "CMPGT", [], annot))
| Seq (loc, [ Prim (_, "COMPARE", [], []) ; | Seq (loc, [ Prim (_, "COMPARE", [], _) ;
Prim (_, "LE", [], []) ]) -> Prim (_, "LE", [], annot) ]) ->
Some (Prim (loc, "CMPLE", [], [])) Some (Prim (loc, "CMPLE", [], annot))
| Seq (loc, [ Prim (_, "COMPARE", [], []) ; | Seq (loc, [ Prim (_, "COMPARE", [], _) ;
Prim (_, "GE", [], []) ]) -> Prim (_, "GE", [], annot) ]) ->
Some (Prim (loc, "CMPGE", [], [])) Some (Prim (loc, "CMPGE", [], annot))
| Seq (loc, [ Prim (_, "COMPARE", [], []) ; | Seq (loc, [ Prim (_, "COMPARE", [], _) ;
Prim (_, "EQ", [], []) ; Prim (_, "EQ", [], _) ;
Prim (_, "IF", args, []) ]) -> Prim (_, "IF", args, annot) ]) ->
Some (Prim (loc, "IFCMPEQ", args, [])) Some (Prim (loc, "IFCMPEQ", args, annot))
| Seq (loc, [ Prim (_, "COMPARE", [], []) ; | Seq (loc, [ Prim (_, "COMPARE", [], _) ;
Prim (_, "NEQ", [], []) ; Prim (_, "NEQ", [], _) ;
Prim (_, "IF", args, []) ]) -> Prim (_, "IF", args, annot) ]) ->
Some (Prim (loc, "IFCMPNEQ", args, [])) Some (Prim (loc, "IFCMPNEQ", args, annot))
| Seq (loc, [ Prim (_, "COMPARE", [], []) ; | Seq (loc, [ Prim (_, "COMPARE", [], _) ;
Prim (_, "LT", [], []) ; Prim (_, "LT", [], _) ;
Prim (_, "IF", args, []) ]) -> Prim (_, "IF", args, annot) ]) ->
Some (Prim (loc, "IFCMPLT", args, [])) Some (Prim (loc, "IFCMPLT", args, annot))
| Seq (loc, [ Prim (_, "COMPARE", [], []) ; | Seq (loc, [ Prim (_, "COMPARE", [], _) ;
Prim (_, "GT", [], []) ; Prim (_, "GT", [], _) ;
Prim (_, "IF", args, []) ]) -> Prim (_, "IF", args, annot) ]) ->
Some (Prim (loc, "IFCMPGT", args, [])) Some (Prim (loc, "IFCMPGT", args, annot))
| Seq (loc, [ Prim (_, "COMPARE", [], []) ; | Seq (loc, [ Prim (_, "COMPARE", [], _) ;
Prim (_, "LE", [], []) ; Prim (_, "LE", [], _) ;
Prim (_, "IF", args, []) ]) -> Prim (_, "IF", args, annot) ]) ->
Some (Prim (loc, "IFCMPLE", args, [])) Some (Prim (loc, "IFCMPLE", args, annot))
| Seq (loc, [ Prim (_, "COMPARE", [], []) ; | Seq (loc, [ Prim (_, "COMPARE", [], _) ;
Prim (_, "GE", [], []) ; Prim (_, "GE", [], _) ;
Prim (_, "IF", args, []) ]) -> Prim (_, "IF", args, annot) ]) ->
Some (Prim (loc, "IFCMPGE", args, [])) Some (Prim (loc, "IFCMPGE", args, annot))
| Seq (loc, [ Prim (_, "EQ", [], []) ; | Seq (loc, [ Prim (_, "EQ", [], _) ;
Prim (_, "IF", args, []) ]) -> Prim (_, "IF", args, annot) ]) ->
Some (Prim (loc, "IFEQ", args, [])) Some (Prim (loc, "IFEQ", args, annot))
| Seq (loc, [ Prim (_, "NEQ", [], []) ; | Seq (loc, [ Prim (_, "NEQ", [], _) ;
Prim (_, "IF", args, []) ]) -> Prim (_, "IF", args, annot) ]) ->
Some (Prim (loc, "IFNEQ", args, [])) Some (Prim (loc, "IFNEQ", args, annot))
| Seq (loc, [ Prim (_, "LT", [], []) ; | Seq (loc, [ Prim (_, "LT", [], _) ;
Prim (_, "IF", args, []) ]) -> Prim (_, "IF", args, annot) ]) ->
Some (Prim (loc, "IFLT", args, [])) Some (Prim (loc, "IFLT", args, annot))
| Seq (loc, [ Prim (_, "GT", [], []) ; | Seq (loc, [ Prim (_, "GT", [], _) ;
Prim (_, "IF", args, []) ]) -> Prim (_, "IF", args, annot) ]) ->
Some (Prim (loc, "IFGT", args, [])) Some (Prim (loc, "IFGT", args, annot))
| Seq (loc, [ Prim (_, "LE", [], []) ; | Seq (loc, [ Prim (_, "LE", [], _) ;
Prim (_, "IF", args, []) ]) -> Prim (_, "IF", args, annot) ]) ->
Some (Prim (loc, "IFLE", args, [])) Some (Prim (loc, "IFLE", args, annot))
| Seq (loc, [ Prim (_, "GE", [], []) ; | Seq (loc, [ Prim (_, "GE", [], _) ;
Prim (_, "IF", args, []) ]) -> Prim (_, "IF", args, annot) ]) ->
Some (Prim (loc, "IFGE", args, [])) Some (Prim (loc, "IFGE", args, annot))
| _ -> None | _ -> None
let unexpand_asserts expanded = let unexpand_asserts expanded =
@ -844,13 +845,13 @@ let unexpand_asserts expanded =
let unexpand_if_some = function let unexpand_if_some = function
| Seq (loc, [ Prim (_, "IF_NONE", [ left ; right ], []) ]) -> | Seq (loc, [ Prim (_, "IF_NONE", [ left ; right ], annot) ]) ->
Some (Prim (loc, "IF_SOME", [ right ; left ], [])) Some (Prim (loc, "IF_SOME", [ right ; left ], annot))
| _ -> None | _ -> None
let unexpand_if_right = function let unexpand_if_right = function
| Seq (loc, [ Prim (_, "IF_LEFT", [ left ; right ], []) ]) -> | Seq (loc, [ Prim (_, "IF_LEFT", [ left ; right ], annot) ]) ->
Some (Prim (loc, "IF_RIGHT", [ right ; left ], [])) Some (Prim (loc, "IF_RIGHT", [ right ; left ], annot))
| _ -> None | _ -> None
let unexpand_rename = function let unexpand_rename = function