Michelson: enforce case sensitivity.

This commit is contained in:
Benjamin Canou 2016-12-14 18:05:09 +01:00
parent 8b8ce63590
commit 6bfbb0d300
4 changed files with 328 additions and 330 deletions

View File

@ -121,10 +121,10 @@ let unexpand_macros type_map program =
let open Script in let open Script in
let rec caddr type_map acc = function let rec caddr type_map acc = function
| [] -> Some (List.rev acc) | [] -> Some (List.rev acc)
| Prim (loc, "car" , []) :: rest when List.mem_assoc loc type_map -> | Prim (loc, "CAR" , []) :: rest when List.mem_assoc loc type_map ->
caddr type_map ((loc, "a") :: acc) rest caddr type_map ((loc, "A") :: acc) rest
| Prim (loc, "cdr" , []) :: rest when List.mem_assoc loc type_map -> | Prim (loc, "CDR" , []) :: rest when List.mem_assoc loc type_map ->
caddr type_map ((loc, "d") :: acc) rest caddr type_map ((loc, "D") :: acc) rest
| _ -> None in | _ -> None in
let rec unexpand type_map node = let rec unexpand type_map node =
match node with match node with
@ -141,7 +141,7 @@ let unexpand_macros type_map program =
type_map, Seq (loc, List.rev l) type_map, Seq (loc, List.rev l)
| Some l -> | Some l ->
let locs, steps = List.split l in let locs, steps = List.split l in
let name = "c" ^ String.concat "" steps ^ "r" in let name = "C" ^ String.concat "" steps ^ "R" in
let first, last = List.hd locs, List.hd (List.rev locs) in let first, last = List.hd locs, List.hd (List.rev locs) in
let (before, _) = List.assoc first type_map in let (before, _) = List.assoc first type_map in
let (_, after) = List.assoc last type_map in let (_, after) = List.assoc last type_map in
@ -256,14 +256,13 @@ let commands () =
let type_map, program = unexpand_macros type_map program in let type_map, program = unexpand_macros type_map program in
cctxt.message "Well typed" >>= fun () -> cctxt.message "Well typed" >>= fun () ->
if !show_types then begin if !show_types then begin
print_program cctxt.message "%a"
(fun l -> List.mem_assoc l type_map) (print_program (fun l -> List.mem_assoc l type_map))
Format.std_formatter program ; program >>= fun () ->
cctxt.message "@." >>= fun () ->
Lwt_list.iter_s Lwt_list.iter_s
(fun (loc, (before, after)) -> (fun (loc, (before, after)) ->
cctxt.message cctxt.message
"%3d@[<v 0> : [ @[<v 0>%a ]@]@,-> [ @[<v 0>%a ]@]@]@." "%3d@[<v 0> : [ @[<v 0>%a ]@]@,-> [ @[<v 0>%a ]@]@]"
loc loc
(Format.pp_print_list (print_ir (fun _ -> false))) (Format.pp_print_list (print_ir (fun _ -> false)))
before before

View File

@ -306,7 +306,7 @@ and raw_token st = parse
| ";" { SEMICOLON } | ";" { SEMICOLON }
| firstidentchar identchar * | firstidentchar identchar *
{ PRIM (String.lowercase_ascii (Lexing.lexeme lexbuf)) } { PRIM (Lexing.lexeme lexbuf) }
| int_literal | int_literal
{ INT (Lexing.lexeme lexbuf) } { INT (Lexing.lexeme lexbuf) }

View File

@ -25,15 +25,15 @@ open Script_located_ir
let expand_caddadr loc str = let expand_caddadr loc str =
let len = String.length str in let len = String.length str in
if len > 3 if len > 3
&& String.get str 0 = 'c' && String.get str 0 = 'C'
&& String.get str (len - 1) = 'r' then && String.get str (len - 1) = 'R' then
let rec parse i acc = let rec parse i acc =
if i = 0 then if i = 0 then
Some (Seq (loc, acc)) Some (Seq (loc, acc))
else else
match String.get str i with match String.get str i with
| 'a' -> parse (i - 1) (Prim (loc, "car", []) :: acc) | 'A' -> parse (i - 1) (Prim (loc, "CAR", []) :: acc)
| 'd' -> parse (i - 1) (Prim (loc, "cdr", []) :: acc) | 'D' -> parse (i - 1) (Prim (loc, "CDR", []) :: acc)
| _ -> None in | _ -> None in
parse (len - 2) [] parse (len - 2) []
else else
@ -48,13 +48,13 @@ let decimal_of_roman roman =
for i = (String.length roman) - 1 downto 0 do for i = (String.length roman) - 1 downto 0 do
let n = let n =
match roman.[i] with match roman.[i] with
| 'm' -> 1000 | 'M' -> 1000
| 'd' -> 500 | 'D' -> 500
| 'c' -> 100 | 'C' -> 100
| 'l' -> 50 | 'L' -> 50
| 'x' -> 10 | 'X' -> 10
| 'v' -> 5 | 'V' -> 5
| 'i' -> 1 | 'I' -> 1
| _ -> raise Not_a_roman | _ -> raise Not_a_roman
in in
if Compare.Int.(n < !lastval) if Compare.Int.(n < !lastval)
@ -67,8 +67,8 @@ let decimal_of_roman roman =
let expand_dxiiivp loc str arg = let expand_dxiiivp loc str arg =
let len = String.length str in let len = String.length str in
if len > 3 if len > 3
&& String.get str 0 = 'd' && String.get str 0 = 'D'
&& String.get str (len - 1) = 'p' then && String.get str (len - 1) = 'P' then
try try
let depth = decimal_of_roman (String.sub str 1 (len - 2)) in let depth = decimal_of_roman (String.sub str 1 (len - 2)) in
let rec make i = let rec make i =
@ -76,7 +76,7 @@ let expand_dxiiivp loc str arg =
arg arg
else else
let sub = make (i - 1) in let sub = make (i - 1) in
Prim (loc, "dip", [ sub ]) in Prim (loc, "DIP", [ Seq (loc, [ sub ]) ]) in
Some (make depth) Some (make depth)
with Not_a_roman -> None with Not_a_roman -> None
else None else None
@ -86,19 +86,19 @@ exception Not_a_pair
let expand_paaiair loc str = let expand_paaiair loc str =
let len = String.length str in let len = String.length str in
if len > 4 if len > 4
&& String.get str 0 = 'p' && String.get str 0 = 'P'
&& String.get str (len - 1) = 'r' then && String.get str (len - 1) = 'R' then
try try
let rec parse i acc = let rec parse i acc =
if String.get str i = 'i' if String.get str i = 'I'
&& String.get str (i - 1) = 'a' then && String.get str (i - 1) = 'A' then
parse (i - 2) (Prim (loc, "pair", []) :: acc) parse (i - 2) (Prim (loc, "PAIR", []) :: acc)
else if String.get str i = 'a' then else if String.get str i = 'A' then
match acc with match acc with
| [] -> | [] ->
raise Not_a_pair raise Not_a_pair
| acc :: accs -> | acc :: accs ->
parse (i - 1) (Prim (loc, "dip", [ acc ]) :: accs) parse (i - 1) (Prim (loc, "DIP", [ acc ]) :: accs)
else else
raise Not_a_pair in raise Not_a_pair in
Some (Seq (loc, parse (len - 2) [])) Some (Seq (loc, parse (len - 2) []))

File diff suppressed because it is too large Load Diff