-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.go
231 lines (218 loc) · 4.86 KB
/
class.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
package main
import (
"bytes"
"fmt"
"go/ast"
"go/token"
"log"
"os"
"path/filepath"
"strings"
goformat "go/format"
xformat "github.com/goplus/gop/x/format"
"github.com/goplus/mod/modfile"
)
type class struct {
name string
cls string
ext string
vars *ast.GenDecl
fns []*ast.FuncDecl
proj bool
}
type context struct {
fset *token.FileSet
proj *modfile.Project
pkg string
classes map[string]*class
imports []*ast.GenDecl
otherDecl []*ast.GenDecl
otherFunc []*ast.FuncDecl
}
func newContext(fset *token.FileSet, proj *modfile.Project) *context {
var pkg string
if ar := strings.Split(proj.PkgPaths[0], "/"); len(ar) > 0 {
pkg = ar[len(ar)-1]
}
return &context{fset: fset, proj: proj, pkg: pkg, classes: make(map[string]*class)}
}
func typIdent(typ ast.Expr) (string, bool) {
if star, ok := typ.(*ast.StarExpr); ok {
typ = star.X
}
if sel, ok := typ.(*ast.SelectorExpr); ok {
if id, ok := typIdent(sel.X); ok {
return id + "." + sel.Sel.Name, true
}
}
if ident, ok := typ.(*ast.Ident); ok {
return ident.Name, true
}
return "", false
}
func (c *context) isClass(name string) (proj bool, cls string, ext string) {
if name == c.pkg+"."+c.proj.Class {
proj = true
cls = c.proj.Class
ext = c.proj.Ext
return
}
for _, work := range c.proj.Works {
if name == c.pkg+"."+work.Class {
cls = work.Class
ext = work.Ext
return
}
}
return
}
// find class in type spec
func (c *context) findClass(spec *ast.TypeSpec) (cls *class) {
st, ok := spec.Type.(*ast.StructType)
if !ok {
return
}
for i, fs := range st.Fields.List {
if len(fs.Names) == 0 {
if name, ok := typIdent(fs.Type); ok {
proj, clsname, ext := c.isClass(name)
if i == 0 && clsname != "" {
cls = &class{proj: proj, cls: clsname, ext: ext}
if proj {
cls.name = "main"
} else {
cls.name = spec.Name.Name
}
}
continue
}
} else if cls != nil {
if cls.vars == nil {
cls.vars = &ast.GenDecl{Tok: token.VAR}
}
cls.vars.Specs = append(cls.vars.Specs, &ast.ValueSpec{Names: fs.Names, Type: fs.Type})
}
}
return
}
func (c *context) parseFile(f *ast.File) {
for _, decl := range f.Decls {
if d, ok := decl.(*ast.GenDecl); ok {
switch d.Tok {
case token.IMPORT:
c.imports = append(c.imports, d)
continue
case token.CONST:
// skip const _ = true
if spec, ok := d.Specs[0].(*ast.ValueSpec); ok && spec.Names[0].Name == "_" {
continue
}
case token.TYPE:
if spec, ok := d.Specs[0].(*ast.TypeSpec); ok {
if cls := c.findClass(spec); cls != nil {
c.classes[spec.Name.Name] = cls
continue
}
}
}
c.otherDecl = append(c.otherDecl, d)
}
}
for _, decl := range f.Decls {
if d, ok := decl.(*ast.FuncDecl); ok {
if d.Name.Name == "main" {
continue
}
if d.Recv != nil && len(d.Recv.List) == 1 {
typ := d.Recv.List[0].Type
if star, ok := typ.(*ast.StarExpr); ok {
typ = star.X
}
if name, ok := typ.(*ast.Ident); ok {
if cls, ok := c.classes[name.Name]; ok {
d.Recv = nil
cls.fns = append(cls.fns, d)
continue
}
}
}
c.otherFunc = append(c.otherFunc, d)
}
}
}
func (c *context) output(dir string, out string) error {
if out == "" {
for _, cls := range c.classes {
code := c.code(cls)
fmt.Println("====", "class:", cls.name+cls.ext, "====")
fmt.Println(code)
}
} else {
if !filepath.IsAbs(out) {
out = filepath.Join(dir, out)
}
if err := os.MkdirAll(out, 0755); err != nil {
return err
}
for _, cls := range c.classes {
code := c.code(cls)
fname := filepath.Join(out, cls.name+cls.ext)
fmt.Println("export", fname)
err := os.WriteFile(fname, []byte(code), 0644)
if err != nil {
return fmt.Errorf("write file %v error: %w", fname, err)
}
}
}
return nil
}
func (c *context) code(cls *class) string {
var buf bytes.Buffer
if cls.vars != nil {
goformat.Node(&buf, c.fset, cls.vars)
buf.Write([]byte{'\n'})
}
// output others decls
if cls.proj {
for _, decl := range c.otherDecl {
goformat.Node(&buf, c.fset, decl)
buf.WriteByte('\n')
}
for _, decl := range c.otherFunc {
goformat.Node(&buf, c.fset, decl)
buf.WriteByte('\n')
}
}
var body *ast.BlockStmt
for _, fn := range cls.fns {
switch fn.Name.Name {
case "Main":
if !cls.proj {
body = fn.Body
}
continue
case "MainEntry":
if cls.proj {
body = fn.Body
}
continue
case "Classfname":
continue
}
goformat.Node(&buf, c.fset, fn)
buf.WriteByte('\n')
}
if body != nil {
goformat.Node(&buf, c.fset, body.List)
buf.WriteByte('\n')
}
// remove sched
data := strings.ReplaceAll(buf.String(), c.pkg+".Sched()", "")
code, err := xformat.GopstyleSource([]byte(data), cls.name+cls.ext)
if err != nil {
log.Panicln(err)
}
r := strings.NewReplacer("this.", "", c.pkg+".", "", "\n\n", "\n")
src := r.Replace(string(code))
return src
}