-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigned_decimal.go
105 lines (84 loc) · 2.65 KB
/
signed_decimal.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
package main
import (
_ "embed"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
)
//go:embed signed_decimal.move.template
var signed_decimal_template string
type signedDecimalInfo struct {
BaseWidth uint
Address string
DoTest bool
Args string
Version string
Precision uint
}
type signedDecimalGenerated struct {
signedDecimalInfo
ModuleName string
TypeName string
BaseTypeName string
SignedIntType string
SignedIntModule string
DoubleWidthModule string
}
func newSignedDecimalGenerated(s *signedDecimalInfo) *signedDecimalGenerated {
r := &signedDecimalGenerated{signedDecimalInfo: *s}
r.ModuleName = fmt.Sprintf("signed_decimal%d_p%d", r.BaseWidth, r.Precision)
r.TypeName = fmt.Sprintf("SDecimal%d", r.BaseWidth)
r.BaseTypeName = fmt.Sprintf("u%d", r.BaseWidth)
r.SignedIntType = getSignedIntType(r.BaseWidth)
r.SignedIntModule = getSignedIntModuleName(r.BaseWidth)
r.DoubleWidthModule = getDoubleWidthModuleName(r.BaseWidth * 2)
return r
}
func (d *signedDecimalGenerated) GenText() (string, error) {
return GenText(d.ModuleName, signed_decimal_template, d)
}
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
}