Skip to content
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
28 changes: 8 additions & 20 deletions _xtool/pydump/pydump.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
package main

import (
"encoding/json"
"os"
"fmt"
"strings"
"encoding/json"
"github.com/goplus/lib/c"
"github.com/goplus/lib/py"
"github.com/goplus/lib/py/inspect"
"os"
"strings"
"github.com/goplus/llpyg/symbol"
)

type symbol struct {
Name string `json:"name"`
Type string `json:"type"`
Doc string `json:"doc"`
Sig string `json:"sig"`
}

type module struct {
Name string `json:"name"` // python module name
Functions []*symbol `json:"functions"` // package functions
// TODO: variables, classes, etc.
}

var pyFuncTypes = map[string]bool{
"ufunc": true,
"method": true,
Expand Down Expand Up @@ -50,7 +38,7 @@ func extractSignatureFromDoc(doc, funcName string) string {
return strings.Join(fields, " ")
}

func getSignature(val *py.Object, sym *symbol) string {
func getSignature(val *py.Object, sym *symbol.Symbol) string {
// function, method, class, or implement __call__
if val.Callable() == 0 {
return ""
Expand All @@ -76,7 +64,7 @@ func getSignature(val *py.Object, sym *symbol) string {
}

// moduleName: Python module name
func pydump(moduleName string) (*module, error) {
func pydump(moduleName string) (*symbol.Module, error) {
// import module
mod := py.ImportModule(c.AllocaCStr(moduleName))
if mod == nil {
Expand All @@ -88,7 +76,7 @@ func pydump(moduleName string) (*module, error) {
return nil, fmt.Errorf("failed to get dict keys of %s", moduleName)
}
// create module instance
modInstance := &module{
modInstance := &symbol.Module{
Name: moduleName,
}
// get symbols
Expand All @@ -99,7 +87,7 @@ func pydump(moduleName string) (*module, error) {
continue
}
// define symbol
sym := &symbol{}
sym := &symbol.Symbol{}
sym.Name = c.GoString(key.CStr())
sym.Type = c.GoString(val.Type().TypeName().CStr())
doc := val.GetAttrString(c.Str("__doc__"))
Expand Down
8 changes: 4 additions & 4 deletions tool/pygen/symbol.go → symbol/symbol.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package pygen
package symbol

type symbol struct {
type Symbol struct {
Name string `json:"name"`
Type string `json:"type"`
Doc string `json:"doc"`
Sig string `json:"sig"`
}
Comment thread
toaction marked this conversation as resolved.

type module struct {
type Module struct {
Name string `json:"name"` // python module name
Functions []*symbol `json:"functions"` // package functions
Functions []*Symbol `json:"functions"` // package functions
// TODO: variables, classes, etc.
}
Comment thread
toaction marked this conversation as resolved.
7 changes: 4 additions & 3 deletions tool/pygen/genfunc.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package pygen

import (
"github.com/goplus/gogen"
"go/ast"
"go/token"
"go/types"
"github.com/goplus/gogen"
"github.com/goplus/llpyg/symbol"
)

func (ctx *context) genFunc(pkg *gogen.Package, sym *symbol) {
func (ctx *context) genFunc(pkg *gogen.Package, sym *symbol.Symbol) {
name, symSig := sym.Name, sym.Sig
if len(name) == 0 || name[0] == '_' {
return
Expand All @@ -30,6 +31,6 @@ func (ctx *context) genFunc(pkg *gogen.Package, sym *symbol) {
fn.SetComments(pkg, &ast.CommentGroup{List: docList})
}

func (ctx *context) genLinkname(name string, sym *symbol) *ast.Comment {
func (ctx *context) genLinkname(name string, sym *symbol.Symbol) *ast.Comment {
return &ast.Comment{Text: "//go:linkname " + name + " py." + sym.Name}
}
9 changes: 5 additions & 4 deletions tool/pygen/pygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"go/types"
"github.com/goplus/gogen"
"github.com/goplus/llpyg/tool/pysig"
"github.com/goplus/llpyg/symbol"
)


Expand All @@ -23,7 +24,7 @@ type context struct {
objPtr *types.Pointer
ret *types.Tuple
py gogen.PkgRef
skips []symbol
skips []symbol.Symbol
}


Expand All @@ -50,7 +51,7 @@ func GenLLGoBindings(moduleName string, outFile io.Writer) {
ctx.pkg.WriteTo(outFile)
}

func pydump(moduleName string) (mod module, err error) {
func pydump(moduleName string) (mod symbol.Module, err error) {
var out bytes.Buffer
cmd := exec.Command("pydump", moduleName)
cmd.Stdout = &out
Expand All @@ -69,7 +70,7 @@ func pydump(moduleName string) (mod module, err error) {
return mod, nil
}

func createGoPackage(mod module) (ctx *context) {
func createGoPackage(mod symbol.Module) (ctx *context) {
parts := strings.Split(mod.Name, ".")
pkgName := parts[len(parts)-1]
if goKeywords[pkgName] {
Expand All @@ -92,7 +93,7 @@ func createGoPackage(mod module) (ctx *context) {
return ctx
}

func (ctx *context) genMod(pkg *gogen.Package, mod *module) {
func (ctx *context) genMod(pkg *gogen.Package, mod *symbol.Module) {
// global functions
funcMap := make(map[string]bool)
for _, sym := range mod.Functions {
Expand Down
Loading