-
Notifications
You must be signed in to change notification settings - Fork 3
[WIP]feat: add signature source #41
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "encoding/json" | ||
|
|
||
| "github.com/goplus/lib/c" | ||
| "github.com/goplus/lib/py" | ||
| "github.com/goplus/lib/py/inspect" | ||
|
|
@@ -38,55 +39,57 @@ func extractSignatureFromDoc(doc, funcName string) string { | |
| return strings.Join(fields, " ") | ||
| } | ||
|
|
||
| func getSignature(val *py.Object, sym *symbol.Symbol) string { | ||
| // function, method, class, or implement __call__ | ||
| func getSignature(val *py.Object, sym *symbol.Symbol) (*symbol.Signature, error) { | ||
| // which implement __call__ | ||
| if val.Callable() == 0 { | ||
| return "" | ||
| return nil, fmt.Errorf("not callable") | ||
| } | ||
| // get signature from inspect | ||
| // inspect | ||
| sigFromInspect := inspect.Signature(val) | ||
| if sigFromInspect != nil { | ||
| sig := c.GoString(sigFromInspect.Str().CStr()) | ||
| if sig != "(*args, **kwargs)" { | ||
| return sig | ||
| } | ||
| return &symbol.Signature{ | ||
| Source: symbol.SigSourceInspect, | ||
| Str: sig, | ||
| }, nil | ||
| } | ||
| // get signature from doc | ||
| // doc | ||
| sigFromDoc := extractSignatureFromDoc(sym.Doc, sym.Name) | ||
| if sigFromDoc != "" { | ||
| return sigFromDoc | ||
| return &symbol.Signature{ | ||
| Source: symbol.SigSourceDoc, | ||
| Str: sigFromDoc, | ||
| }, nil | ||
| } | ||
| // Paradigms | ||
| if pyFuncTypes[sym.Type] { | ||
| return "(*args, **kwargs)" | ||
| return &symbol.Signature{ | ||
| Source: symbol.SigSourceParadigm, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as i'm concerned, the usage of signature source here, is only to indicate to remove first param of functions which are from In conclusion, the only usage of signature source is to make some one know it's from Hence, why not handle it here? It seems that exporting signature source is unnecessary.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Meanwhile, I also have a suggestion: the current architecture extracts all signature strings first, then parses these strings during the pygen phase to obtain structured data before generating code. I think this step should also be moved forward to the pydump stage, so that pygen only receives the processed structured data and can focus solely on Go code generation. |
||
| Str: "(*args, **kwargs)", | ||
| }, nil | ||
| } | ||
| return "" | ||
| return nil, fmt.Errorf("failed to get signature") | ||
| } | ||
|
|
||
| // moduleName: Python module name | ||
| func pydump(moduleName string) (*symbol.Module, error) { | ||
| // import module | ||
| mod := py.ImportModule(c.AllocaCStr(moduleName)) | ||
| if mod == nil { | ||
| return nil, fmt.Errorf("failed to import module %s", moduleName) | ||
| } | ||
| // get dict, python list Object | ||
| keys := mod.ModuleGetDict().DictKeys() | ||
| if keys == nil { | ||
| return nil, fmt.Errorf("failed to get dict keys of %s", moduleName) | ||
| } | ||
| // create module instance | ||
| modInstance := &symbol.Module{ | ||
| Name: moduleName, | ||
| } | ||
| // get symbols | ||
| for i, n := 0, keys.ListLen(); i < n; i++ { | ||
| key := keys.ListItem(i) | ||
| val := mod.GetAttr(key) | ||
| if val == nil { | ||
| continue | ||
| } | ||
| // define symbol | ||
| sym := &symbol.Symbol{} | ||
| sym.Name = c.GoString(key.CStr()) | ||
| sym.Type = c.GoString(val.Type().TypeName().CStr()) | ||
|
|
@@ -96,7 +99,11 @@ func pydump(moduleName string) (*symbol.Module, error) { | |
| } | ||
| // functions | ||
| if pyFuncTypes[sym.Type] { | ||
| sym.Sig = getSignature(val, sym) | ||
| sig, err := getSignature(val, sym) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
toaction marked this conversation as resolved.
|
||
| sym.Sig = *sig | ||
| modInstance.Functions = append(modInstance.Functions, sym) | ||
| } | ||
| // TODO: variables, classes, etc. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.