This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
builder.go
155 lines (128 loc) · 3.04 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
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package micheline
import (
"math/big"
"blockwatch.cc/tzgo/tezos"
)
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 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 NewBytes(b []byte) Prim {
return Prim{Type: PrimBytes, Bytes: b}
}
func NewString(s string) Prim {
return Prim{Type: PrimString, String: s}
}
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 {
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 (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))),
))
}