Skip to content

Commit

Permalink
Merge pull request #1404 from xushiwei/q
Browse files Browse the repository at this point in the history
support main.spx; classfile: register => import; IsClass => ClassKind
  • Loading branch information
xushiwei authored Aug 26, 2023
2 parents 09c8cc1 + a52ca84 commit 262efe7
Show file tree
Hide file tree
Showing 20 changed files with 82 additions and 71 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ coverage.txt
.gop/
gop_autogen*.go
go.json
go.work
go.work.sum
go.work*
format.result
*.cache

Expand Down
1 change: 1 addition & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ type File struct {
NoPkgDecl bool // no `package xxx` declaration
IsClass bool // is a classfile
IsProj bool // is a project classfile
IsNormalGox bool // is a normal .gox file
}

// Pos returns position of first character belonging to the node.
Expand Down
11 changes: 1 addition & 10 deletions ast/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func exportFilter(name string) bool {
// stripped. The File.Comments list is not changed.
//
// FileExports reports whether there are exported declarations.
//
func FileExports(src *File) bool {
return filterFile(src, exportFilter, true)
}
Expand All @@ -48,7 +47,6 @@ func FileExports(src *File) bool {
//
// PackageExports reports whether there are exported declarations;
// it returns false otherwise.
//
func PackageExports(pkg *Package) bool {
return filterPackage(pkg, exportFilter, true)
}
Expand All @@ -73,7 +71,6 @@ func filterIdentList(list []*Ident, f Filter) []*Ident {
// fieldName assumes that x is the type of an anonymous field and
// returns the corresponding field name. If x is not an acceptable
// anonymous field, the result is nil.
//
func fieldName(x Expr) *Ident {
switch t := x.(type) {
case *Ident:
Expand Down Expand Up @@ -243,7 +240,6 @@ func filterSpecList(list []Spec, f Filter, export bool) []Spec {
//
// FilterDecl reports whether there are any declared names left after
// filtering.
//
func FilterDecl(decl Decl, f Filter) bool {
return filterDecl(decl, f, false)
}
Expand All @@ -268,7 +264,6 @@ func filterDecl(decl Decl, f Filter, export bool) bool {
//
// FilterFile reports whether there are any top-level declarations
// left after filtering.
//
func FilterFile(src *File, f Filter) bool {
return filterFile(src, f, false)
}
Expand All @@ -295,7 +290,6 @@ func filterFile(src *File, f Filter, export bool) bool {
//
// FilterPackage reports whether there are any top-level declarations
// left after filtering.
//
func FilterPackage(pkg *Package, f Filter) bool {
return filterPackage(pkg, f, false)
}
Expand Down Expand Up @@ -329,7 +323,6 @@ const (
// nameOf returns the function (foo) or method name (foo.bar) for
// the given function declaration. If the AST is incorrect for the
// receiver, it assumes a function instead.
//
func nameOf(f *FuncDecl) string {
if r := f.Recv; r != nil && len(r.List) == 1 {
// looks like a correct receiver declaration
Expand All @@ -349,12 +342,10 @@ func nameOf(f *FuncDecl) string {

// separator is an empty //-style comment that is interspersed between
// different comment groups when they are concatenated into a single group
//
var separator = &Comment{Slash: token.NoPos, Text: "//"}

// MergePackageFiles creates a file AST by merging the ASTs of the
// files belonging to a package. The mode flags control merging behavior.
//
func MergePackageFiles(pkg *Package, mode MergeMode) *File {
// Count the number of package docs, comments and declarations across
// all package files. Also, compute sorted list of filenames, so that
Expand Down Expand Up @@ -509,6 +500,6 @@ func MergePackageFiles(pkg *Package, mode MergeMode) *File {
// TODO(gri) need to compute unresolved identifiers!
return &File{
doc, pos, NewIdent(pkg.Name), decls, pkg.Scope,
imports, nil, comments, nil, false, false, false, false,
imports, nil, comments, nil, false, false, false, false, false,
}
}
2 changes: 1 addition & 1 deletion cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestClRangeStmt(t *testing.T) {

func TestGmxSettings(t *testing.T) {
pkg := gox.NewPackage("", "foo", goxConf)
gmx := newGmx(nil, pkg, "main.t2gmx", &Config{
gmx := newGmx(nil, pkg, "main.t2gmx", &ast.File{IsProj: true}, &Config{
LookupClass: lookupClass,
})
scheds := gmx.getScheds(pkg.CB())
Expand Down
6 changes: 3 additions & 3 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ func (p *gmxSettings) getScheds(cb *gox.CodeBuilder) []goast.Stmt {
return p.schedStmts
}

func newGmx(ctx *pkgCtx, pkg *gox.Package, file string, conf *Config) *gmxSettings {
ext, _ := parser.ClassFileExt(file)
func newGmx(ctx *pkgCtx, pkg *gox.Package, file string, f *ast.File, conf *Config) *gmxSettings {
ext := parser.ClassFileExt(file)
gt, ok := conf.LookupClass(ext)
if !ok {
panic("TODO: class not found")
}
var name string
if gt.Ext == ext {
if f.IsProj {
_, name = filepath.Split(file)
if idx := strings.Index(name, "."); idx > 0 {
name = name[:idx]
Expand Down
12 changes: 5 additions & 7 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,17 +402,15 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
})
for file, gmx := range files {
if gmx.IsProj {
ctx.gmxSettings = newGmx(ctx, p, file, conf)
ctx.gmxSettings = newGmx(ctx, p, file, gmx, conf)
break
}
}
if ctx.gmxSettings == nil {
for file, gmx := range files {
if gmx.IsClass {
if ext, _ := parser.ClassFileExt(file); ext != ".gox" {
ctx.gmxSettings = newGmx(ctx, p, file, conf)
break
}
if gmx.IsClass && !gmx.IsNormalGox {
ctx.gmxSettings = newGmx(ctx, p, file, gmx, conf)
break
}
}
}
Expand Down Expand Up @@ -562,7 +560,7 @@ func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, con
case f.IsClass:
classType = getDefaultClass(file)
if parent.gmxSettings != nil {
ext, _ := parser.ClassFileExt(file)
ext := parser.ClassFileExt(file)
o, ok := parent.sprite[ext]
if ok {
baseTypeName, baseType, spxClass = o.Name(), o.Type(), true
Expand Down
4 changes: 3 additions & 1 deletion cl/compile_spx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cl_test
import (
"bytes"
"os"
"path"
"testing"

"github.com/goplus/gop/cl"
Expand Down Expand Up @@ -52,7 +53,8 @@ func lookupClass(ext string) (c *modfile.Project, ok bool) {

func spxParserConf() parser.Config {
return parser.Config{
IsClass: func(ext string) (isProj bool, ok bool) {
ClassKind: func(fname string) (isProj bool, ok bool) {
ext := path.Ext(fname)
c, ok := lookupClass(ext)
if ok {
isProj = (c.Ext == ext)
Expand Down
1 change: 0 additions & 1 deletion cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,6 @@ func compileSliceLit(ctx *blockCtx, v *ast.SliceLit, typ types.Type) {
} else {
ctx.cb.SliceLit(typ, n)
}
return
}

func compileRangeExpr(ctx *blockCtx, v *ast.RangeExpr) {
Expand Down
2 changes: 1 addition & 1 deletion cl/func_type_and_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func lookupType(ctx *blockCtx, name string) (types.Object, types.Object) {
}

type checkRedecl struct {
ctx *blockCtx
// ctx *blockCtx
names map[string]token.Pos
}

Expand Down
2 changes: 1 addition & 1 deletion cl/types_go118.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ L:
}
switch t := typ.(type) {
case *ast.IndexExpr:
if 1 != orgTypeParams.Len() {
if orgTypeParams.Len() != 1 {
panic(ctx.newCodeErrorf(typ.Pos(), "got 1 type parameter, but receiver base type declares %v", orgTypeParams.Len()))
}
v := t.Index.(*ast.Ident)
Expand Down
11 changes: 4 additions & 7 deletions cmd/gopfmt/gopfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"go/token"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -64,7 +63,7 @@ func processFile(filename string, class bool, in io.Reader, out io.Writer) error
return err
}
}
src, err := ioutil.ReadAll(in)
src, err := io.ReadAll(in)
if err != nil {
return err
}
Expand All @@ -90,7 +89,7 @@ func processFile(filename string, class bool, in io.Reader, out io.Writer) error

if *write {
dir, file := filepath.Split(filename)
f, err := ioutil.TempFile(dir, file)
f, err := os.CreateTemp(dir, file)
if err != nil {
return err
}
Expand Down Expand Up @@ -141,10 +140,8 @@ func (w *walker) walk(path string, d fs.DirEntry, err error) error {
case ".gox", ".spx", ".gmx":
ok, class = true, true
default:
_, class = mod.IsClass(ext)
if class {
ok = true
}
class = mod.IsClass(ext)
ok = class
}
return
}
Expand Down
11 changes: 4 additions & 7 deletions cmd/internal/gopfmt/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"bytes"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -63,7 +62,7 @@ var (
)

func gopfmt(path string, class, smart, mvgo bool) (err error) {
src, err := ioutil.ReadFile(path)
src, err := os.ReadFile(path)
if err != nil {
return
}
Expand Down Expand Up @@ -106,7 +105,7 @@ func gopfmt(path string, class, smart, mvgo bool) (err error) {

func writeFileWithBackup(path string, target []byte) (err error) {
dir, file := filepath.Split(path)
f, err := ioutil.TempFile(dir, file)
f, err := os.CreateTemp(dir, file)
if err != nil {
return
}
Expand Down Expand Up @@ -150,10 +149,8 @@ func (w *walker) walk(path string, d fs.DirEntry, err error) error {
case ".gox", ".spx", ".gmx":
ok, class = true, true
default:
_, class = mod.IsClass(ext)
if class {
ok = true
}
class = mod.IsClass(ext)
ok = class
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/gopget/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func get(pkgPath string) {
pkgMod, err := modload.Load(pkgModRoot, 0)
check(err)
if pkgMod.Project != nil {
mod.AddRegister(pkgModVer.Path)
mod.AddImport(pkgModVer.Path)
fmt.Fprintf(os.Stderr, "gop get: registered %s\n", pkgModVer.Path)
}

Expand Down
6 changes: 3 additions & 3 deletions gendeps.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
func GenDepMods(mod *gopmod.Module, dir string, recursively bool) (ret map[string]struct{}, err error) {
modBase := mod.Path()
ret = make(map[string]struct{})
for _, r := range mod.Register {
for _, r := range mod.Import {
ret[r.ClassfileMod] = struct{}{}
}
err = HandleDeps(mod, dir, recursively, func(pkgPath string) {
Expand Down Expand Up @@ -81,8 +81,8 @@ type depsGen struct {

func (p depsGen) gen(dir string) (err error) {
pkgs, err := parser.ParseDirEx(p.fset, dir, parser.Config{
IsClass: p.mod.IsClass,
Mode: parser.ImportsOnly,
ClassKind: p.mod.ClassKind,
Mode: parser.ImportsOnly,
})
if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ require (
github.com/goplus/c2go v0.7.13
github.com/goplus/gox v1.11.37
github.com/goplus/libc v0.3.13
github.com/goplus/mod v0.10.1
github.com/goplus/mod v0.11.2
github.com/qiniu/x v1.11.9
)
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ github.com/goplus/gox v1.11.37/go.mod h1:NkgUJWIjKxrhUwM4bgyUt3ZE0WlTunqfksMzrbn
github.com/goplus/libc v0.3.13 h1:2yHG8ghezBTK3Da7Fw1lNiitmt+Va+RzwKJ5GuqypCQ=
github.com/goplus/libc v0.3.13/go.mod h1:xqG4/g3ilKBE/UDn5vkaE7RRQPQPyspj7ecuMuvlQJ8=
github.com/goplus/mod v0.9.12/go.mod h1:YoPIowz71rnLLROA4YG0AC8bzDtPRyMaQwgTRLr8ri4=
github.com/goplus/mod v0.10.1 h1:us4GMfWL+7tcK8diI7Z90awoVmUgDhftt83NeeqgYE4=
github.com/goplus/mod v0.10.1/go.mod h1:cD7VktDd88SM0fPJ9oT3z5r8KTBW+EGi0gA3G8cDKHo=
github.com/goplus/mod v0.11.2 h1:kgIdro905lTZgqGecTbZ416O8ayN+ca2DR5OB4ayeWI=
github.com/goplus/mod v0.11.2/go.mod h1:d40I3nOr2qkGfLUjwc/BqLSq3oUUlQy5+/SpdiBKgY4=
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 All @@ -37,7 +37,6 @@ golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIi
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
8 changes: 4 additions & 4 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func LoadMod(dir string, gop *env.Gop, conf *Config) (mod *gopmod.Module, err er
return
}
if mod != nil {
err = mod.RegisterClasses()
err = mod.ImportClasses()
if err != nil {
err = errors.NewWith(err, `mod.RegisterClasses()`, -2, "(*gopmod.Module).RegisterClasses", mod)
return
Expand Down Expand Up @@ -107,9 +107,9 @@ func LoadDir(dir string, conf *Config, genTestPkg bool, promptGenGo ...bool) (ou
}

pkgs, err := parser.ParseDirEx(fset, dir, parser.Config{
IsClass: mod.IsClass,
Filter: conf.Filter,
Mode: parser.ParseComments,
ClassKind: mod.ClassKind,
Filter: conf.Filter,
Mode: parser.ParseComments,
})
if err != nil {
return
Expand Down
2 changes: 2 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2104,13 +2104,15 @@ func isLiteralType(x ast.Expr) bool {
return true
}

/*
// If x is of the form *T, deref returns T, otherwise it returns x.
func deref(x ast.Expr) ast.Expr {
if p, isPtr := x.(*ast.StarExpr); isPtr {
x = p.X
}
return x
}
*/

// If x is of the form (T), unparen returns unparen(T), otherwise it returns x.
func unparen(x ast.Expr) ast.Expr {
Expand Down
Loading

0 comments on commit 262efe7

Please sign in to comment.