Skip to content

Commit ce1c337

Browse files
committed
x/format: GopClass check project
1 parent a57858c commit ce1c337

File tree

2 files changed

+136
-14
lines changed

2 files changed

+136
-14
lines changed

x/format/gopclass_test.go

+114-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222
"github.com/goplus/gop/x/format"
2323
)
2424

25-
func testClass(t *testing.T, name string, pkg string, class string, entry string, src, expect string) {
25+
func testClass(t *testing.T, name string, pkg string, class string, proj bool, src, expect string) {
2626
t.Run(name, func(t *testing.T) {
27-
result, err := format.GopClassSource([]byte(src), pkg, class, entry, name)
27+
result, err := format.GopClassSource([]byte(src), pkg, class, proj, name)
2828
if err != nil {
2929
t.Fatal("format.GopClassSource failed:", err)
3030
}
@@ -35,7 +35,7 @@ func testClass(t *testing.T, name string, pkg string, class string, entry string
3535
}
3636

3737
func TestClassSpx(t *testing.T) {
38-
testClass(t, "spx class", "github.com/goplus/spx", "Calf", "Main", `package main
38+
testClass(t, "spx class", "github.com/goplus/spx", "Calf", false, `package main
3939
4040
import (
4141
"github.com/goplus/spx"
@@ -59,6 +59,35 @@ func (this *Calf) Main() {
5959
this.OnStart(func() {
6060
this.Say("Hello Go+")
6161
})
62+
//line Calf.spx:38:1
63+
this.OnMsg__1("tap", func() {
64+
//line Calf.spx:39:1
65+
for calfPlay {
66+
spx.Sched()
67+
//line Calf.spx:40:1
68+
for !(this.KeyPressed(spx.KeySpace) || this.MousePressed()) {
69+
spx.Sched()
70+
//line Calf.spx:41:1
71+
this.Wait(0.01)
72+
}
73+
//line Calf.spx:43:1
74+
calfGravity = 0.8
75+
//line Calf.spx:44:1
76+
for
77+
//line Calf.spx:44:1
78+
i := 0; i < 10;
79+
//line Calf.spx:44:1
80+
i++ {
81+
spx.Sched()
82+
//line Calf.spx:45:1
83+
this.ChangeYpos(3.5)
84+
//line Calf.spx:46:1
85+
this.Wait(0.03)
86+
}
87+
//line Calf.spx:48:1
88+
this.Wait(0.03)
89+
}
90+
})
6291
}
6392
func (this *Calf) Classfname() string {
6493
return "Calf"
@@ -83,6 +112,88 @@ func Dump() {
83112
onStart func() {
84113
say "Hello Go+"
85114
}
115+
116+
onMsg__1 "tap", func() {
117+
118+
for calfPlay {
119+
120+
for !(keyPressed(KeySpace) || mousePressed()) {
121+
122+
wait 0.01
123+
}
124+
125+
calfGravity = 0.8
126+
127+
for i := 0; i < 10; i++ {
128+
129+
changeYpos 3.5
130+
131+
wait 0.03
132+
}
133+
134+
wait 0.03
135+
}
136+
}
86137
`)
138+
}
139+
140+
func TestClassProj(t *testing.T) {
141+
testClass(t, "spx project", "github.com/goplus/spx", "Game", true, `package main
87142
143+
import "github.com/goplus/spx"
144+
import "log"
145+
146+
type Game struct {
147+
spx.Game
148+
MyAircraft MyAircraft
149+
Bullet Bullet
150+
}
151+
152+
var calfPlay = false
153+
var calfDie = false
154+
var calfGravity = 0.0
155+
//line main.spx:30:1
156+
func (this *Game) reset() {
157+
//line main.spx:31:1
158+
this.userScore = 0
159+
//line main.spx:32:1
160+
calfPlay = false
161+
//line main.spx:33:1
162+
calfDie = false
163+
//line main.spx:34:1
164+
calfGravity = 0.0
165+
}
166+
func (this *Game) MainEntry() {
167+
log.Println("MainEntry")
168+
}
169+
func (this *Game) Main() {
170+
spx.Gopt_Game_Main(this, new(Bullet), new(MyAircraft))
171+
}
172+
func main() {
173+
new(Game).Main()
174+
}
175+
`, `import "log"
176+
177+
var (
178+
MyAircraft MyAircraft
179+
Bullet Bullet
180+
)
181+
182+
var calfPlay = false
183+
var calfDie = false
184+
var calfGravity = 0.0
185+
186+
func reset() {
187+
188+
userScore = 0
189+
190+
calfPlay = false
191+
192+
calfDie = false
193+
194+
calfGravity = 0.0
195+
}
196+
197+
log.println "MainEntry"
198+
`)
88199
}

x/format/gopstyle.go

+22-11
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ func Gopstyle(file *ast.File) {
6969
formatFile(file)
7070
}
7171

72-
func GopClassSource(src []byte, pkg string, class string, entry string, filename ...string) (ret []byte, err error) {
72+
func GopClassSource(src []byte, pkg string, class string, proj bool, filename ...string) (ret []byte, err error) {
7373
var fname string
7474
if filename != nil {
7575
fname = filename[0]
7676
}
7777
fset := token.NewFileSet()
7878
var f *ast.File
79-
if f, err = parser.ParseFile(fset, fname, src, parser.ParseComments); err == nil {
80-
GopClass(f, pkg, class, entry)
79+
if f, err = parser.ParseFile(fset, fname, src, parser.AllErrors); err == nil {
80+
GopClass(f, pkg, class, proj)
8181
var buf bytes.Buffer
8282
if err = format.Node(&buf, fset, f); err == nil {
8383
ret = buf.Bytes()
@@ -87,8 +87,8 @@ func GopClassSource(src []byte, pkg string, class string, entry string, filename
8787
}
8888

8989
// GopClass format ast.File to Go+ class
90-
func GopClass(file *ast.File, pkg string, class string, entry string) {
91-
formatClass(file, pkg, class, entry)
90+
func GopClass(file *ast.File, pkg string, class string, proj bool) {
91+
formatClass(file, pkg, class, proj)
9292
}
9393

9494
func findFuncDecl(decls []ast.Decl, name string) (int, *ast.FuncDecl) {
@@ -235,7 +235,7 @@ func formatFile(file *ast.File) {
235235
}
236236
}
237237

238-
func formatClass(file *ast.File, pkg string, class string, entry string) {
238+
func formatClass(file *ast.File, pkg string, class string, proj bool) {
239239
var funcs []*ast.FuncDecl
240240
ctx := &formatCtx{
241241
imports: make(map[string]*importCtx),
@@ -257,13 +257,24 @@ func formatClass(file *ast.File, pkg string, class string, entry string) {
257257
if isClassFunc(v, class) {
258258
v.IsClass = true
259259
switch v.Name.Name {
260-
case entry:
261-
fnEntry = v
262-
file.ShadowEntry = v
263-
continue
260+
case "MainEntry":
261+
if proj {
262+
fnEntry = v
263+
file.ShadowEntry = v
264+
continue
265+
}
266+
case "Main":
267+
if !proj {
268+
fnEntry = v
269+
file.ShadowEntry = v
270+
continue
271+
}
272+
v.Shadow = true
264273
case "Classfname":
265274
v.Shadow = true
266275
}
276+
} else if v.Name.Name == "main" && proj {
277+
v.Shadow = true
267278
}
268279
case *ast.GenDecl:
269280
switch v.Tok {
@@ -309,7 +320,7 @@ func formatClass(file *ast.File, pkg string, class string, entry string) {
309320
var pkgPath = toString(spec.Path)
310321
var name string
311322
if spec.Name == nil {
312-
name = path.Base(pkgPath) // TODO: open pkgPath to get pkgName
323+
name = path.Base(pkgPath)
313324
} else {
314325
name = spec.Name.Name
315326
if name == "." || name == "_" {

0 commit comments

Comments
 (0)