Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

classExt: support _[class].gox or .[class] #1606

Merged
merged 3 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ type File struct {
Code []byte
ShadowEntry *FuncDecl // no entrypoint func to indicate the module entry point.
NoPkgDecl bool // no `package xxx` declaration
IsClass bool // is a classfile
IsClass bool // is a classfile (including normal .gox file)
IsProj bool // is a project classfile
IsNormalGox bool // is a normal .gox file
}
Expand Down
15 changes: 4 additions & 11 deletions cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,10 @@ func getGoxConf() *gox.Config {
return &gox.Config{Fset: fset, Importer: imp}
}

func TestExt(t *testing.T) {
cases := [][2]string{
{"t.spx.gox", ".spx"},
{"t.spx", ".spx"},
{"t.gox", ".gox"},
{"t.abc", ".abc"},
}
for _, c := range cases {
if ret := ClassFileExt(c[0]); ret != c[1] {
t.Fatal("ClassFileExt:", c[0], "expected:", c[1], "got:", ret)
}
func TestClassNameAndExt(t *testing.T) {
name, ext := classNameAndExt("/foo/bar.abc_yap.gox")
if name != "bar" || ext != "_yap.gox" {
t.Fatal("classNameAndExt:", name, ext)
}
}

Expand Down
36 changes: 13 additions & 23 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/goplus/gop/ast"
"github.com/goplus/gop/token"
"github.com/goplus/gox"
"github.com/goplus/mod/modfile"
)

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -59,8 +60,18 @@ func (p *gmxSettings) getScheds(cb *gox.CodeBuilder) []goast.Stmt {
return p.schedStmts
}

func classNameAndExt(file string) (name, ext string) {
fname := filepath.Base(file)
name, ext = modfile.SplitFname(fname)
if idx := strings.Index(name, "."); idx > 0 {
name = name[:idx]
}
return
}

func newGmx(ctx *pkgCtx, pkg *gox.Package, file string, f *ast.File, conf *Config) *gmxSettings {
ext := ClassFileExt(file)
fname := filepath.Base(file)
ext := modfile.ClassExt(fname)
gt, ok := conf.LookupClass(ext)
if !ok {
panic("TODO: class not found")
Expand Down Expand Up @@ -105,19 +116,11 @@ func spxLookup(pkgImps []*gox.PkgRef, name string) gox.Ref {
panic("spxLookup: symbol not found - " + name)
}

func getDefaultClass(file string) string {
_, name := filepath.Split(file)
if idx := strings.Index(name, "."); idx > 0 {
name = name[:idx]
}
return name
}

func spxTryRef(spx *gox.PkgRef, typ string) (obj types.Object, isPtr bool) {
if strings.HasPrefix(typ, "*") {
typ, isPtr = typ[1:], true
}
obj = spx.Ref(typ)
obj = spx.TryRef(typ)
return
}

Expand Down Expand Up @@ -176,16 +179,3 @@ func gmxMainFunc(p *gox.Package, ctx *pkgCtx) {
}

// -----------------------------------------------------------------------------

// ClassFileExt returns the classfile extension
func ClassFileExt(path string) (ext string) {
ext = filepath.Ext(path)
if ext == ".gox" {
if c := filepath.Ext(path[:len(path)-4]); c != "" {
return c
}
}
return
}

// -----------------------------------------------------------------------------
12 changes: 3 additions & 9 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,6 @@ func (p *pkgCtx) newCodeErrorf(pos token.Pos, format string, args ...interface{}
return &gox.CodeError{Fset: p.nodeInterp, Pos: pos, Msg: fmt.Sprintf(format, args...)}
}

/*
func (p *pkgCtx) handleError(pos token.Pos, msg string) {
p.handleErr(p.newCodeError(pos, msg))
}
*/

func (p *pkgCtx) handleErrorf(pos token.Pos, format string, args ...interface{}) {
p.handleErr(p.newCodeErrorf(pos, format, args...))
}
Expand Down Expand Up @@ -701,10 +695,10 @@ func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, con
baseType = types.NewPointer(baseType)
}
case f.IsClass:
classType = getDefaultClass(file)
var classExt string
classType, classExt = classNameAndExt(file)
if parent.gmxSettings != nil {
ext := ClassFileExt(file)
o, ok := parent.sprite[ext]
o, ok := parent.sprite[classExt]
if ok {
baseTypeName, baseType, spxClass = o.Name(), o.Type(), true
}
Expand Down
8 changes: 4 additions & 4 deletions cl/compile_spx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func lookupClass(ext string) (c *modfile.Project, ok bool) {
Works: []*modfile.Class{{Ext: ".t2spx", Class: "Sprite"},
{Ext: ".t2spx2", Class: "Sprite2"}},
PkgPaths: []string{"github.com/goplus/gop/cl/internal/spx2"}}, true
case ".t3spx", ".t3spx2":
case "_t3spx.gox", ".t3spx2":
return &modfile.Project{
Works: []*modfile.Class{{Ext: ".t3spx", Class: "Sprite"},
Works: []*modfile.Class{{Ext: "_t3spx.gox", Class: "Sprite"},
{Ext: ".t3spx2", Class: "Sprite2"}},
PkgPaths: []string{"github.com/goplus/gop/cl/internal/spx2"}}, true
}
Expand Down Expand Up @@ -460,7 +460,7 @@ type Kai struct {

func (this *Kai) onMsg(msg string) {
}
`, "Dog.t3spx", "Kai.t3spx2")
`, "Dog_t3spx.gox", "Kai.t3spx2")
}

func TestSpxMainEntry(t *testing.T) {
Expand Down Expand Up @@ -589,7 +589,7 @@ func (this *Kai) onMsg(msg string) {
this.Say("Hi")
}
}
`, "Game.tgmx.gox", "Kai.tspx.gox")
`, "Game.tgmx", "Kai.tspx")
}

func TestSpxClone(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/fsnotify/fsnotify v1.7.0
github.com/goplus/c2go v0.7.19
github.com/goplus/gox v1.13.2
github.com/goplus/mod v0.12.0
github.com/goplus/mod v0.12.1
github.com/qiniu/x v1.13.2
golang.org/x/tools v0.16.1
)
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ github.com/goplus/c2go v0.7.19 h1:pPMarGN1RiNJFAYlL8oZlM7VNXeg054B4czDgBlck0o=
github.com/goplus/c2go v0.7.19/go.mod h1:EJgnt9CEHnXuWQc4y78w3DhKErL1aBzgE41NZaEa/3c=
github.com/goplus/gox v1.13.2 h1:XZZtLcZasRVKOYNzTnJ9E2Cu350TWu9K+CYMJTItkis=
github.com/goplus/gox v1.13.2/go.mod h1:iIchh0wp8Ye0DOPcFgHc5d0qlMOx8/OJ+DBQfe7hcTs=
github.com/goplus/mod v0.12.0 h1:b1nPDFRPjksb/BXBUiIA1uVNHc3egRVfYSlShlq3nZE=
github.com/goplus/mod v0.12.0/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs=
github.com/goplus/mod v0.12.1 h1:4M1py5liQ/bfxPPXcmVbYn3JAjfcDpTANtih5omFI6c=
github.com/goplus/mod v0.12.1/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
Expand Down
25 changes: 8 additions & 17 deletions parser/parser_gop.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ func ParseFSDir(fset *token.FileSet, fs FileSystem, dir string, conf Config) (pk
continue
}
fname := d.Name()
fnameRmGox := fname
ext := path.Ext(fname)
var isProj, isClass, isNormalGox, useGoParser, rmGox bool
var isProj, isClass, isNormalGox, useGoParser bool
switch ext {
case ".gop":
case ".go":
Expand All @@ -143,23 +142,15 @@ func ParseFSDir(fset *token.FileSet, fs FileSystem, dir string, conf Config) (pk
}
useGoParser = (conf.Mode & ParseGoAsGoPlus) == 0
case ".gox":
isClass = true
t := fname[:len(fname)-4]
if c := path.Ext(t); c != "" {
fnameRmGox, rmGox = t, true
} else {
isNormalGox = true
}
isNormalGox = true
fallthrough
default:
if !isNormalGox {
if isProj, isClass = conf.ClassKind(fnameRmGox); !isClass {
if !rmGox { // unknown fileKind
continue
}
// not found Go+ class by ext, but is a .gox file
isClass, isNormalGox = true, true
}
if isProj, isClass = conf.ClassKind(fname); isClass {
isNormalGox = false
} else if isNormalGox { // not found Go+ class by ext, but is a .gox file
isClass = true
} else { // unknown fileKind
continue
}
}
mode := conf.Mode
Expand Down