-
Notifications
You must be signed in to change notification settings - Fork 19
/
gen.go
205 lines (181 loc) · 5.36 KB
/
gen.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
//go:build ignore
package main
import (
"bytes"
"fmt"
"go/format"
"log"
"os"
"strings"
"text/template"
_ "embed"
)
// Inspired by sort/gen_sort_variants.go
type Variant struct {
// Package is the package name.
Package string
// Name is the variant name: should be unique among variants.
Name string
// Path is the file path into which the generator will emit the code for this
// variant.
Path string
// Imports is the imports needed for this package.
Imports string
StructPrefix string
StructPrefixLow string
StructSuffix string
ExtraFileds string
// Basic type. T or "".
Type string
// Basic type argument. [T] or "".
TypeArgument string
// TypeParam is the optional type parameter for the function.
TypeParam string // e.g. [T any]
// Funcs is a map of functions used from within the template. The following
// functions are expected to exist:
Funcs template.FuncMap
}
type TypeReplacement struct {
Type string
Desc string
}
func main() {
// For New.
base := &Variant{
Package: "skipset",
Name: "ordered",
Path: "gen_ordered.go",
Imports: "\"sync\"\n\"sync/atomic\"\n\"unsafe\"\n",
Type: "T",
TypeArgument: "[T]",
TypeParam: "[T ordered]",
StructPrefix: "Ordered",
StructPrefixLow: "ordered",
StructSuffix: "",
Funcs: template.FuncMap{
"Less": func(i, j string) string {
return fmt.Sprintf("(%s < %s)", i, j)
},
"Equal": func(i, j string) string {
return fmt.Sprintf("%s == %s", i, j)
},
},
}
generate(base)
base.Name += "Desc"
base.StructSuffix += "Desc"
base.Path = "gen_ordereddesc.go"
base.Funcs = template.FuncMap{
"Less": func(i, j string) string {
return fmt.Sprintf("(%s > %s)", i, j)
},
"Equal": func(i, j string) string {
return fmt.Sprintf("%s == %s", i, j)
},
}
generate(base)
// For NewFunc.
basefunc := &Variant{
Package: "skipset",
Name: "func",
Path: "gen_func.go",
Imports: "\"sync\"\n\"sync/atomic\"\n\"unsafe\"\n",
Type: "T",
TypeArgument: "[T]",
TypeParam: "[T any]",
ExtraFileds: "\nless func(a,b T)bool\n",
StructPrefix: "Func",
StructPrefixLow: "func",
StructSuffix: "",
Funcs: template.FuncMap{
"Less": func(i, j string) string {
return fmt.Sprintf("s.less(%s,%s)", i, j)
},
"Equal": func(i, j string) string {
return fmt.Sprintf("!s.less(%s,%s)", j, i)
},
},
}
generate(basefunc)
// For New{{Type}}.
ts := []string{"String", "Int", "Int64", "Int32", "Uint64", "Uint32", "Uint"}
for _, t := range ts {
baseType := &Variant{
Package: "skipset",
Name: "{{TypeLow}}",
Path: "gen_{{TypeLow}}.go",
Imports: "\"sync\"\n\"sync/atomic\"\n\"unsafe\"\n",
Type: "{{TypeLow}}",
TypeArgument: "",
TypeParam: "",
StructPrefix: "{{Type}}",
StructPrefixLow: "{{TypeLow}}",
StructSuffix: "",
Funcs: template.FuncMap{
"Less": func(i, j string) string {
return fmt.Sprintf("(%s < %s)", i, j)
},
"Equal": func(i, j string) string {
return fmt.Sprintf("%s == %s", i, j)
},
},
}
baseTypeDesc := &Variant{
Package: "skipset",
Name: "{{TypeLow}}Desc",
Path: "gen_{{TypeLow}}desc.go",
Imports: "\"sync\"\n\"sync/atomic\"\n\"unsafe\"\n",
Type: "{{TypeLow}}",
TypeArgument: "",
TypeParam: "",
StructPrefix: "{{Type}}",
StructPrefixLow: "{{TypeLow}}",
StructSuffix: "Desc",
Funcs: template.FuncMap{
"Less": func(i, j string) string {
return fmt.Sprintf("(%s > %s)", i, j)
},
"Equal": func(i, j string) string {
return fmt.Sprintf("%s == %s", i, j)
},
},
}
tl := strings.ToLower(t)
baseType.StructPrefix = strings.Replace(baseType.StructPrefix, "{{Type}}", t, -1)
baseType.Name = strings.Replace(baseType.Name, "{{TypeLow}}", tl, -1)
baseType.Path = strings.Replace(baseType.Path, "{{TypeLow}}", tl, -1)
baseType.Type = strings.Replace(baseType.Type, "{{TypeLow}}", tl, -1)
baseType.StructPrefixLow = strings.Replace(baseType.StructPrefixLow, "{{TypeLow}}", tl, -1)
baseTypeDesc.StructPrefix = strings.Replace(baseTypeDesc.StructPrefix, "{{Type}}", t, -1)
baseTypeDesc.Name = strings.Replace(baseTypeDesc.Name, "{{TypeLow}}", tl, -1)
baseTypeDesc.Path = strings.Replace(baseTypeDesc.Path, "{{TypeLow}}", tl, -1)
baseTypeDesc.Type = strings.Replace(baseTypeDesc.Type, "{{TypeLow}}", tl, -1)
baseTypeDesc.StructPrefixLow = strings.Replace(baseTypeDesc.StructPrefixLow, "{{TypeLow}}", tl, -1)
generate(baseType)
generate(baseTypeDesc)
}
}
// generate generates the code for variant `v` into a file named by `v.Path`.
func generate(v *Variant) {
// Parse templateCode anew for each variant because Parse requires Funcs to be
// registered, and it helps type-check the funcs.
tmpl, err := template.New("gen").Funcs(v.Funcs).Parse(templateCode)
if err != nil {
log.Fatal("template Parse:", err)
}
var out bytes.Buffer
err = tmpl.Execute(&out, v)
if err != nil {
log.Fatal("template Execute:", err)
}
os.WriteFile(v.Path, out.Bytes(), 0644)
formatted, err := format.Source(out.Bytes())
if err != nil {
log.Fatal("format:", err)
}
if err := os.WriteFile(v.Path, formatted, 0644); err != nil {
log.Fatal("WriteFile:", err)
}
}
//go:embed skipset.tpl
var templateCode string