Skip to content

Commit 0ba53b8

Browse files
committed
cl: universe scope
1 parent b0affac commit 0ba53b8

File tree

2 files changed

+129
-43
lines changed

2 files changed

+129
-43
lines changed

Diff for: cl/builtin.go

+98-43
Original file line numberDiff line numberDiff line change
@@ -31,68 +31,123 @@ func initMathBig(_ *gogen.Package, conf *gogen.Config, big gogen.PkgRef) {
3131
conf.UntypedBigFloat = big.Ref("UntypedBigfloat").Type().(*types.Named)
3232
}
3333

34-
func initBuiltinFns(builtin *types.Package, scope *types.Scope, pkg gogen.PkgRef, fns []string) {
35-
for _, fn := range fns {
36-
fnTitle := string(fn[0]-'a'+'A') + fn[1:]
37-
scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, fn, pkg.Ref(fnTitle)))
34+
type Builtin struct {
35+
types.Object
36+
name string
37+
pkg string
38+
sym string
39+
}
40+
41+
func (t *Builtin) Parent() *types.Scope {
42+
return Universe
43+
}
44+
func (t *Builtin) Pos() token.Pos {
45+
return token.NoPos
46+
}
47+
func (t *Builtin) Pkg() *types.Package {
48+
return nil
49+
}
50+
func (t *Builtin) Name() string {
51+
return t.name
52+
}
53+
func (t *Builtin) Type() types.Type {
54+
return types.Typ[types.Invalid]
55+
}
56+
func (t *Builtin) Exported() bool {
57+
return false
58+
}
59+
func (t *Builtin) Id() string {
60+
return "_." + t.name
61+
}
62+
func (t *Builtin) String() string {
63+
return "builtin " + t.name
64+
}
65+
func (t *Builtin) Sym() string {
66+
return t.pkg + "." + t.sym
67+
}
68+
69+
var (
70+
Universe *types.Scope
71+
)
72+
73+
var builtinDefs = [...]struct {
74+
name string
75+
pkg string
76+
sym string
77+
}{
78+
{"bigint", "github.com/goplus/gop/builtin/ng", ""},
79+
{"bigrat", "github.com/goplus/gop/builtin/ng", ""},
80+
{"bigfloat", "github.com/goplus/gop/builtin/ng", ""},
81+
{"int128", "github.com/goplus/gop/builtin/ng", ""},
82+
{"uint128", "github.com/goplus/gop/builtin/ng", ""},
83+
{"lines", "github.com/goplus/gop/builtin/iox", ""},
84+
{"blines", "github.com/goplus/gop/builtin/iox", "BLines"},
85+
{"newRange", "github.com/goplus/gop/builtin", "NewRange__0"},
86+
{"echo", "fmt", "Println"},
87+
{"print", "fmt", ""},
88+
{"println", "fmt", ""},
89+
{"printf", "fmt", ""},
90+
{"errorf", "fmt", ""},
91+
{"fprint", "fmt", ""},
92+
{"fprintln", "fmt", ""},
93+
{"sprint", "fmt", ""},
94+
{"sprintln", "fmt", ""},
95+
{"sprintf", "fmt", ""},
96+
{"open", "os", ""},
97+
{"create", "os", ""},
98+
{"type", "reflect", "TypeOf"},
99+
}
100+
101+
type defSym struct {
102+
name string
103+
sym string
104+
}
105+
106+
var (
107+
builtinSym map[string][]defSym
108+
)
109+
110+
func init() {
111+
Universe = types.NewScope(nil, 0, 0, "universe")
112+
builtinSym = make(map[string][]defSym)
113+
for _, def := range builtinDefs {
114+
if def.sym == "" {
115+
def.sym = string(def.name[0]-('a'-'A')) + def.name[1:]
116+
}
117+
builtinSym[def.pkg] = append(builtinSym[def.pkg], defSym{name: def.name, sym: def.sym})
118+
obj := &Builtin{name: def.name, pkg: def.pkg, sym: def.sym}
119+
Universe.Insert(obj)
38120
}
39121
}
40122

41-
func initBuiltin(_ *gogen.Package, builtin *types.Package, os, fmt, ng, iox, buil, reflect gogen.PkgRef) {
123+
func initBuiltin(pkg *gogen.Package, builtin *types.Package) {
42124
scope := builtin.Scope()
43-
if ng.Types != nil {
44-
typs := []string{"bigint", "bigrat", "bigfloat"}
45-
for _, typ := range typs {
46-
name := string(typ[0]-('a'-'A')) + typ[1:]
47-
scope.Insert(types.NewTypeName(token.NoPos, builtin, typ, ng.Ref(name).Type()))
125+
for im, defs := range builtinSym {
126+
if p := pkg.TryImport(im); p.Types != nil {
127+
for _, def := range defs {
128+
obj := p.Ref(def.sym)
129+
if _, ok := obj.Type().(*types.Named); ok {
130+
scope.Insert(types.NewTypeName(token.NoPos, builtin, def.name, obj.Type()))
131+
} else {
132+
scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, def.name, obj))
133+
}
134+
}
48135
}
49-
scope.Insert(types.NewTypeName(token.NoPos, builtin, "uint128", ng.Ref("Uint128").Type()))
50-
scope.Insert(types.NewTypeName(token.NoPos, builtin, "int128", ng.Ref("Int128").Type()))
51-
}
52-
if fmt.Types != nil {
53-
scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, "echo", fmt.Ref("Println")))
54-
initBuiltinFns(builtin, scope, fmt, []string{
55-
"print", "println", "printf", "errorf",
56-
"fprint", "fprintln", "fprintf",
57-
"sprint", "sprintln", "sprintf",
58-
})
59-
}
60-
if os.Types != nil {
61-
initBuiltinFns(builtin, scope, os, []string{
62-
"open", "create",
63-
})
64-
}
65-
if iox.Types != nil {
66-
initBuiltinFns(builtin, scope, iox, []string{
67-
"lines",
68-
})
69-
scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, "blines", iox.Ref("BLines")))
70-
}
71-
if reflect.Types != nil {
72-
scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, "type", reflect.Ref("TypeOf")))
73-
}
74-
if buil.Types != nil {
75-
scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, "newRange", buil.Ref("NewRange__0")))
76136
}
77137
scope.Insert(types.NewTypeName(token.NoPos, builtin, "any", gogen.TyEmptyInterface))
78138
}
79139

80140
func newBuiltinDefault(pkg *gogen.Package, conf *gogen.Config) *types.Package {
81141
builtin := types.NewPackage("", "")
82-
fmt := pkg.TryImport("fmt")
83-
os := pkg.TryImport("os")
84-
reflect := pkg.TryImport("reflect")
85-
buil := pkg.TryImport("github.com/goplus/gop/builtin")
86142
ng := pkg.TryImport("github.com/goplus/gop/builtin/ng")
87-
iox := pkg.TryImport("github.com/goplus/gop/builtin/iox")
88143
strx := pkg.TryImport("github.com/qiniu/x/stringutil")
89144
stringslice := pkg.TryImport("github.com/goplus/gop/builtin/stringslice")
90145
pkg.TryImport("strconv")
91146
pkg.TryImport("strings")
92147
if ng.Types != nil {
93148
initMathBig(pkg, conf, ng)
94149
}
95-
initBuiltin(pkg, builtin, os, fmt, ng, iox, buil, reflect)
150+
initBuiltin(pkg, builtin)
96151
gogen.InitBuiltin(pkg, builtin, conf)
97152
if strx.Types != nil {
98153
ti := pkg.BuiltinTI(types.Typ[types.String])

Diff for: cl/builtin_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -603,4 +603,35 @@ func TestClassFileEnd(t *testing.T) {
603603
}
604604
}
605605

606+
func TestUniverse(t *testing.T) {
607+
echo := Universe.Lookup("echo")
608+
if echo == nil {
609+
t.Fatal("not found")
610+
}
611+
if echo.Parent() != Universe {
612+
t.Fatal("bad parent")
613+
}
614+
if echo.Pos() != token.NoPos {
615+
t.Fatal("must nopos")
616+
}
617+
if echo.Pkg() != nil {
618+
t.Fatal("must nil")
619+
}
620+
if echo.Type() != types.Typ[types.Invalid] {
621+
t.Fatal("must invalid")
622+
}
623+
if echo.Name() != "echo" {
624+
t.Fatal("bad name")
625+
}
626+
if echo.Id() != "_.echo" {
627+
t.Fatal("bad id")
628+
}
629+
if echo.String() != "builtin echo" {
630+
t.Fatal("bad string")
631+
}
632+
if b, ok := echo.(*Builtin); !ok || b.Sym() != "fmt.Println" {
633+
t.Fatal("bad sym")
634+
}
635+
}
636+
606637
// -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)