Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gopbuiltingen: import #2124

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions builtin/doc/builtin.gop
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
package builtin

// echo formats using the default formats for its operands and writes to standard output.
import (
"gop/builtin"
"gop/builtin/iox"
"io"
"os"
"reflect"
)

// Echo formats using the default formats for its operands and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func Echo(a ...any) (n int, err error)
func Blines(r io.Reader) BLineReader
func Blines(r io.Reader) iox.BLineReader

// type returns the reflection [Type] that represents the dynamic type of i.
// If i is a nil interface value, type returns nil.
func Type(i any) Type
func NewRange(start, end, step int) *IntRange
// Type returns the reflection [Type] that represents the dynamic type of i.
// If i is a nil interface value, Type returns nil.
func Type(i any) reflect.Type
func NewRange(start, end, step int) *builtin.IntRange

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

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

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

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

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

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

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

// sprint formats using the default formats for its operands and returns the resulting string.
// Sprint formats using the default formats for its operands and returns the resulting string.
// Spaces are added between operands when neither is a string.
func Sprint(a ...any) string

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

// sprintf formats according to a format specifier and returns the resulting string.
// Sprintf formats according to a format specifier and returns the resulting string.
func Sprintf(format string, a ...any) string

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

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

// String converts the floating-point number f to a string,
// according to the format fmt and precision prec. It rounds the
Expand Down
12 changes: 7 additions & 5 deletions cmd/chore/gopbuiltingen/builtin.gox
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ var (

func genAST() *ast.FuncDecl {
ref := Fn.getAST()
newName := Name.capitalize
return {
Doc: reference.docFor(ref.Doc, Fn.Name, Name),
Name: ast.newIdent(Name.capitalize),
Type: ref.Type,
Doc: reference.docFor(ref.Doc, Fn.Name, newName),
Name: ast.newIdent(newName),
Type: reference.toType(ref.Type, Fn.Pkg),
}
}

func genMethodAST(methods []builtin) *ast.FuncDecl {
at := Fn.Pkg
ref := Fn.getAST()
mt, recvType := reference.toMethodType(ref.Type)
if Fn.Pkg == "" { // builtin
mt, recvType := reference.toMethodType(ref.Type, at)
if at == "" { // builtin
recvType = methods[methods.len-1].genAST().Type.Params.List[0].Type
}
return {
Expand Down
11 changes: 11 additions & 0 deletions cmd/chore/gopbuiltingen/builtingen.gox
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import (
"bytes"
"go/ast"
"go/format"
"go/token"
"gop/ast/gopq"
"gop/parser"
"os"
Expand All @@ -21,6 +22,16 @@ func gen() []byte {
Name: ast.newIdent("builtin"),
Decls: make([]ast.Decl, 0, 128),
}
f.Decls <- &ast.GenDecl{
Tok: token.IMPORT,
Specs: []ast.Spec{
importSpec("gop/builtin"),
importSpec("gop/builtin/iox"),
importSpec("io"),
importSpec("os"),
importSpec("reflect"),
},
}
genDecls f
b := new(bytes.Buffer)
format.node! b, fset, f
Expand Down
50 changes: 50 additions & 0 deletions cmd/chore/gopbuiltingen/helper.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import (
"go/ast"
)

func importSpec(path string) *ast.ImportSpec {
return {
Path: {
Value: path.quote,
},
}
}

func toParams(params *ast.FieldList, at string) *ast.FieldList {
if params == nil {
return nil
}
if at == "buil" {
at = "builtin"
}
list := make([]*ast.Field, len(params.List))
for i, p <- params.List {
typ := p.Type
switch t := typ.(type) {
case *ast.Ident:
if t.isExported {
typ = &ast.SelectorExpr{
X: ast.newIdent(at),
Sel: t,
}
}
case *ast.StarExpr:
if x, ok := t.X.(*ast.Ident); ok && x.isExported {
typ = &ast.StarExpr{
X: &ast.SelectorExpr{
X: ast.newIdent(at),
Sel: x,
},
}
}
}
list[i] = {
Doc: p.Doc,
Names: p.Names,
Type: typ,
Tag: p.Tag,
Comment: p.Comment,
}
}
return {List: list}
}
11 changes: 9 additions & 2 deletions cmd/chore/gopbuiltingen/reference.gox
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ var (
Name string
)

func .toMethodType(t *ast.FuncType) (mt *ast.FuncType, recvType ast.Expr) {
func .toType(t *ast.FuncType, at string) *ast.FuncType {
return {
Params: t.Params,
Results: toParams(t.Results, at),
}
}

func .toMethodType(t *ast.FuncType, at string) (mt *ast.FuncType, recvType ast.Expr) {
list := t.Params.List
first := list[0]
recvType = first.Type
Expand All @@ -27,7 +34,7 @@ func .toMethodType(t *ast.FuncType) (mt *ast.FuncType, recvType ast.Expr) {
}
mt = {
Params: {List: list},
Results: t.Results,
Results: toParams(t.Results, at),
}
return
}
Expand Down