llpyg os
This commit is contained in:
@@ -58,10 +58,18 @@ func main() {
|
|||||||
var mod module
|
var mod module
|
||||||
json.Unmarshal(out.Bytes(), &mod)
|
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"
|
pkg.Import("unsafe").MarkForceUsed(pkg) // import _ "unsafe"
|
||||||
py := pkg.Import("github.com/goplus/llgo/py") // import "github.com/goplus/llgo/py"
|
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)
|
obj := py.Ref("Object").(*types.TypeName).Type().(*types.Named)
|
||||||
objPtr := types.NewPointer(obj)
|
objPtr := types.NewPointer(obj)
|
||||||
ret := types.NewTuple(pkg.NewParam(0, "", objPtr))
|
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)
|
log.Println("skip func:", name, sym.Sig)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if c := name[0]; c >= 'a' && c <= 'z' {
|
name = genName(name, -1)
|
||||||
name = string(c+'A'-'a') + name[1:]
|
|
||||||
}
|
|
||||||
sig := types.NewSignatureType(nil, nil, nil, params, ctx.ret, variadic)
|
sig := types.NewSignatureType(nil, nil, nil, params, ctx.ret, variadic)
|
||||||
fn := pkg.NewFuncDecl(token.NoPos, name, sig)
|
fn := pkg.NewFuncDecl(token.NoPos, name, sig)
|
||||||
list := ctx.genDoc(sym.Doc)
|
list := ctx.genDoc(sym.Doc)
|
||||||
@@ -124,21 +130,46 @@ func (ctx *context) genParams(pkg *gogen.Package, sig string) (*types.Tuple, boo
|
|||||||
n--
|
n--
|
||||||
}
|
}
|
||||||
objPtr := ctx.objPtr
|
objPtr := ctx.objPtr
|
||||||
list := make([]*types.Var, n)
|
list := make([]*types.Var, 0, n)
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
part := strings.TrimSpace(parts[i])
|
part := strings.TrimSpace(parts[i])
|
||||||
|
if part == "*" {
|
||||||
|
break
|
||||||
|
}
|
||||||
if strings.HasPrefix(part, "*") {
|
if strings.HasPrefix(part, "*") {
|
||||||
if len(part) > 1 && part[1] == '*' || i != n-1 {
|
if len(part) > 1 && part[1] == '*' || i != n-1 {
|
||||||
return nil, false, true
|
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
|
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
|
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 {
|
func (ctx *context) genLinkname(name string, sym *symbol) *ast.Comment {
|
||||||
return &ast.Comment{Text: "//go:linkname " + name + " py." + sym.Name}
|
return &ast.Comment{Text: "//go:linkname " + name + " py." + sym.Name}
|
||||||
}
|
}
|
||||||
|
|||||||
1448
py/os/gen.go
Normal file
1448
py/os/gen.go
Normal file
File diff suppressed because it is too large
Load Diff
22
py/os/os.go
22
py/os/os.go
@@ -22,9 +22,21 @@ import (
|
|||||||
"github.com/goplus/llgo/py"
|
"github.com/goplus/llgo/py"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// https://docs.python.org/3/library/os.html
|
||||||
LLGoPackage = "py.os"
|
|
||||||
)
|
|
||||||
|
|
||||||
//go:linkname Getcwd py.getcwd
|
// Rename the file or directory src to dst. If dst exists, the operation will
|
||||||
func Getcwd() *py.Object
|
// 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
|
||||||
|
|||||||
Reference in New Issue
Block a user