Skip to content

Commit d18de61

Browse files
authored
Merge pull request #2124 from xushiwei/q
gopbuiltingen: import
2 parents 7ec369d + 849fdcc commit d18de61

File tree

5 files changed

+107
-29
lines changed

5 files changed

+107
-29
lines changed

builtin/doc/builtin.gop

+30-22
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
package builtin
22

3-
// echo formats using the default formats for its operands and writes to standard output.
3+
import (
4+
"gop/builtin"
5+
"gop/builtin/iox"
6+
"io"
7+
"os"
8+
"reflect"
9+
)
10+
11+
// Echo formats using the default formats for its operands and writes to standard output.
412
// Spaces are always added between operands and a newline is appended.
513
// It returns the number of bytes written and any write error encountered.
614
func Echo(a ...any) (n int, err error)
7-
func Blines(r io.Reader) BLineReader
15+
func Blines(r io.Reader) iox.BLineReader
816

9-
// type returns the reflection [Type] that represents the dynamic type of i.
10-
// If i is a nil interface value, type returns nil.
11-
func Type(i any) Type
12-
func NewRange(start, end, step int) *IntRange
17+
// Type returns the reflection [Type] that represents the dynamic type of i.
18+
// If i is a nil interface value, Type returns nil.
19+
func Type(i any) reflect.Type
20+
func NewRange(start, end, step int) *builtin.IntRange
1321

14-
// print formats using the default formats for its operands and writes to standard output.
22+
// Print formats using the default formats for its operands and writes to standard output.
1523
// Spaces are added between operands when neither is a string.
1624
// It returns the number of bytes written and any write error encountered.
1725
func Print(a ...any) (n int, err error)
1826

19-
// println formats using the default formats for its operands and writes to standard output.
27+
// Println formats using the default formats for its operands and writes to standard output.
2028
// Spaces are always added between operands and a newline is appended.
2129
// It returns the number of bytes written and any write error encountered.
2230
func Println(a ...any) (n int, err error)
2331

24-
// printf formats according to a format specifier and writes to standard output.
32+
// Printf formats according to a format specifier and writes to standard output.
2533
// It returns the number of bytes written and any write error encountered.
2634
func Printf(format string, a ...any) (n int, err error)
2735

28-
// errorf formats according to a format specifier and returns the string as a
36+
// Errorf formats according to a format specifier and returns the string as a
2937
// value that satisfies error.
3038
//
3139
// If the format specifier includes a %w verb with an error operand,
@@ -37,45 +45,45 @@ func Printf(format string, a ...any) (n int, err error)
3745
// the error interface. The %w verb is otherwise a synonym for %v.
3846
func Errorf(format string, a ...any) error
3947

40-
// fprint formats using the default formats for its operands and writes to w.
48+
// Fprint formats using the default formats for its operands and writes to w.
4149
// Spaces are added between operands when neither is a string.
4250
// It returns the number of bytes written and any write error encountered.
4351
func Fprint(w io.Writer, a ...any) (n int, err error)
4452

45-
// fprintln formats using the default formats for its operands and writes to w.
53+
// Fprintln formats using the default formats for its operands and writes to w.
4654
// Spaces are always added between operands and a newline is appended.
4755
// It returns the number of bytes written and any write error encountered.
4856
func Fprintln(w io.Writer, a ...any) (n int, err error)
4957

50-
// fprintf formats according to a format specifier and writes to w.
58+
// Fprintf formats according to a format specifier and writes to w.
5159
// It returns the number of bytes written and any write error encountered.
5260
func Fprintf(w io.Writer, format string, a ...any) (n int, err error)
5361

54-
// sprint formats using the default formats for its operands and returns the resulting string.
62+
// Sprint formats using the default formats for its operands and returns the resulting string.
5563
// Spaces are added between operands when neither is a string.
5664
func Sprint(a ...any) string
5765

58-
// sprintln formats using the default formats for its operands and returns the resulting string.
66+
// Sprintln formats using the default formats for its operands and returns the resulting string.
5967
// Spaces are always added between operands and a newline is appended.
6068
func Sprintln(a ...any) string
6169

62-
// sprintf formats according to a format specifier and returns the resulting string.
70+
// Sprintf formats according to a format specifier and returns the resulting string.
6371
func Sprintf(format string, a ...any) string
6472

65-
// open opens the named file for reading. If successful, methods on
73+
// Open opens the named file for reading. If successful, methods on
6674
// the returned file can be used for reading; the associated file
6775
// descriptor has mode O_RDONLY.
6876
// If there is an error, it will be of type *PathError.
69-
func Open(name string) (*File, error)
77+
func Open(name string) (*os.File, error)
7078

71-
// create creates or truncates the named file. If the file already exists,
79+
// Create creates or truncates the named file. If the file already exists,
7280
// it is truncated. If the file does not exist, it is created with mode 0666
7381
// (before umask). If successful, methods on the returned File can
7482
// be used for I/O; the associated file descriptor has mode O_RDWR.
7583
// If there is an error, it will be of type *PathError.
76-
func Create(name string) (*File, error)
77-
func Lines(r io.Reader) LineReader
78-
func (io.Reader) Gop_Enum() LineIter
84+
func Create(name string) (*os.File, error)
85+
func Lines(r io.Reader) iox.LineReader
86+
func (io.Reader) Gop_Enum() iox.LineIter
7987

8088
// String converts the floating-point number f to a string,
8189
// according to the format fmt and precision prec. It rounds the

cmd/chore/gopbuiltingen/builtin.gox

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ var (
99

1010
func genAST() *ast.FuncDecl {
1111
ref := Fn.getAST()
12+
newName := Name.capitalize
1213
return {
13-
Doc: reference.docFor(ref.Doc, Fn.Name, Name),
14-
Name: ast.newIdent(Name.capitalize),
15-
Type: ref.Type,
14+
Doc: reference.docFor(ref.Doc, Fn.Name, newName),
15+
Name: ast.newIdent(newName),
16+
Type: reference.toType(ref.Type, Fn.Pkg),
1617
}
1718
}
1819

1920
func genMethodAST(methods []builtin) *ast.FuncDecl {
21+
at := Fn.Pkg
2022
ref := Fn.getAST()
21-
mt, recvType := reference.toMethodType(ref.Type)
22-
if Fn.Pkg == "" { // builtin
23+
mt, recvType := reference.toMethodType(ref.Type, at)
24+
if at == "" { // builtin
2325
recvType = methods[methods.len-1].genAST().Type.Params.List[0].Type
2426
}
2527
return {

cmd/chore/gopbuiltingen/builtingen.gox

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import (
22
"bytes"
33
"go/ast"
44
"go/format"
5+
"go/token"
56
"gop/ast/gopq"
67
"gop/parser"
78
"os"
@@ -21,6 +22,16 @@ func gen() []byte {
2122
Name: ast.newIdent("builtin"),
2223
Decls: make([]ast.Decl, 0, 128),
2324
}
25+
f.Decls <- &ast.GenDecl{
26+
Tok: token.IMPORT,
27+
Specs: []ast.Spec{
28+
importSpec("gop/builtin"),
29+
importSpec("gop/builtin/iox"),
30+
importSpec("io"),
31+
importSpec("os"),
32+
importSpec("reflect"),
33+
},
34+
}
2435
genDecls f
2536
b := new(bytes.Buffer)
2637
format.node! b, fset, f

cmd/chore/gopbuiltingen/helper.gop

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import (
2+
"go/ast"
3+
)
4+
5+
func importSpec(path string) *ast.ImportSpec {
6+
return {
7+
Path: {
8+
Value: path.quote,
9+
},
10+
}
11+
}
12+
13+
func toParams(params *ast.FieldList, at string) *ast.FieldList {
14+
if params == nil {
15+
return nil
16+
}
17+
if at == "buil" {
18+
at = "builtin"
19+
}
20+
list := make([]*ast.Field, len(params.List))
21+
for i, p <- params.List {
22+
typ := p.Type
23+
switch t := typ.(type) {
24+
case *ast.Ident:
25+
if t.isExported {
26+
typ = &ast.SelectorExpr{
27+
X: ast.newIdent(at),
28+
Sel: t,
29+
}
30+
}
31+
case *ast.StarExpr:
32+
if x, ok := t.X.(*ast.Ident); ok && x.isExported {
33+
typ = &ast.StarExpr{
34+
X: &ast.SelectorExpr{
35+
X: ast.newIdent(at),
36+
Sel: x,
37+
},
38+
}
39+
}
40+
}
41+
list[i] = {
42+
Doc: p.Doc,
43+
Names: p.Names,
44+
Type: typ,
45+
Tag: p.Tag,
46+
Comment: p.Comment,
47+
}
48+
}
49+
return {List: list}
50+
}

cmd/chore/gopbuiltingen/reference.gox

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ var (
1111
Name string
1212
)
1313

14-
func .toMethodType(t *ast.FuncType) (mt *ast.FuncType, recvType ast.Expr) {
14+
func .toType(t *ast.FuncType, at string) *ast.FuncType {
15+
return {
16+
Params: t.Params,
17+
Results: toParams(t.Results, at),
18+
}
19+
}
20+
21+
func .toMethodType(t *ast.FuncType, at string) (mt *ast.FuncType, recvType ast.Expr) {
1522
list := t.Params.List
1623
first := list[0]
1724
recvType = first.Type
@@ -27,7 +34,7 @@ func .toMethodType(t *ast.FuncType) (mt *ast.FuncType, recvType ast.Expr) {
2734
}
2835
mt = {
2936
Params: {List: list},
30-
Results: t.Results,
37+
Results: toParams(t.Results, at),
3138
}
3239
return
3340
}

0 commit comments

Comments
 (0)