-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
248 lines (208 loc) · 7.31 KB
/
cmd.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// gen-move-math
//
// move-lang (https://github.com/move-language/move) is a growing language mainly aimed at smart contract development for blockchains.
// This package provides auto-code generation for
// - signed integer.
// - double width unsigned integer u256 and u16.
// - decimal.
//
// For each type of actions, there is the base data structure (which is provided through command line),
// such as `signedMath`, and the generated data structure, such as `signedMathGenerated`. After which the struct is
// fed into a template and results concatenated.
package main
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
)
func genIntRange(start, end, step uint) []uint {
r := []uint{}
for i := start; i <= end; i = i + step {
r = append(r, i)
}
return r
}
const longDescription = `Generate missing math functions for move-lang.
The standard library and built-in types of move (https://github.com/move-language/move)
only support a subset of math functions that we generally use.
gen-move-math will generate code for
- signed math
- u16 and u256
- decimal (such as u128 based 9 digits - Decimal128N9)
- signed decimal with a dynamic decimal and a predefiend precision.
and possibly more.
`
func newSignedMathCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "gen-move-math",
Short: "generate missing math functions before official move-lang support matures.",
Args: cobra.NoArgs,
Long: longDescription,
}
output := "./sources/signed_math.move"
address := "more_math"
widths := newIntWidth([]uint{8, 64, 128})
doTest := false
cmd.Flags().StringVarP(&output, "out", "o", output, "output file. this should be the in the sources folder of your move package module")
cmd.MarkFlagFilename("out")
cmd.Flags().StringVarP(&address, "address", "p", address, "(named) address")
cmd.Flags().VarP(widths, "width", "w", fmt.Sprintf("int widths, must be one of %s. can be specified multiple times.", sliceToString(widths.permitted)))
cmd.Flags().BoolVarP(&doTest, "do-test", "t", false, "generate test code")
cmd.Run = func(cmd *cobra.Command, args []string) {
allTexts := []string{}
for _, w := range widths.values {
s := signedMath{
BaseWidth: w,
Address: address,
DoTest: doTest,
Args: strings.Join(os.Args[1:], " "),
Version: version,
}
code, err := newSignedMathGenerated(&s).GenText()
if err != nil {
panic(err)
}
allTexts = append(allTexts, code)
}
os.WriteFile(output, []byte(strings.Join(allTexts, "\n")), 0o666)
}
return cmd
}
func newDoubleWidthUnsignedCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "double-width",
Short: "generate double width unsigned integer (for example, Uint16 based on u8)",
Args: cobra.NoArgs,
}
output := "./sources/double_width_unsigned.move"
address := "more_math"
widths := newIntWidth([]uint{16, 128, 256})
doTest := false
cmd.Flags().StringVarP(&output, "out", "o", output, "output file. this should be the in the sources folder of your move package module")
cmd.MarkFlagFilename("out")
cmd.Flags().StringVarP(&address, "address", "p", address, "(named) address")
cmd.Flags().VarP(widths, "width", "w", fmt.Sprintf("int widths, must be one of %s. can be specified multiple times.", sliceToString(widths.permitted)))
cmd.Flags().BoolVarP(&doTest, "do-test", "t", false, "generate test code")
cmd.Run = func(cmd *cobra.Command, args []string) {
allTexts := []string{}
for _, w := range widths.values {
s := doubleWidthUnsigned{
DesiredWidth: w,
Address: address,
DoTest: doTest,
Args: strings.Join(os.Args[1:], " "),
Version: version,
}
code, err := newDoubleWidthUnsignedGenerated(&s).GenText()
if err != nil {
panic(err)
}
allTexts = append(allTexts, code)
}
os.WriteFile(output, []byte(strings.Join(allTexts, "\n")), 0o666)
}
return cmd
}
func newDecimalCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "decimal",
Short: "generate decimal based on unsigned integer and a predefined decimal place",
Args: cobra.NoArgs,
}
output := "./sources/decimal.move"
moduleNameFmt := "decimal%dn%d"
address := "more_math"
widths := newIntWidth([]uint{64, 128})
doTest := false
decimals := newIntWidth(genIntRange(1, 18, 1))
decimals.values = []uint{5, 6, 7, 8, 9, 10, 18}
cmd.Flags().StringVarP(&moduleNameFmt, "module", "m", moduleNameFmt, "module name string for decimal type")
cmd.Flags().StringVarP(&output, "out", "o", output, "output file. this should be the in the sources folder of your move package module")
cmd.MarkFlagFilename("out")
cmd.Flags().StringVarP(&address, "address", "p", address, "(named) address")
cmd.Flags().VarP(widths, "width", "w", fmt.Sprintf("int widths, must be one of %s. can be specified multiple times.", sliceToString(widths.permitted)))
cmd.Flags().BoolVarP(&doTest, "do-test", "t", false, "generate test code")
cmd.Flags().VarP(decimals, "decimal", "d", "decimal")
cmd.Run = func(*cobra.Command, []string) {
allTexts := []string{}
for _, w := range widths.values {
for _, decimal := range decimals.values {
if w == 64 && decimal >= 10 {
continue
}
d := decimalInfo{
DoTest: doTest,
BaseWidth: w,
ModuleNameFmt: moduleNameFmt,
Address: address,
Decimal: decimal,
Args: strings.Join(os.Args[1:], " "),
Version: version,
}
code, err := newDecimalGenerated(&d).GenText()
if err != nil {
panic(err)
}
allTexts = append(allTexts, code)
}
}
os.WriteFile(output, []byte(strings.Join(allTexts, "\n")), 0o666)
}
return cmd
}
func newSignedDecimalCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "signed-decimal",
Short: "generate signed decimal based on signed integer",
Args: cobra.NoArgs,
}
output := "./sources/signed_decimal.move"
address := "more_math"
widths := newIntWidth([]uint{64, 128})
doTest := false
precision := newIntWidth(genIntRange(6, 34, 2))
precision.values = genIntRange(12, 18, 2)
cmd.Flags().StringVarP(&output, "out", "o", output, "output file. this should be the in the sources folder of your move package module")
cmd.MarkFlagFilename("out")
cmd.Flags().StringVarP(&address, "address", "p", address, "(named) address")
cmd.Flags().VarP(widths, "width", "w", fmt.Sprintf("int widths, must be one of %s. can be specified multiple times.", sliceToString(widths.permitted)))
cmd.Flags().BoolVarP(&doTest, "do-test", "t", false, "generate test code")
cmd.Flags().Var(precision, "precision", "precision")
cmd.Run = func(*cobra.Command, []string) {
allTexts := []string{}
for _, w := range widths.values {
for _, precision := range precision.values {
if w == 64 && precision >= 6 {
continue
}
d := signedDecimalInfo{
DoTest: doTest,
BaseWidth: w,
Address: address,
Precision: precision,
Args: strings.Join(os.Args[1:], " "),
Version: version,
}
code, err := newSignedDecimalGenerated(&d).GenText()
if err != nil {
panic(err)
}
allTexts = append(allTexts, code)
}
}
os.WriteFile(output, []byte(strings.Join(allTexts, "\n")), 0o666)
}
return cmd
}
func main() {
// signed math method is the root,
// since it's the first one developed.
rootCmd := newSignedMathCmd()
rootCmd.AddCommand(
newDoubleWidthUnsignedCmd(),
newDecimalCmd(),
newSignedDecimalCmd(),
)
rootCmd.Execute()
}