-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.go
133 lines (109 loc) · 2.04 KB
/
generate.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
package main
import (
"bufio"
"os"
"strconv"
)
const (
rootFolder = "./generated/"
nn = 10004
)
func main() {
err := os.MkdirAll(rootFolder, 0777)
if err != nil {
panic(err)
}
file, err := os.Create(rootFolder + "dummy_main.go")
if err != nil {
panic(err)
}
defer file.Close()
writer := bufio.NewWriter(file)
_, err = writer.WriteString("package main\n\nfunc main() {\n}\n")
if err != nil {
panic(err)
}
defer writer.Flush()
generateStatic()
generateGenerics()
generateFunctionCalls()
}
func generateStatic() {
// generate huge number of static functions
generate(rootFolder+"static.go", "package main\n\n", "",
func(i int) string {
return `
func MaxOfType` + strconv.Itoa(i) + `(a, b int) int {
if a > b {
return a
}
return b
}
`
})
}
func generateGenerics() {
s := "package main\n\n"
s += `
type number interface {
~int | ~int32 | ~int64 | ~float32 | ~float64 | ~uint
}
`
generate(rootFolder+"generics.go", s, "",
func(i int) string {
return `
func MaxOfType` + strconv.Itoa(i) + `[T number](a, b T) T {
if a > b {
return a
}
return b
}
`
})
}
func generateFunctionCalls() {
// generate calls for every function
generate(rootFolder+"calls_main.go", "package main\n\nfunc main() {\n", "}\n",
func(i int) string {
s := strconv.Itoa(i)
return "\tMaxOfType" + s + "(" + s + ", " + s + ")\n"
})
}
func generate(fileName, begining, ending string, f func(int) string) {
file, err := os.Create(fileName)
if err != nil {
panic(err)
}
defer file.Close()
writer := bufio.NewWriter(file)
_, err = writer.WriteString(begining)
if err != nil {
panic(err)
}
defer writer.Flush()
for i := 0; i < nn; i++ {
_, err = writer.WriteString(f(i))
if err != nil {
panic(err)
}
}
_, err = writer.WriteString(ending)
if err != nil {
panic(err)
}
}
type xhf string
func (s xhf) String() string {
return ""
}
type Stringer interface {
String() string
xhf
}
func Tos[T Stringer](s []T) []string {
var ret []string
for _, v := range s {
ret = append(ret, v.String())
}
return ret
}