From 475e4a45da80db08ec53544d9c228e2f738867f4 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sat, 20 Jan 2024 22:03:50 +0800 Subject: [PATCH 1/3] call gmxMainFunc only if no main func --- cl/classfile.go | 4 +++- cl/compile.go | 22 +++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/cl/classfile.go b/cl/classfile.go index 727772891..a9031c230 100644 --- a/cl/classfile.go +++ b/cl/classfile.go @@ -164,14 +164,16 @@ func setBodyHandler(ctx *blockCtx) { } } -func gmxMainFunc(p *gox.Package, ctx *pkgCtx) { +func gmxMainFunc(p *gox.Package, ctx *pkgCtx) bool { if o := p.Types.Scope().Lookup(ctx.gameClass); o != nil && hasMethod(o, "MainEntry") { // new(Game).Main() p.NewFunc(nil, "main", nil, nil, false).BodyStart(p). Val(p.Builtin().Ref("new")).Val(o).Call(1). MemberVal("Main").Call(0).EndStmt(). End() + return true } + return false } // ----------------------------------------------------------------------------- diff --git a/cl/compile.go b/cl/compile.go index 794359187..8a10c46bf 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -561,10 +561,20 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package, return sfiles[i].path < sfiles[j].path }) + // genMain = true if it is main package and no main func + var genMain bool + if pkg.Name == "main" && !conf.NoAutoGenMain { + if obj := p.Types.Scope().Lookup("main"); obj == nil { + genMain = true + } + } + for _, f := range sfiles { if f.IsProj { loadFile(ctx, f.File) - gmxMainFunc(p, ctx) + if genMain { // generate main func if need + genMain = !gmxMainFunc(p, ctx) + } break } } @@ -586,12 +596,10 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package, } err = ctx.complete() - if !conf.NoAutoGenMain && pkg.Name == "main" { - if obj := p.Types.Scope().Lookup("main"); obj == nil { - old, _ := p.SetCurFile(defaultGoFile, false) - p.NewFunc(nil, "main", nil, nil, false).BodyStart(p).End() - p.RestoreCurFile(old) - } + if genMain { // generate empty main func + old, _ := p.SetCurFile(defaultGoFile, false) + p.NewFunc(nil, "main", nil, nil, false).BodyStart(p).End() + p.RestoreCurFile(old) } return } From 61145d36602f396c3cd8e3b34b98d440b7d23c9f Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sat, 20 Jan 2024 22:11:14 +0800 Subject: [PATCH 2/3] initBuiltin: echo --- cl/builtin.go | 1 + cl/compile_test.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cl/builtin.go b/cl/builtin.go index 3058e246e..661635adb 100644 --- a/cl/builtin.go +++ b/cl/builtin.go @@ -51,6 +51,7 @@ func initBuiltin(pkg *gox.Package, builtin *types.Package, os, fmt, ng, iox, bui scope.Insert(types.NewTypeName(token.NoPos, builtin, "int128", ng.Ref("Int128").Type())) } if fmt != nil { + scope.Insert(gox.NewOverloadFunc(token.NoPos, builtin, "echo", fmt.Ref("Println"))) initBuiltinFns(builtin, scope, fmt, []string{ "print", "println", "printf", "errorf", "fprint", "fprintln", "fprintf", diff --git a/cl/compile_test.go b/cl/compile_test.go index 1c334d974..02e6afe8d 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -123,7 +123,7 @@ func gopClTestFS(t *testing.T, conf *cl.Config, fs parser.FileSystem, pkgname, e } func TestStringLitBasic(t *testing.T) { - gopClTest(t, `println "$$"`, `package main + gopClTest(t, `echo "$$"`, `package main import "fmt" From 2e3b77ef2083714489561ea0d8dbbb13f72cf7b4 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sat, 20 Jan 2024 23:03:11 +0800 Subject: [PATCH 3/3] file.NoEntrypoint => file.HasShadowEntry --- ast/ast.go | 2 +- cl/compile.go | 11 +++++------ parser/parsertest/parsertest.go | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/ast/ast.go b/ast/ast.go index 3d2836b60..423e60b99 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -1135,7 +1135,7 @@ type File struct { } // There is no entrypoint func to indicate the module entry point. -func (f *File) NoEntrypoint() bool { +func (f *File) HasShadowEntry() bool { return f.ShadowEntry != nil } diff --git a/cl/compile.go b/cl/compile.go index 8a10c46bf..b112333cf 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -563,10 +563,9 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package, // genMain = true if it is main package and no main func var genMain bool - if pkg.Name == "main" && !conf.NoAutoGenMain { - if obj := p.Types.Scope().Lookup("main"); obj == nil { - genMain = true - } + if pkg.Name == "main" { + _, hasMain := ctx.syms["main"] + genMain = !hasMain } for _, f := range sfiles { @@ -596,7 +595,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package, } err = ctx.complete() - if genMain { // generate empty main func + if genMain && !conf.NoAutoGenMain { // generate empty main func old, _ := p.SetCurFile(defaultGoFile, false) p.NewFunc(nil, "main", nil, nil, false).BodyStart(p).End() p.RestoreCurFile(old) @@ -809,7 +808,7 @@ func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, con }}} } // check class project no MainEntry and auto added - if f.IsProj && !conf.NoAutoGenMain && !f.NoEntrypoint() && f.Name.Name == "main" { + if f.IsProj && !conf.NoAutoGenMain && !f.HasShadowEntry() && f.Name.Name == "main" { entry := getEntrypoint(f) var hasEntry bool for _, decl := range f.Decls { diff --git a/parser/parsertest/parsertest.go b/parser/parsertest/parsertest.go index 89a846d3b..081d233ab 100644 --- a/parser/parsertest/parsertest.go +++ b/parser/parsertest/parsertest.go @@ -111,7 +111,7 @@ func Fprint(w io.Writer, pkg *ast.Package) { for _, fpath := range paths { fmt.Fprintf(w, "\nfile %s\n", filepath.Base(fpath)) file := pkg.Files[fpath] - if file.NoEntrypoint() { + if file.HasShadowEntry() { fmt.Fprintf(w, "noEntrypoint\n") } FprintNode(w, "", file.Decls, "", " ")