Skip to content

Commit

Permalink
Merge pull request #1798 from xushiwei/q
Browse files Browse the repository at this point in the history
gox => gogen
  • Loading branch information
xushiwei committed Mar 8, 2024
2 parents 40d509f + 64ae777 commit 62897ea
Show file tree
Hide file tree
Showing 39 changed files with 187 additions and 207 deletions.
11 changes: 0 additions & 11 deletions ast/commentmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (a byPos) Less(i, j int) bool { return a[i].Pos() < a[j].Pos() }
func (a byPos) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// sortComments sorts the list of comment groups in source order.
//
func sortComments(list []*CommentGroup) {
// TODO(gri): Does it make sense to check for sorted-ness
// first (because we know that sorted-ness is
Expand All @@ -44,7 +43,6 @@ func sortComments(list []*CommentGroup) {
// A CommentMap maps an AST node to a list of comment groups
// associated with it. See NewCommentMap for a description of
// the association.
//
type CommentMap map[Node][]*CommentGroup

func (cmap CommentMap) addComment(n Node, c *CommentGroup) {
Expand All @@ -58,7 +56,6 @@ func (cmap CommentMap) addComment(n Node, c *CommentGroup) {
}

// nodeList returns the list of nodes of the AST n in source order.
//
func nodeList(n Node) []Node {
var list []Node
Inspect(n, func(n Node) bool {
Expand All @@ -79,7 +76,6 @@ func nodeList(n Node) []Node {
}

// A commentListReader helps iterating through a list of comment groups.
//
type commentListReader struct {
fset *token.FileSet
list []*CommentGroup
Expand All @@ -103,12 +99,10 @@ func (r *commentListReader) next() {

// A nodeStack keeps track of nested nodes.
// A node lower on the stack lexically contains the nodes higher on the stack.
//
type nodeStack []Node

// push pops all nodes that appear lexically before n
// and then pushes n on the stack.
//
func (s *nodeStack) push(n Node) {
s.pop(n.Pos())
*s = append((*s), n)
Expand All @@ -117,7 +111,6 @@ func (s *nodeStack) push(n Node) {
// pop pops all nodes that appear lexically before pos
// (i.e., whose lexical extent has ended before or at pos).
// It returns the last node popped.
//
func (s *nodeStack) pop(pos token.Pos) (top Node) {
i := len(*s)
for i > 0 && (*s)[i-1].End() <= pos {
Expand All @@ -143,7 +136,6 @@ func (s *nodeStack) pop(pos token.Pos) (top Node) {
// node possible: For instance, if the comment is a line comment
// trailing an assignment, the comment is associated with the entire
// assignment rather than just the last operand in the assignment.
//
func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) CommentMap {
if len(comments) == 0 {
return nil // no comments to map
Expand Down Expand Up @@ -246,7 +238,6 @@ func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) Com
// Update replaces an old node in the comment map with the new node
// and returns the new node. Comments that were associated with the
// old node are associated with the new node.
//
func (cmap CommentMap) Update(old, new Node) Node {
if list := cmap[old]; len(list) > 0 {
delete(cmap, old)
Expand All @@ -258,7 +249,6 @@ func (cmap CommentMap) Update(old, new Node) Node {
// Filter returns a new comment map consisting of only those
// entries of cmap for which a corresponding node exists in
// the AST specified by node.
//
func (cmap CommentMap) Filter(node Node) CommentMap {
umap := make(CommentMap)
Inspect(node, func(n Node) bool {
Expand All @@ -272,7 +262,6 @@ func (cmap CommentMap) Filter(node Node) CommentMap {

// Comments returns the list of comment groups in the comment map.
// The result is sorted in source order.
//
func (cmap CommentMap) Comments() []*CommentGroup {
list := make([]*CommentGroup, 0, len(cmap))
for _, e := range cmap {
Expand Down
1 change: 0 additions & 1 deletion ast/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ type Importer func(imports map[string]*Object, path string) (pkg *Object, err er
// belong to different packages, one package name is selected and files with
// different package names are reported and then ignored.
// The result is a package node and a scanner.ErrorList if there were errors.
//
func NewPackage(fset *token.FileSet, files map[string]*File, importer Importer, universe *Scope) (*Package, error) {
var p pkgBuilder
p.fset = fset
Expand Down
4 changes: 0 additions & 4 deletions ast/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
// A Scope maintains the set of named language entities declared
// in the scope and a link to the immediately surrounding (outer)
// scope.
//
type Scope struct {
Outer *Scope
Objects map[string]*Object
Expand All @@ -43,7 +42,6 @@ func NewScope(outer *Scope) *Scope {
// Lookup returns the object with the given name if it is
// found in scope s, otherwise it returns nil. Outer scopes
// are ignored.
//
func (s *Scope) Lookup(name string) *Object {
return s.Objects[name]
}
Expand All @@ -52,7 +50,6 @@ func (s *Scope) Lookup(name string) *Object {
// If the scope already contains an object alt with the same name,
// Insert leaves the scope unchanged and returns alt. Otherwise
// it inserts obj and returns nil.
//
func (s *Scope) Insert(obj *Object) (alt *Object) {
if alt = s.Objects[obj.Name]; alt == nil {
s.Objects[obj.Name] = obj
Expand Down Expand Up @@ -85,7 +82,6 @@ func (s *Scope) String() string {
// Kind Data type Data value
// Pkg *Scope package scope
// Con int iota for the respective declaration
//
type Object struct {
Kind ObjKind
Name string // declared name
Expand Down
22 changes: 11 additions & 11 deletions cl/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ import (
"go/token"
"go/types"

"github.com/goplus/gox"
"github.com/goplus/gogen"
)

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

func initMathBig(_ *gox.Package, conf *gox.Config, big gox.PkgRef) {
func initMathBig(_ *gogen.Package, conf *gogen.Config, big gogen.PkgRef) {
conf.UntypedBigInt = big.Ref("UntypedBigint").Type().(*types.Named)
conf.UntypedBigRat = big.Ref("UntypedBigrat").Type().(*types.Named)
conf.UntypedBigFloat = big.Ref("UntypedBigfloat").Type().(*types.Named)
}

func initBuiltinFns(builtin *types.Package, scope *types.Scope, pkg gox.PkgRef, fns []string) {
func initBuiltinFns(builtin *types.Package, scope *types.Scope, pkg gogen.PkgRef, fns []string) {
for _, fn := range fns {
fnTitle := string(fn[0]-'a'+'A') + fn[1:]
scope.Insert(gox.NewOverloadFunc(token.NoPos, builtin, fn, pkg.Ref(fnTitle)))
scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, fn, pkg.Ref(fnTitle)))
}
}

func initBuiltin(_ *gox.Package, builtin *types.Package, os, fmt, ng, iox, buil gox.PkgRef) {
func initBuiltin(_ *gogen.Package, builtin *types.Package, os, fmt, ng, iox, buil gogen.PkgRef) {
scope := builtin.Scope()
if ng.Types != nil {
typs := []string{"bigint", "bigrat", "bigfloat"}
Expand All @@ -50,7 +50,7 @@ func initBuiltin(_ *gox.Package, builtin *types.Package, os, fmt, ng, iox, buil
scope.Insert(types.NewTypeName(token.NoPos, builtin, "int128", ng.Ref("Int128").Type()))
}
if fmt.Types != nil {
scope.Insert(gox.NewOverloadFunc(token.NoPos, builtin, "echo", fmt.Ref("Println")))
scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, "echo", fmt.Ref("Println")))
initBuiltinFns(builtin, scope, fmt, []string{
"print", "println", "printf", "errorf",
"fprint", "fprintln", "fprintf",
Expand All @@ -66,15 +66,15 @@ func initBuiltin(_ *gox.Package, builtin *types.Package, os, fmt, ng, iox, buil
initBuiltinFns(builtin, scope, iox, []string{
"lines",
})
scope.Insert(gox.NewOverloadFunc(token.NoPos, builtin, "blines", iox.Ref("BLines")))
scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, "blines", iox.Ref("BLines")))
}
if buil.Types != nil {
scope.Insert(gox.NewOverloadFunc(token.NoPos, builtin, "newRange", buil.Ref("NewRange__0")))
scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, "newRange", buil.Ref("NewRange__0")))
}
scope.Insert(types.NewTypeName(token.NoPos, builtin, "any", gox.TyEmptyInterface))
scope.Insert(types.NewTypeName(token.NoPos, builtin, "any", gogen.TyEmptyInterface))
}

func newBuiltinDefault(pkg *gox.Package, conf *gox.Config) *types.Package {
func newBuiltinDefault(pkg *gogen.Package, conf *gogen.Config) *types.Package {
builtin := types.NewPackage("", "")
fmt := pkg.TryImport("fmt")
os := pkg.TryImport("os")
Expand All @@ -87,7 +87,7 @@ func newBuiltinDefault(pkg *gox.Package, conf *gox.Config) *types.Package {
initMathBig(pkg, conf, ng)
}
initBuiltin(pkg, builtin, os, fmt, ng, iox, buil)
gox.InitBuiltin(pkg, builtin, conf)
gogen.InitBuiltin(pkg, builtin, conf)
return builtin
}

Expand Down
32 changes: 16 additions & 16 deletions cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ import (
"log"
"testing"

"github.com/goplus/gogen"
"github.com/goplus/gogen/cpackages"
"github.com/goplus/gogen/packages"
"github.com/goplus/gop/ast"
"github.com/goplus/gop/token"
"github.com/goplus/gox"
"github.com/goplus/gox/cpackages"
"github.com/goplus/gox/packages"
"github.com/goplus/mod/modfile"
)

var (
goxConf = getGoxConf()
)

func getGoxConf() *gox.Config {
func getGoxConf() *gogen.Config {
fset := token.NewFileSet()
imp := packages.NewImporter(fset)
return &gox.Config{Fset: fset, Importer: imp}
return &gogen.Config{Fset: fset, Importer: imp}
}

func TestCompileLambdaExpr(t *testing.T) {
Expand All @@ -47,7 +47,7 @@ func TestCompileLambdaExpr(t *testing.T) {
lhs := []*ast.Ident{ast.NewIdent("x")}
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
e := compileLambdaExpr(ctx, &ast.LambdaExpr{Lhs: lhs}, sig)
if ce := e.(*gox.CodeError); ce.Msg != `too many arguments in lambda expression
if ce := e.(*gogen.CodeError); ce.Msg != `too many arguments in lambda expression
have (x)
want ()` {
t.Fatal("compileLambdaExpr:", ce.Msg)
Expand All @@ -57,7 +57,7 @@ func TestCompileLambdaExpr(t *testing.T) {
func TestCompileLambda1(t *testing.T) {
defer func() {
if e := recover(); e != nil {
if ce := e.(*gox.CodeError); ce.Msg != `too many arguments in lambda expression
if ce := e.(*gogen.CodeError); ce.Msg != `too many arguments in lambda expression
have (x)
want ()` {
t.Fatal("compileLambda:", ce.Msg)
Expand All @@ -75,7 +75,7 @@ func TestCompileLambda1(t *testing.T) {
func TestCompileLambda2(t *testing.T) {
defer func() {
if e := recover(); e != nil {
if ce := e.(*gox.CodeError); ce.Msg != `too many arguments in lambda expression
if ce := e.(*gogen.CodeError); ce.Msg != `too many arguments in lambda expression
have (x)
want ()` {
t.Fatal("compileLambda:", ce.Msg)
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestCompileStmt(t *testing.T) {
}

func TestTryGopExec(t *testing.T) {
pkg := gox.NewPackage("", "foo", goxConf)
pkg := gogen.NewPackage("", "foo", goxConf)
if tryGopExec(pkg.CB(), nil) {
t.Fatal("tryGopExec")
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestErrStringLit(t *testing.T) {
}

func TestErrPreloadFile(t *testing.T) {
pkg := gox.NewPackage("", "foo", goxConf)
pkg := gogen.NewPackage("", "foo", goxConf)
ctx := &blockCtx{pkgCtx: &pkgCtx{}}
t.Run("overloadName", func(t *testing.T) {
defer func() {
Expand Down Expand Up @@ -355,7 +355,7 @@ func TestCompileErrWrapExpr(t *testing.T) {
t.Fatal("TestCompileErrWrapExpr failed")
}
}()
pkg := gox.NewPackage("", "foo", goxConf)
pkg := gogen.NewPackage("", "foo", goxConf)
ctx := &blockCtx{pkg: pkg, cb: pkg.CB()}
compileErrWrapExpr(ctx, &ast.ErrWrapExpr{Tok: token.QUESTION}, 0)
}
Expand Down Expand Up @@ -401,7 +401,7 @@ func TestCheckCommandWithoutArgs(t *testing.T) {

func TestClRangeStmt(t *testing.T) {
ctx := &blockCtx{
cb: &gox.CodeBuilder{},
cb: &gogen.CodeBuilder{},
}
stmt := &ast.RangeStmt{
Tok: token.DEFINE,
Expand All @@ -417,7 +417,7 @@ func TestClRangeStmt(t *testing.T) {
// -----------------------------------------------------------------------------

func TestGetStringConst(t *testing.T) {
spx := gox.PkgRef{Types: types.NewPackage("", "foo")}
spx := gogen.PkgRef{Types: types.NewPackage("", "foo")}
if v := getStringConst(spx, "unknown"); v != "" {
t.Fatal("getStringConst:", v)
}
Expand All @@ -429,7 +429,7 @@ func TestSpxRef(t *testing.T) {
t.Fatal("TestSpxRef:", e)
}
}()
pkg := gox.PkgRef{
pkg := gogen.PkgRef{
Types: types.NewPackage("foo", "foo"),
}
spxRef(pkg, "bar")
Expand All @@ -448,7 +448,7 @@ func isError(e interface{}, msg string) bool {
}

func TestGmxProject(t *testing.T) {
pkg := gox.NewPackage("", "foo", goxConf)
pkg := gogen.NewPackage("", "foo", goxConf)
ctx := &pkgCtx{
projs: make(map[string]*gmxProject),
classes: make(map[*ast.File]*gmxClass),
Expand Down Expand Up @@ -578,7 +578,7 @@ func TestErrLoadImport(t *testing.T) {

func TestErrCompileBasicLit(t *testing.T) {
testPanic(t, "compileBasicLit: invalid syntax\n", func() {
ctx := &blockCtx{cb: new(gox.CodeBuilder)}
ctx := &blockCtx{cb: new(gogen.CodeBuilder)}
compileBasicLit(ctx, &ast.BasicLit{Kind: token.CSTRING, Value: `\\x`})
})
}
Expand Down
4 changes: 2 additions & 2 deletions cl/c.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package cl
import (
"strings"

"github.com/goplus/gogen"
"github.com/goplus/gop/ast"
"github.com/goplus/gox"
)

const (
Expand Down Expand Up @@ -57,7 +57,7 @@ func c2goBase(base string) string {

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

func loadC2goPkg(ctx *blockCtx, realPath string, src *ast.BasicLit) (ret gox.PkgRef) {
func loadC2goPkg(ctx *blockCtx, realPath string, src *ast.BasicLit) (ret gogen.PkgRef) {
cpkg, err := ctx.cpkgs.Import(realPath)
if err != nil {
ctx.handleErrorf(src.Pos(),
Expand Down

0 comments on commit 62897ea

Please sign in to comment.