forked from mumax/mumax.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apigen.go
235 lines (206 loc) · 4.5 KB
/
apigen.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
// Automatic generation of api.html based on template.
import (
"github.com/mumax/3/cuda"
"github.com/mumax/3/engine"
"io/ioutil"
"os"
"reflect"
"sort"
"strings"
"text/template"
"unicode"
)
var (
api_entries entries
api_ident = make(map[string]entry)
)
type entry struct {
name string
Type reflect.Type
Doc string
touched bool
}
func buildAPI() {
cuda.Init(0) // gpu 0
ident := engine.World.Identifiers
doc := engine.World.Doc
e := make(entries, 0, len(ident))
for K, v := range doc {
k := strings.ToLower(K)
t := ident[k].Type()
entr := entry{K, t, v, false}
e = append(e, &entr)
api_ident[k] = entr
}
sort.Sort(&e)
api_entries = e
}
func (e *entry) Name() string {
return e.name
}
// input parameters
func (e *entry) Ins() string {
t := e.Type.String()
if strings.HasPrefix(t, "func(") {
return cleanType(t[len("func"):])
} else {
return ""
}
}
// dumbed-down type
func cleanType(typ string) string {
typ = strings.Replace(typ, "engine.", "", -1)
typ = strings.Replace(typ, "*data.", "", -1)
typ = strings.Replace(typ, "script.", "", -1)
return typ
}
func (e *entry) Methods() []string {
t := e.Type
// if it's a function, we list the methods on the output type
if t.Kind() == reflect.Func && t.NumOut() == 1 {
t = t.Out(0)
}
nm := t.NumMethod()
M := make([]string, 0, nm)
for i := 0; i < nm; i++ {
m := t.Method(i)
n := m.Name
if unicode.IsUpper(rune(n[0])) && !hidden(n) {
var args string
for i := 1; i < m.Type.NumIn(); i++ {
args += cleanType(m.Type.In(i).String()) + " "
}
M = append(M, n+"( "+args+")")
}
}
return M
}
// return value
func (e *entry) Ret() string {
t := e.Type
if t.Kind() == reflect.Func && t.NumOut() == 1 {
return cleanType(t.Out(0).String())
} else {
return ""
}
}
// hidden methods
func hidden(name string) bool {
switch name {
default:
return false
case "Eval", "InputType", "Type", "Slice", "Name", "Unit", "NComp", "Mesh", "SetValue", "String":
return true
}
}
// list of examples where entry is used.
func (e *entry) Examples() []int {
return api_examples[strings.ToLower(e.name)]
}
type api struct {
Entries entries
}
// include file
func (e *api) Include(fname string) string {
b, err := ioutil.ReadFile(fname)
check(err)
return string(b)
}
// list of entries not used so far
func (a *api) remaining() []*entry {
var E []*entry
for _, e := range a.Entries {
if !e.touched {
E = append(E, e)
}
}
return E
}
// return all entries, unused so far, which have given type.
func (a *api) FilterType(typ ...string) []*entry {
var E []*entry
for _, e := range a.remaining() {
for _, t := range typ {
if match(t, e.Type.String()) &&
!strings.HasPrefix(e.name, "ext_") {
e.touched = true
E = append(E, e)
}
}
}
return E
}
// return all entries, unused so far, which have given return type.
func (a *api) FilterReturn(typ ...string) []*entry {
var E []*entry
for _, e := range a.remaining() {
for _, t := range typ {
if match(t, e.Ret()) &&
!strings.HasPrefix(e.name, "ext_") {
e.touched = true
E = append(E, e)
}
}
}
return E
}
// return all entries, unused so far, which have given name.
func (a *api) FilterName(typ ...string) []*entry {
var E []*entry
for _, e := range a.remaining() {
for _, t := range typ {
if match(t, e.name) &&
!strings.HasPrefix(e.name, "ext_") {
e.touched = true
E = append(E, e)
}
}
}
return E
}
// return all entries, unused so far, whose name starts with prefix.
func (a *api) FilterPrefix(pre string) []*entry {
var E []*entry
for _, e := range a.remaining() {
if strings.HasPrefix(e.name, pre) {
e.touched = true
E = append(E, e)
}
}
return E
}
// return all entries not yet used.
func (a *api) FilterLeftovers() []*entry {
return a.remaining()
}
// case insensitive match.
func match(a, b string) bool {
a = strings.ToLower(a)
b = strings.ToLower(b)
match := a == b
return match
}
func renderAPI() {
e := api_entries
t := template.Must(template.New("api").Parse(templ))
f, err2 := os.OpenFile("api.html", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
check(err2)
check(t.Execute(f, &api{e}))
}
var templ = read("api-template.html")
func read(fname string) string {
b, err := ioutil.ReadFile(fname)
check(err)
return string(b)
}
type entries []*entry
func (e *entries) Len() int {
return len(*e)
}
func (e *entries) Less(i, j int) bool {
return strings.ToLower((*e)[i].name) < strings.ToLower((*e)[j].name)
}
func (e *entries) Swap(i, j int) {
(*e)[i], (*e)[j] = (*e)[j], (*e)[i]
}