-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigned_math.go
117 lines (92 loc) · 2.64 KB
/
signed_math.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
package main
import (
_ "embed"
"fmt"
"math/big"
"os"
"strings"
"github.com/spf13/cobra"
)
//go:embed signed_math.move.template
var signed_math_template string
var one *big.Int = big.NewInt(1)
type signedMath struct {
BaseWidth uint
Address string
DoTest bool
Args string
Version string
}
type signedMathGenerated struct {
signedMath
BaseTypeName string
BreakPoint string
MaxUnsigned string
MaxPositive string
ModuleName string
TypeName string
}
func getSignedIntModuleName(baseWidth uint) string {
return fmt.Sprintf("int%d", baseWidth)
}
func getSignedIntType(baseWidth uint) string {
return fmt.Sprintf("Int%d", baseWidth)
}
func newSignedMathGenerated(s *signedMath) *signedMathGenerated {
r := &signedMathGenerated{
signedMath: *s,
}
r.BaseTypeName = fmt.Sprintf("u%d", s.BaseWidth)
break_point := big.NewInt(0)
break_point.Lsh(one, s.BaseWidth-1)
r.BreakPoint = break_point.String()
max_unsigned := big.NewInt(0)
max_unsigned.Lsh(one, s.BaseWidth)
max_unsigned.Sub(max_unsigned, one)
r.MaxUnsigned = max_unsigned.String()
max_positive := big.NewInt(0)
max_positive.Lsh(one, s.BaseWidth-1)
max_positive.Sub(max_positive, one)
r.MaxPositive = max_positive.String()
r.ModuleName = getSignedIntModuleName(s.BaseWidth)
r.TypeName = getSignedIntType(s.BaseWidth)
return r
}
func (s signedMathGenerated) GenText() (string, error) {
return GenText(s.ModuleName, signed_math_template, s)
}
func newSignedMathCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "signed-math",
Short: "",
Args: cobra.NoArgs,
}
output := "./sources/signed_math.move"
address := "more_math"
widths := newIntWidth([]uint{8, 16, 32, 64, 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 := 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
}