/
builder.go
193 lines (160 loc) · 3.74 KB
/
builder.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package micheline
import (
"math/big"
"sort"
"blockwatch.cc/tzgo/tezos"
)
var (
Unit = NewCode(D_UNIT)
)
func NewCode(c OpCode, args ...Prim) Prim {
var typ PrimType
switch len(args) {
case 0:
typ = PrimNullary
case 1:
typ = PrimUnary
case 2:
typ = PrimBinary
default:
typ = PrimVariadicAnno
}
return Prim{Type: typ, OpCode: c, Args: args}
}
func NewCodeAnno(c OpCode, anno string, args ...Prim) Prim {
p := NewCode(c, args...)
p.Anno = []string{anno}
if p.Type != PrimVariadicAnno {
p.Type++
}
return p
}
func NewSeq(args ...Prim) Prim {
return Prim{Type: PrimSequence, Args: args}
}
func NewInt64(i int64) Prim {
return NewBig(big.NewInt(i))
}
func NewZ(z tezos.Z) Prim {
return NewBig(new(big.Int).Set(z.Big()))
}
func NewMutez(n tezos.N) Prim {
return NewBig(big.NewInt(int64(n)))
}
func NewBig(i *big.Int) Prim {
return Prim{Type: PrimInt, Int: i}
}
func NewNat(i *big.Int) Prim {
return Prim{Type: PrimInt, Int: i}
}
func NewKeyHash(a tezos.Address) Prim {
return NewBytes(a.Encode())
}
func NewAddress(a tezos.Address) Prim {
return NewBytes(a.EncodePadded())
}
func NewBytes(b []byte) Prim {
return Prim{Type: PrimBytes, Bytes: b}
}
func NewString(s string) Prim {
return Prim{Type: PrimString, String: s}
}
func NewOption(p ...Prim) Prim {
if len(p) == 0 {
return Prim{Type: PrimNullary, OpCode: D_NONE}
}
return Prim{Type: PrimUnary, OpCode: D_SOME, Args: []Prim{p[0]}}
}
func NewPairType(l, r Prim, anno ...string) Prim {
typ := PrimBinary
if len(anno) > 0 {
typ = PrimBinaryAnno
}
return Prim{Type: typ, OpCode: T_PAIR, Args: []Prim{l, r}, Anno: anno}
}
func NewMapType(k, v Prim, anno ...string) Prim {
typ := PrimBinary
if len(anno) > 0 {
typ = PrimBinaryAnno
}
return Prim{Type: typ, OpCode: T_MAP, Args: []Prim{k, v}, Anno: anno}
}
func NewMap(elts ...Prim) Prim {
sort.Slice(elts, func(i, j int) bool {
return elts[i].Args[0].Compare(elts[j].Args[0]) <= 0
})
return Prim{Type: PrimSequence, Args: elts}
}
func NewMapElem(k, v Prim) Prim {
return Prim{Type: PrimBinary, OpCode: D_ELT, Args: []Prim{k, v}}
}
func NewSetType(e Prim, anno ...string) Prim {
typ := PrimUnary
if len(anno) > 0 {
typ = PrimUnaryAnno
}
return Prim{Type: typ, OpCode: T_SET, Args: []Prim{e}, Anno: anno}
}
func NewOptType(e Prim, anno ...string) Prim {
typ := PrimUnary
if len(anno) > 0 {
typ = PrimUnaryAnno
}
return Prim{Type: typ, OpCode: T_OPTION, Args: []Prim{e}, Anno: anno}
}
func NewPair(l, r Prim) Prim {
return Prim{Type: PrimBinary, OpCode: D_PAIR, Args: []Prim{l, r}}
}
func NewCombPair(contents ...Prim) Prim {
return Prim{Type: PrimSequence, Args: contents}
}
func NewCombPairType(contents ...Prim) Prim {
return Prim{Type: PrimSequence, OpCode: T_PAIR, Args: contents}
}
func NewPrim(c OpCode, anno ...string) Prim {
typ := PrimNullary
if len(anno) > 0 {
typ = PrimNullaryAnno
}
return Prim{Type: typ, OpCode: c, Anno: anno}
}
func NewUnion(path []int, prim Prim) Prim {
if len(path) == 0 {
return prim
}
oc := D_LEFT
if path[0] == 1 {
oc = D_RIGHT
}
return NewCode(oc, NewUnion(path[1:], prim))
}
func (p Prim) WithAnno(anno string) Prim {
p.Anno = append(p.Anno, anno)
return p
}
// Macros
func ASSERT_CMPEQ() Prim {
return NewSeq(
NewSeq(NewCode(I_COMPARE), NewCode(I_EQ)),
NewCode(I_IF, NewSeq(), NewSeq(NewSeq(NewCode(I_UNIT), NewCode(I_FAILWITH)))),
)
}
func DUUP() Prim {
return NewSeq(NewCode(I_DIP, NewSeq(NewCode(I_DUP)), NewCode(I_SWAP)))
}
func IFCMPNEQ(left, right Prim) Prim {
return NewSeq(
NewCode(I_COMPARE),
NewCode(I_EQ),
NewCode(I_IF, left, right),
)
}
func UNPAIR() Prim {
return NewSeq(NewSeq(
NewCode(I_DUP),
NewCode(I_CAR),
NewCode(I_DIP, NewSeq(NewCode(I_CDR))),
))
}