This commit is contained in:
xushiwei
2024-05-14 19:19:46 +08:00
parent 6340ff7da0
commit 0c31300578
3 changed files with 1503 additions and 12 deletions

View File

@@ -58,10 +58,18 @@ func main() {
var mod module
json.Unmarshal(out.Bytes(), &mod)
pkg := gogen.NewPackage("", mod.Name, nil)
modName := mod.Name
pkg := gogen.NewPackage("", modName, nil)
pkg.Import("unsafe").MarkForceUsed(pkg) // import _ "unsafe"
py := pkg.Import("github.com/goplus/llgo/py") // import "github.com/goplus/llgo/py"
f := func(cb *gogen.CodeBuilder) int {
cb.Val("py." + modName)
return 1
}
defs := pkg.NewConstDefs(pkg.Types.Scope())
defs.New(f, 0, 0, nil, "LLGoPackage")
obj := py.Ref("Object").(*types.TypeName).Type().(*types.Named)
objPtr := types.NewPointer(obj)
ret := types.NewTuple(pkg.NewParam(0, "", objPtr))
@@ -101,9 +109,7 @@ func (ctx *context) genFunc(pkg *gogen.Package, sym *symbol) {
log.Println("skip func:", name, sym.Sig)
return
}
if c := name[0]; c >= 'a' && c <= 'z' {
name = string(c+'A'-'a') + name[1:]
}
name = genName(name, -1)
sig := types.NewSignatureType(nil, nil, nil, params, ctx.ret, variadic)
fn := pkg.NewFuncDecl(token.NoPos, name, sig)
list := ctx.genDoc(sym.Doc)
@@ -124,21 +130,46 @@ func (ctx *context) genParams(pkg *gogen.Package, sig string) (*types.Tuple, boo
n--
}
objPtr := ctx.objPtr
list := make([]*types.Var, n)
list := make([]*types.Var, 0, n)
for i := 0; i < n; i++ {
part := strings.TrimSpace(parts[i])
if part == "*" {
break
}
if strings.HasPrefix(part, "*") {
if len(part) > 1 && part[1] == '*' || i != n-1 {
return nil, false, true
}
list[i] = pkg.NewParam(0, part[1:], types.NewSlice(objPtr))
list = append(list, pkg.NewParam(0, genName(part[1:], 0), types.NewSlice(objPtr)))
return types.NewTuple(list...), true, false
}
list[i] = pkg.NewParam(0, part, objPtr)
pos := strings.IndexByte(part, '=')
if pos >= 0 {
part = part[:pos]
}
list = append(list, pkg.NewParam(0, genName(part, 0), objPtr))
}
return types.NewTuple(list...), false, false
}
func genName(name string, idxDontTitle int) string {
parts := strings.Split(name, "_")
for i, part := range parts {
if i != idxDontTitle && part != "" {
if c := part[0]; c >= 'a' && c <= 'z' {
part = string(c+'A'-'a') + part[1:]
}
parts[i] = part
}
}
name = strings.Join(parts, "")
switch name {
case "default", "":
name += "_"
}
return name
}
func (ctx *context) genLinkname(name string, sym *symbol) *ast.Comment {
return &ast.Comment{Text: "//go:linkname " + name + " py." + sym.Name}
}

1448
py/os/gen.go Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -22,9 +22,21 @@ import (
"github.com/goplus/llgo/py"
)
const (
LLGoPackage = "py.os"
)
// https://docs.python.org/3/library/os.html
//go:linkname Getcwd py.getcwd
func Getcwd() *py.Object
// Rename the file or directory src to dst. If dst exists, the operation will
// fail with an OSError subclass in a number of cases:
//
// On Windows, if dst exists a FileExistsError is always raised. The operation
// may fail if src and dst are on different filesystems. Use shutil.move() to
// support moves to a different filesystem.
//
// On Unix, if src is a file and dst is a directory or vice-versa, an IsADirectoryError
// or a NotADirectoryError will be raised respectively. If both are directories and dst
// is empty, dst will be silently replaced. If dst is a non-empty directory, an OSError
// is raised. If both are files, dst will be replaced silently if the user has permission.
// The operation may fail on some Unix flavors if src and dst are on different filesystems.
// If successful, the renaming will be an atomic operation (this is a POSIX requirement).
//
//go:linkname Rename py.rename
func Rename(src, dst *py.Object) *py.Object