/
constants_generation.go
167 lines (144 loc) · 4.44 KB
/
constants_generation.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
package internal
import (
"fmt"
"sort"
"strconv"
"strings"
)
type constantDescription struct {
typeName string
constructor string
}
func constantsV1() map[string]constantDescription {
return map[string]constantDescription{
"tx": {constructor: "newTx"},
"unit": {constructor: "newUnit"},
"height": {constructor: "newHeight"},
}
}
func constantsV2() map[string]constantDescription {
c := constantsV1()
c["Sell"] = constantDescription{"Sell", ""}
c["Buy"] = constantDescription{"Buy", ""}
c["CEILING"] = constantDescription{"Ceiling", ""}
c["FLOOR"] = constantDescription{"Floor", ""}
c["HALFEVEN"] = constantDescription{"HalfEven", ""}
c["DOWN"] = constantDescription{"Down", ""}
c["UP"] = constantDescription{"Up", ""}
c["HALFUP"] = constantDescription{"HalfUp", ""}
c["HALFDOWN"] = constantDescription{"HalfDown", ""}
c["nil"] = constantDescription{constructor: "newNil"}
return c
}
func constantsV3() map[string]constantDescription {
c := constantsV2()
c["NOALG"] = constantDescription{"NoAlg", ""}
c["MD5"] = constantDescription{"Md5", ""}
c["SHA1"] = constantDescription{"Sha1", ""}
c["SHA224"] = constantDescription{"Sha224", ""}
c["SHA256"] = constantDescription{"Sha256", ""}
c["SHA384"] = constantDescription{"Sha384", ""}
c["SHA512"] = constantDescription{"Sha512", ""}
c["SHA3224"] = constantDescription{"Sha3224", ""}
c["SHA3256"] = constantDescription{"Sha3256", ""}
c["SHA3384"] = constantDescription{"Sha3384", ""}
c["SHA3512"] = constantDescription{"Sha3512", ""}
c["this"] = constantDescription{constructor: "newThis"}
c["lastBlock"] = constantDescription{constructor: "newLastBlock"}
return c
}
func constantsV4() map[string]constantDescription {
return constantsV3()
}
func constantsV5() map[string]constantDescription {
c := constantsV4()
delete(c, "UP")
delete(c, "HALFDOWN")
return c
}
func constantsV6() map[string]constantDescription {
return constantsV5()
}
func constantsV7() map[string]constantDescription {
return constantsV6()
}
func createConstants(cd *Coder, ver string, c map[string]constantDescription) {
keys := make([]string, 0, len(c))
for k := range c {
keys = append(keys, k)
}
sort.Strings(keys)
qks := make([]string, len(keys))
for i, k := range keys {
qks[i] = strconv.Quote(k)
}
cd.Line("var Constants%s = []string{%s}", ver, strings.Join(qks, ", "))
cd.Line("const _constants_%s = \"%s\"", ver, strings.Join(keys, ""))
constructors := make([]string, len(keys))
for i, k := range keys {
if c[k].constructor == "" {
constructors[i] = fmt.Sprintf("new%s", c[k].typeName)
} else {
constructors[i] = c[k].constructor
}
}
cd.Line("var _constructors_%s = [...]rideConstructor{%s}", ver, strings.Join(constructors, ", "))
idx := 0
positions := make([]string, len(keys))
for i, k := range keys {
idx += len(k)
positions[i] = strconv.Itoa(idx)
}
cd.Line("var _c_index_%s = [...]int{0, %s}", ver, strings.Join(positions, ", "))
cd.Line("")
cd.Line("func constant%s(id int) rideConstructor {", ver)
cd.Line("if id < 0 || id > %d {", len(keys)-1)
cd.Line("return nil")
cd.Line("}")
cd.Line("return _constructors_%s[id]", ver)
cd.Line("}")
cd.Line("")
cd.Line("func checkConstant%s(name string) (uint16, bool) {", ver)
cd.Line("for i := 0; i <= %d; i++ {", len(keys)-1)
cd.Line("if _constants_%s[_c_index_%s[i]:_c_index_%s[i+1]] == name {", ver, ver, ver)
cd.Line("return uint16(i), true")
cd.Line("}")
cd.Line("}")
cd.Line("return 0, false")
cd.Line("}")
cd.Line("")
}
func createConstructors(cd *Coder, c map[string]constantDescription) {
keys := make([]string, 0, len(c))
for k := range c {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if c[k].constructor == "" {
tn := c[k].typeName
cd.Line("func new%s(environment) rideType {", tn)
cd.Line("return rideNamedType{name: \"%s\"}", tn)
cd.Line("}")
cd.Line("")
cd.Line("func create%s(_ environment, _ ...rideType) (rideType, error) {", tn)
cd.Line("return rideNamedType{name: \"%s\"}, nil", tn)
cd.Line("}")
cd.Line("")
}
}
}
func GenerateConstants(fn string) {
cd := NewCoder("ride")
createConstants(cd, "V1", constantsV1())
createConstants(cd, "V2", constantsV2())
createConstants(cd, "V3", constantsV3())
createConstants(cd, "V4", constantsV4())
createConstants(cd, "V5", constantsV5())
createConstants(cd, "V6", constantsV6())
createConstants(cd, "V7", constantsV7())
createConstructors(cd, constantsV4())
if err := cd.Save(fn); err != nil {
panic(err)
}
}