package archetype

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file micheline_tools.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module PT = ParseTree
module A  = Ast
module M  = Model
module TZ = Michelson

type micheline = Micheline_printer.node

let emptyloc : Micheline_printer.location = {comment = None}

let mkstring  v = Micheline.String (emptyloc, v)
let mkint     v = Micheline.Int (emptyloc, v)
let mkbytes   v = Micheline.Bytes (emptyloc, v)
let mkprim    (p, args, annot) = Micheline.Prim (emptyloc, p, args, annot)
let mkseq nodes = Micheline.Seq (emptyloc, nodes)

let pt_to_micheline (pt : PT.micheline_t) : micheline =
  let rec aux (pt : PT.micheline_t) : micheline =
    match pt with
    | MIstring v -> mkstring v
    | MIint    v -> mkint v
    | MIbytes  v -> mkbytes (Hex.to_bytes (`Hex v))
    | MIprim   (p, args, annots) -> mkprim (p, List.map aux args, annots)
    | MIseq    vs -> mkseq (List.map aux vs)
  in
  aux pt

let pt_to_obj (pt : PT.micheline_t) : TZ.obj_micheline =
  let rec aux (pt : PT.micheline_t) : TZ.obj_micheline =
    match pt with
    | MIstring v -> Ostring v
    | MIint    v -> Oint (Big_int.string_of_big_int v)
    | MIbytes  v -> Obytes v
    | MIprim   (prim, args, annots) -> Oprim {prim; args = List.map aux args ; annots}
    | MIseq    vs -> Oarray (List.map aux vs)
  in
  aux pt

let obj_to_micheline (obj : TZ.obj_micheline) : micheline =
  let rec aux (obj : TZ.obj_micheline) : micheline =
    match obj with
    | Oprim p -> mkprim (p.prim, List.map aux p.args, p.annots)
    | Ostring v -> mkstring v
    | Obytes v -> mkbytes (Hex.to_bytes (`Hex v))
    | Oint v -> mkint (Big_int.big_int_of_string v)
    | Oarray v -> mkseq (List.map aux v)
    | Ovar v -> begin
        let f id = mkprim (id, [], []) in
        match v with
        | OMVfree id -> f id
        | OMVint (id, _) -> f id
        | OMVstring id -> f id
        | OMVbytes id -> f id
        | OMVif (id, _, _) -> f id
      end
  in
  aux obj

let obj_to_pt (obj : TZ.obj_micheline) : PT.micheline_t =
  let mkprim (p, args, annot) = PT.MIprim (p, args, annot) in
  let rec aux (obj : TZ.obj_micheline) : PT.micheline_t =
    match obj with
    | Oprim p -> mkprim (p.prim, List.map aux p.args, p.annots)
    | Ostring v -> PT.MIstring v
    | Obytes v -> PT.MIbytes v
    | Oint v -> PT.MIint (Big_int.big_int_of_string v)
    | Oarray v -> PT.MIseq (List.map aux v)
    | Ovar v -> begin
        let f id = mkprim (id, [], []) in
        match v with
        | OMVfree id -> f id
        | OMVint (id, _) -> f id
        | OMVstring id -> f id
        | OMVbytes id -> f id
        | OMVif (id, _, _) -> f id
      end
  in
  aux obj

let obj_to_micheline_t (obj : TZ.obj_micheline) : PT.micheline_t =
  let rec aux (obj : TZ.obj_micheline) : PT.micheline_t =
    match obj with
    | Oprim p -> PT.MIprim (p.prim, List.map aux p.args, p.annots)
    | Ostring v -> PT.MIstring v
    | Obytes v -> PT.MIbytes v
    | Oint v ->  PT.MIint (Big_int.big_int_of_string v)
    | Oarray v -> PT.MIseq (List.map aux v)
    | Ovar v -> begin
        let f id = PT.MIprim (id, [], []) in
        match v with
        | OMVfree id -> f id
        | OMVint (id, _) -> f id
        | OMVstring id -> f id
        | OMVbytes id -> f id
        | OMVif (id, _, _) -> f id
       end
  in
  aux obj