-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
182 lines (152 loc) · 5.73 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
// 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 (
_ "embed"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
)
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)
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"
moduleNameFmt := "int%d"
address := "more_math"
widths := newIntWidth([]uint{8, 64, 128})
doTest := false
cmd.Flags().StringVarP(&moduleNameFmt, "module", "m", moduleNameFmt, "module name string for signed integers")
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,
ModuleNameFmt: moduleNameFmt,
Address: address,
DoTest: doTest,
}
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"
moduleNameFmt := "uint%d"
address := "more_math"
widths := newIntWidth([]uint{16, 128, 256})
doTest := false
cmd.Flags().StringVarP(&moduleNameFmt, "module", "m", moduleNameFmt, "module name string for double width unsigned integers")
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,
ModuleNameFmt: moduleNameFmt,
Address: address,
DoTest: doTest,
}
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([]uint{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18})
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,
}
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 main() {
// signed math method is the root,
// since it's the first one developed.
rootCmd := newSignedMathCmd()
rootCmd.AddCommand(newDoubleWidthUnsignedCmd(), newDecimalCmd())
rootCmd.Execute()
}