Skip to content

Commit

Permalink
spxClass: Classfname() string
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Mar 8, 2024
1 parent 998c3e4 commit 1abc2bc
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 16 deletions.
10 changes: 5 additions & 5 deletions cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,12 @@ func TestMarkAutogen(t *testing.T) {
}

func TestClassNameAndExt(t *testing.T) {
name, ext := ClassNameAndExt("/foo/bar.abc_yap.gox")
if name != "bar_abc" || ext != "_yap.gox" {
name, clsfile, ext := ClassNameAndExt("/foo/bar.abc_yap.gox")
if name != "bar_abc" || clsfile != "bar.abc" || ext != "_yap.gox" {
t.Fatal("classNameAndExt:", name, ext)
}
name, ext = ClassNameAndExt("/foo/get_bar_:id.yap")
if name != "get_bar_id" || ext != ".yap" {
name, clsfile, ext = ClassNameAndExt("/foo/get_bar_:id.yap")
if name != "get_bar_id" || clsfile != "get_bar_:id" || ext != ".yap" {
t.Fatal("classNameAndExt:", name, ext)
}
}
Expand Down Expand Up @@ -451,7 +451,7 @@ func TestGmxProject(t *testing.T) {
pkg := gox.NewPackage("", "foo", goxConf)
ctx := &pkgCtx{
projs: make(map[string]*gmxProject),
classes: make(map[*ast.File]gmxClass),
classes: make(map[*ast.File]*gmxClass),
}
gmx := loadClass(ctx, pkg, "main.t2gmx", &ast.File{IsProj: true}, &Config{
LookupClass: lookupClass,
Expand Down
16 changes: 9 additions & 7 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import (
// -----------------------------------------------------------------------------

type gmxClass struct {
tname string // class type
ext string
proj *gmxProject
tname string // class type
clsfile string
ext string
proj *gmxProject
}

type gmxProject struct {
Expand Down Expand Up @@ -70,9 +71,10 @@ func (p *gmxProject) getScheds(cb *gox.CodeBuilder) []goast.Stmt {
return p.schedStmts
}

func ClassNameAndExt(file string) (name, ext string) {
func ClassNameAndExt(file string) (name, clsfile, ext string) {
fname := filepath.Base(file)
name, ext = modfile.SplitFname(fname)
clsfile, ext = modfile.SplitFname(fname)
name = clsfile
if strings.ContainsAny(name, ":.") {
name = strings.NewReplacer(":", "", ".", "_").Replace(name)
}
Expand All @@ -84,7 +86,7 @@ func isGoxTestFile(ext string) bool {
}

func loadClass(ctx *pkgCtx, pkg *gox.Package, file string, f *ast.File, conf *Config) *gmxProject {
tname, ext := ClassNameAndExt(file)
tname, clsfile, ext := ClassNameAndExt(file)
gt, ok := conf.LookupClass(ext)
if !ok {
panic("TODO: class not found")
Expand Down Expand Up @@ -138,7 +140,7 @@ func loadClass(ctx *pkgCtx, pkg *gox.Package, file string, f *ast.File, conf *Co
} else {
p.sptypes = append(p.sptypes, tname)
}
ctx.classes[f] = gmxClass{tname, ext, p}
ctx.classes[f] = &gmxClass{tname, clsfile, ext, p}
if debugLoad {
log.Println("==> InitClass", tname, "isProj:", f.IsProj)
}
Expand Down
34 changes: 30 additions & 4 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func doInitMethods(ld *typeLoader) {
type pkgCtx struct {
*nodeInterp
projs map[string]*gmxProject // .gmx => project
classes map[*ast.File]gmxClass
classes map[*ast.File]*gmxClass
fset *token.FileSet
cpkgs *cpackages.Importer
syms map[string]loader
Expand Down Expand Up @@ -491,7 +491,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
fset: fset,
nodeInterp: interp,
projs: make(map[string]*gmxProject),
classes: make(map[*ast.File]gmxClass),
classes: make(map[*ast.File]*gmxClass),
syms: make(map[string]loader),
generics: make(map[string]bool),
}
Expand Down Expand Up @@ -718,6 +718,7 @@ func genGoFile(file string, goxTestFile bool) string {

func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, conf *Config) {
var proj *gmxProject
var c *gmxClass
var classType string
var testType string
var baseTypeName string
Expand All @@ -727,12 +728,12 @@ func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, con
var parent = ctx.pkgCtx
if f.IsClass {
if f.IsNormalGox {
classType, _ = ClassNameAndExt(file)
classType, _, _ = ClassNameAndExt(file)
if classType == "main" {
classType = "_main"
}
} else {
c := parent.classes[f]
c = parent.classes[f]
classType = c.tname
proj, ctx.proj = c.proj, c.proj
ctx.autoimps = proj.autoimps
Expand Down Expand Up @@ -851,6 +852,31 @@ func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, con
X: &ast.Ident{Name: classType},
},
}}}
// func Classfname() string
if spxClass {
f.Decls = append(f.Decls, &ast.FuncDecl{
Name: &ast.Ident{
Name: "Classfname",
},
Type: &ast.FuncType{
Params: &ast.FieldList{},
Results: &ast.FieldList{
List: []*ast.Field{
{Type: &ast.Ident{Name: "string"}},
},
},
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.ReturnStmt{
Results: []ast.Expr{
&ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(c.clsfile)},
},
},
},
},
})
}
}
if d := f.ShadowEntry; d != nil {
d.Name.Name = getEntrypoint(f)
Expand Down
72 changes: 72 additions & 0 deletions cl/compile_spx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ func (this *Kai) onMsg(msg string) {
this.Say("Hi")
}
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.tgmx", "Kai.tspx")
}

Expand Down Expand Up @@ -250,6 +253,9 @@ func (this *index) onInit() {
this.bar()
fmt.Println("Hi")
}
func (this *bar) Classfname() string {
return "bar"
}
`)
}

Expand Down Expand Up @@ -278,6 +284,9 @@ func (this *index) MainEntry() {
func main() {
spx.Gopt_MyGame_Main(new(index))
}
func (this *bar) Classfname() string {
return "bar"
}
`)
}

Expand Down Expand Up @@ -314,6 +323,9 @@ func (this *index) MainEntry() {
func main() {
spx.Gopt_MyGame_Main(new(index))
}
func (this *bar) Classfname() string {
return "bar"
}
`)
}

Expand Down Expand Up @@ -359,6 +371,9 @@ func (this *bar) onInit() {
this.Say("Where do you come from?", 2)
this.Broadcast__0("msg2")
}
func (this *bar) Classfname() string {
return "bar"
}
`, "Game.tgmx", "bar.tspx")
}

Expand Down Expand Up @@ -408,6 +423,9 @@ func (this *Kai) onInit() {
func (this *Kai) onCloned() {
this.Say("Hi")
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.tgmx", "Kai.tspx")
}

Expand Down Expand Up @@ -451,6 +469,9 @@ func main() {
func (this *Kai) Main() {
fmt.Println("Hi")
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "index.tgmx", "Kai.tspx")
}

Expand Down Expand Up @@ -490,6 +511,9 @@ func (this *Kai) Main(_gop_arg0 string) {
this.Sprite.Main(_gop_arg0)
fmt.Println(jwt.Token("Hi"))
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "main_spx.gox", "Kai_spx.gox")
}

Expand Down Expand Up @@ -523,6 +547,9 @@ func (this *Game) MainEntry() {
func main() {
spx3.Gopt_Game_Main(new(Game), new(Kai))
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "main_spx.gox", "Kai_spx.gox")
}

Expand Down Expand Up @@ -555,6 +582,9 @@ func main() {
}
func (this *Kai) onMsg(msg string) {
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.t2gmx", "Kai.t2spx")
}

Expand Down Expand Up @@ -587,6 +617,9 @@ func main() {
}
func (this *Kai) onMsg(msg string) {
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.t2gmx", "Kai.t2spx2")
}

Expand All @@ -613,8 +646,14 @@ type Kai struct {
func (this *Dog) Main() {
fmt.Println("Hi, Sprite")
}
func (this *Dog) Classfname() string {
return "Dog"
}
func (this *Kai) onMsg(msg string) {
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Dog_t3spx.gox", "Kai.t3spx2")
}

Expand Down Expand Up @@ -642,6 +681,9 @@ func (this *Game) MainEntry() {
func main() {
new(Game).Main()
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.t2gmx", "Kai.t2spx", "")
gopSpxTestExConf(t, "OnlyGmx", &conf, `
var (
Expand All @@ -666,6 +708,9 @@ func (this *Game) MainEntry() {
func main() {
new(Game).Main()
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.t2gmx", "Kai.t2spx", "")

gopSpxTestExConf(t, "KaiAndGmx", &conf, `
Expand Down Expand Up @@ -708,6 +753,9 @@ func (this *Kai) Main() {
}
func (this *Kai) onMsg(msg string) {
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.t2gmx", "Kai.t2spx", "")
}

Expand Down Expand Up @@ -746,6 +794,9 @@ func (this *Kai) onMsg(msg string) {
this.Say("Hi")
}
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.tgmx", "Kai.tspx")
}

Expand Down Expand Up @@ -810,6 +861,9 @@ func (this *Kai) onInit() {
func (this *Kai) onCloned() {
this.Say("Hi")
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.tgmx", "Kai.tspx")
}

Expand Down Expand Up @@ -847,6 +901,9 @@ func main() {
}
func (this *Kai) onMsg(msg string) {
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.tgmx", "Kai.tspx")
}

Expand Down Expand Up @@ -881,6 +938,9 @@ func main() {
func (this *Kai) onMsg(msg string) {
this.Position().Add__0(100, 200)
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.tgmx", "Kai.tspx")
}

Expand Down Expand Up @@ -940,6 +1000,9 @@ func (this *Kai) onMsg(msg string) {
fmt.Println(this.Vector().X)
fmt.Println(this.Vector().Self().Self())
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.tgmx", "Kai.tspx")
}

Expand Down Expand Up @@ -1041,6 +1104,9 @@ func (this *Kai) Main() {
spx.Gopt_Sprite_OnKey2(this, "hello", func(key string) {
})
}
func (this *Kai) Classfname() string {
return "Kai"
}
`, "Game.tgmx", "Kai.tspx")
}

Expand Down Expand Up @@ -1077,6 +1143,9 @@ func (this *caseFoo) Main() {
t.Fatal("failed")
})
}
func (this *caseFoo) Classfname() string {
return "Foo"
}
func TestFoo(t *testing.T) {
test.Gopt_Case_TestMain(new(caseFoo), t)
}
Expand Down Expand Up @@ -1105,6 +1174,9 @@ type case_foo struct {
func (this *case_foo) Main() {
this.T().Log("Hi")
}
func (this *case_foo) Classfname() string {
return "foo"
}
func Test_foo(t *testing.T) {
test.Gopt_Case_TestMain(new(case_foo), t)
}
Expand Down

0 comments on commit 1abc2bc

Please sign in to comment.