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

rename .gopx => .gox #1403

Merged
merged 7 commits into from
Aug 26, 2023
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 .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

*.gop text eol=lf
*.go text eol=lf
*.gopx text eol=lf
*.gox text eol=lf
3 changes: 2 additions & 1 deletion cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"

"github.com/goplus/gop/ast"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/token"
"github.com/goplus/gox"
)
Expand Down Expand Up @@ -60,7 +61,7 @@ func (p *gmxSettings) getScheds(cb *gox.CodeBuilder) []goast.Stmt {
}

func newGmx(ctx *pkgCtx, pkg *gox.Package, file string, conf *Config) *gmxSettings {
ext := filepath.Ext(file)
ext, _ := parser.ClassFileExt(file)
gt, ok := conf.LookupClass(ext)
if !ok {
panic("TODO: class not found")
Expand Down
13 changes: 8 additions & 5 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (
"go/types"
"log"
"os"
"path/filepath"
"reflect"
"sort"
"strings"

"github.com/goplus/gop/ast"
"github.com/goplus/gop/ast/fromgo"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/token"
"github.com/goplus/gox"
"github.com/goplus/gox/cpackages"
Expand Down Expand Up @@ -408,9 +408,11 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
}
if ctx.gmxSettings == nil {
for file, gmx := range files {
if gmx.IsClass && filepath.Ext(file) != ".gopx" {
ctx.gmxSettings = newGmx(ctx, p, file, conf)
break
if gmx.IsClass {
if ext, _ := parser.ClassFileExt(file); ext != ".gox" {
ctx.gmxSettings = newGmx(ctx, p, file, conf)
break
}
}
}
}
Expand Down Expand Up @@ -560,7 +562,8 @@ func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, con
case f.IsClass:
classType = getDefaultClass(file)
if parent.gmxSettings != nil {
o, ok := parent.sprite[filepath.Ext(file)]
ext, _ := parser.ClassFileExt(file)
o, ok := parent.sprite[ext]
if ok {
baseTypeName, baseType, spxClass = o.Name(), o.Type(), true
}
Expand Down
40 changes: 40 additions & 0 deletions cl/compile_spx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,43 @@ func (this *Kai) onMsg(msg string) {
}
`, "Game.t2gmx", "Kai.t2spx")
}

func TestSpxGoxBasic(t *testing.T) {
gopSpxTestEx(t, `
func onInit() {
for {
}
}
`, `
func onMsg(msg string) {
for {
say "Hi"
}
}
`, `package main

import spx "github.com/goplus/gop/cl/internal/spx"

type Game struct {
*spx.MyGame
}

func (this *Game) onInit() {
for {
spx.SchedNow()
}
}

type Kai struct {
spx.Sprite
*Game
}

func (this *Kai) onMsg(msg string) {
for {
spx.Sched()
this.Say("Hi")
}
}
`, "Game.tgmx.gox", "Kai.tspx.gox")
}
8 changes: 4 additions & 4 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4349,7 +4349,7 @@ type Rect struct {
func (this *Rect) Area() float64 {
return this.Width * this.Height
}
`, "Rect.gopx")
`, "Rect.gox")
gopClTestFile(t, `
import "bytes"
var (
Expand All @@ -4366,7 +4366,7 @@ type Rect struct {

func (this *Rect) test() {
}
`, "Rect.gopx")
`, "Rect.gox")
gopClTestFile(t, `
import "bytes"
var (
Expand All @@ -4383,7 +4383,7 @@ type Rect struct {

func (this *Rect) test() {
}
`, "Rect.gopx")
`, "Rect.gox")
gopClTestFile(t, `
import "bytes"
var (
Expand All @@ -4404,7 +4404,7 @@ type Rect struct {

func (this *Rect) test() {
}
`, "Rect.gopx")
`, "Rect.gox")
}

func TestOverload(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions cl/error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,9 +868,9 @@ import (
}

func TestErrClassFileGopx(t *testing.T) {
codeErrorTestEx(t, "main", "Rect.gopx",
`./Rect.gopx:5:2: A redeclared
./Rect.gopx:3:2 other declaration of A`, `
codeErrorTestEx(t, "main", "Rect.gox",
`./Rect.gox:5:2: A redeclared
./Rect.gox:3:2 other declaration of A`, `
var (
A
i int
Expand Down
4 changes: 2 additions & 2 deletions cmd/gopfmt/gopfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (w *walker) walk(path string, d fs.DirEntry, err error) error {
switch ext {
case ".go", ".gop":
ok = true
case ".gopx", ".spx", ".gmx":
case ".gox", ".spx", ".gmx":
ok, class = true, true
default:
_, class = mod.IsClass(ext)
Expand All @@ -153,7 +153,7 @@ func (w *walker) walk(path string, d fs.DirEntry, err error) error {
switch ext {
case ".go", ".gop":
ok = true
case ".gopx", ".spx", ".gmx":
case ".gox", ".spx", ".gmx":
ok, class = true, true
}
return
Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/gopfmt/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (w *walker) walk(path string, d fs.DirEntry, err error) error {
switch ext {
case ".go", ".gop":
ok = true
case ".gopx", ".spx", ".gmx":
case ".gox", ".spx", ".gmx":
ok, class = true, true
default:
_, class = mod.IsClass(ext)
Expand All @@ -162,7 +162,7 @@ func (w *walker) walk(path string, d fs.DirEntry, err error) error {
switch ext {
case ".go", ".gop":
ok = true
case ".gopx", ".spx", ".gmx":
case ".gox", ".spx", ".gmx":
ok, class = true, true
}
return
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

file bar.gopx
file bar.gox
ast.GenDecl:
Tok: var
Specs:
Expand Down
19 changes: 17 additions & 2 deletions parser/parser_gop.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package parser
import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"io/ioutil"
Expand Down Expand Up @@ -114,6 +115,17 @@ func ParseDirEx(fset *token.FileSet, path string, conf Config) (pkgs map[string]
return ParseFSDir(fset, local, path, conf)
}

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

// ParseFSDir calls ParseFile for all files with names ending in ".gop" in the
// directory specified by path and returns a map of package name -> package
// AST with all the packages found.
Expand All @@ -140,7 +152,7 @@ func ParseFSDir(fset *token.FileSet, fs FileSystem, path string, conf Config) (p
continue
}
fname := d.Name()
ext := filepath.Ext(fname)
ext, compositeGox := ClassFileExt(fname)
var isProj, isClass, useGoParser bool
switch ext {
case ".gop":
Expand All @@ -149,10 +161,13 @@ func ParseFSDir(fset *token.FileSet, fs FileSystem, path string, conf Config) (p
continue
}
useGoParser = (conf.Mode & ParseGoAsGoPlus) == 0
case ".gopx":
case ".gox":
isClass = true
default:
if isProj, isClass = conf.IsClass(ext); !isClass {
if compositeGox {
return nil, fmt.Errorf("not found Go+ class by ext %q for %q", ext, fname)
}
continue
}
}
Expand Down
16 changes: 8 additions & 8 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func testClassErrCode(t *testing.T, code string, errExp, panicExp string) {
}
}()
fset := token.NewFileSet()
_, err := Parse(fset, "/foo/bar.gopx", code, ParseGoPlusClass)
_, err := Parse(fset, "/foo/bar.gox", code, ParseGoPlusClass)
if err == nil || err.Error() != errExp {
t.Fatal("testErrCode error:", err)
}
Expand Down Expand Up @@ -395,36 +395,36 @@ func TestClassErrCode(t *testing.T) {
A,B
v int
)
`, `/foo/bar.gopx:2:2: missing variable type or initialization`, ``)
`, `/foo/bar.gox:2:2: missing variable type or initialization`, ``)
testClassErrCode(t, `var (
A.*B
v int
)
`, `/foo/bar.gopx:2:4: expected 'IDENT', found '*' (and 2 more errors)`, ``)
`, `/foo/bar.gox:2:4: expected 'IDENT', found '*' (and 2 more errors)`, ``)
testClassErrCode(t, `var (
[]A
v int
)
`, `/foo/bar.gopx:2:2: expected 'IDENT', found '['`, ``)
`, `/foo/bar.gox:2:2: expected 'IDENT', found '['`, ``)
testClassErrCode(t, `var (
*[]A
v int
)
`, `/foo/bar.gopx:2:3: expected 'IDENT', found '['`, ``)
`, `/foo/bar.gox:2:3: expected 'IDENT', found '['`, ``)
testClassErrCode(t, `var (
v int = 10
)
`, `/foo/bar.gopx:2:8: syntax error: cannot assign value to field in class file`, ``)
`, `/foo/bar.gox:2:8: syntax error: cannot assign value to field in class file`, ``)
testClassErrCode(t, `var (
v = 10
)
`, `/foo/bar.gopx:2:4: syntax error: cannot assign value to field in class file`, ``)
`, `/foo/bar.gox:2:4: syntax error: cannot assign value to field in class file`, ``)
testClassErrCode(t, `
var (
)
const c = 100
const d
`, `/foo/bar.gopx:5:7: missing constant value`, ``)
`, `/foo/bar.gox:5:7: missing constant value`, ``)
}

// -----------------------------------------------------------------------------
6 changes: 6 additions & 0 deletions parser/parserdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ func TestErrParse(t *testing.T) {
if err != syscall.ENOENT {
t.Fatal("ParseFSDir:", err)
}

fs = parsertest.NewSingleFileFS("/foo", "test.abc.gox", `package foo`)
_, err = ParseFSDir(fset, fs, "/foo", Config{})
if err == nil {
t.Fatal("ParseFSDir test.gop: no error?")
}
}

func testFromDir(t *testing.T, sel, relDir string) {
Expand Down
6 changes: 3 additions & 3 deletions printer/gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func testFrom(t *testing.T, fpath, sel string, mode int) {
if (mode & excludeFormatSource) == 0 {
t.Run("format.Source "+fpath, func(t *testing.T) {
var class bool
if filepath.Ext(fpath) == ".gopx" {
if filepath.Ext(fpath) == ".gox" {
class = true
}
res, err := format.Source(src, class, fpath)
Expand All @@ -137,7 +137,7 @@ func testFrom(t *testing.T, fpath, sel string, mode int) {
t.Run("format.Node "+fpath, func(t *testing.T) {
fset := token.NewFileSet()
m := parser.ParseComments
if filepath.Ext(fpath) == ".gopx" {
if filepath.Ext(fpath) == ".gox" {
m |= parser.ParseGoPlusClass
}
f, err := parser.ParseFile(fset, fpath, src, m)
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestFromParse(t *testing.T) {
}
name := info.Name()
ext := filepath.Ext(name)
if !info.IsDir() && (ext == ".gop" || ext == ".gopx") {
if !info.IsDir() && (ext == ".gop" || ext == ".gox") {
testFrom(t, path, sel, 0)
}
return nil
Expand Down