This commit is contained in:
xushiwei
2024-05-14 23:56:55 +08:00
parent e9570f2400
commit 44fe7e8dc4
6 changed files with 10216 additions and 13 deletions

View File

@@ -43,9 +43,11 @@ func main() {
val := mod.GetAttr(key) val := mod.GetAttr(key)
doc := val.GetAttrString(c.Str("__doc__")) doc := val.GetAttrString(c.Str("__doc__"))
sym := cjson.Object() sym := cjson.Object()
sym.SetItem(c.Str("type"), cjson.String(val.Type().Name().CStr())) sym.SetItem(c.Str("type"), cjson.String(val.Type().TypeName().CStr()))
sym.SetItem(c.Str("name"), cjson.String(key.CStr())) sym.SetItem(c.Str("name"), cjson.String(key.CStr()))
sym.SetItem(c.Str("doc"), cjson.String(doc.CStr())) if doc != nil {
sym.SetItem(c.Str("doc"), cjson.String(doc.CStr()))
}
if val.Callable() != 0 { if val.Callable() != 0 {
sig := inspect.Signature(val) sig := inspect.Signature(val)
sym.SetItem(c.Str("sig"), cjson.String(sig.Str().CStr())) sym.SetItem(c.Str("sig"), cjson.String(sig.Str().CStr()))

View File

@@ -81,10 +81,10 @@ func main() {
ctx := &context{pkg, obj, objPtr, ret, py} ctx := &context{pkg, obj, objPtr, ret, py}
for _, sym := range mod.Items { for _, sym := range mod.Items {
switch sym.Type { switch sym.Type {
case "builtin_function_or_method", "function": case "builtin_function_or_method", "function", "ufunc":
ctx.genFunc(pkg, sym) ctx.genFunc(pkg, sym)
case "str", "float", "bool", "type", "dict", "tuple", "list", case "str", "float", "bool", "type", "dict", "tuple", "list",
"module", "int", "set", "frozenset", "flags": // skip "module", "int", "set", "frozenset", "flags", "bool_": // skip
default: default:
t := sym.Type t := sym.Type
if len(t) > 0 && (t[0] >= 'a' && t[0] <= 'z') && !strings.HasSuffix(t, "_info") { if len(t) > 0 && (t[0] >= 'a' && t[0] <= 'z') && !strings.HasSuffix(t, "_info") {
@@ -104,14 +104,14 @@ type context struct {
} }
func (ctx *context) genFunc(pkg *gogen.Package, sym *symbol) { func (ctx *context) genFunc(pkg *gogen.Package, sym *symbol) {
name := sym.Name name, symSig := sym.Name, sym.Sig
if len(name) == 0 || name[0] == '_' { if len(name) == 0 || name[0] == '_' {
return return
} }
params, variadic, skip := ctx.genParams(pkg, sym.Sig) params, variadic, skip := ctx.genParams(pkg, symSig)
if skip { if skip {
// TODO(xsw): don't skip any func // TODO(xsw): don't skip any func
log.Println("skip func:", name, sym.Sig) log.Println("skip func:", name, symSig)
return return
} }
name = genName(name, -1) name = genName(name, -1)
@@ -145,14 +145,17 @@ func (ctx *context) genParams(pkg *gogen.Package, sig string) (*types.Tuple, boo
break break
} }
if strings.HasPrefix(part, "*") { if strings.HasPrefix(part, "*") {
if len(part) > 1 && part[1] == '*' || i != n-1 { if part[1] != '*' {
return nil, false, true list = append(list, pkg.NewParam(0, genName(part[1:], 0), types.NewSlice(objPtr)))
return types.NewTuple(list...), true, false
} }
list = append(list, pkg.NewParam(0, genName(part[1:], 0), types.NewSlice(objPtr))) return types.NewTuple(list...), false, false
return types.NewTuple(list...), true, false
} }
pos := strings.IndexByte(part, '=') pos := strings.IndexByte(part, '=')
if pos >= 0 { if pos >= 0 {
if strings.HasPrefix(part[pos+1:], "<") { // skip complex default value
return nil, false, true
}
part = part[:pos] part = part[:pos]
} }
list = append(list, pkg.NewParam(0, genName(part, 0), objPtr)) list = append(list, pkg.NewParam(0, genName(part, 0), objPtr))

View File

@@ -25,7 +25,7 @@ int main() {
Py_Initialize(); Py_Initialize();
PyObject* inspect = PyImport_ImportModule("inspect"); PyObject* inspect = PyImport_ImportModule("inspect");
PyObject* signature = PyObject_GetAttrString(inspect, "signature"); PyObject* signature = PyObject_GetAttrString(inspect, "signature");
PyObject* mod = PyImport_ImportModule("math"); PyObject* mod = PyImport_ImportModule("numpy");
PyObject* dict = PyModule_GetDict(mod); PyObject* dict = PyModule_GetDict(mod);
PyObject* keys = PyDict_Keys(dict); PyObject* keys = PyDict_Keys(dict);
size_t i, n; size_t i, n;
@@ -39,7 +39,10 @@ int main() {
printf("-----------------------------------\n"); printf("-----------------------------------\n");
printf("sig: %p\n", sig); printf("sig: %p\n", sig);
printf("%s: %s\n", PyUnicode_AsUTF8(key), PyUnicode_AsUTF8(PyObject_Str(sig))); printf("%s: %s\n", PyUnicode_AsUTF8(key), PyUnicode_AsUTF8(PyObject_Str(sig)));
printf("%s\n", PyUnicode_AsUTF8(doc)); printf("%s\n", PyUnicode_AsUTF8(key));
if (doc != NULL) {
printf("%s\n", PyUnicode_AsUTF8(doc));
}
} }
} }
return 0; return 0;

10195
py/numpy/gen.go Normal file

File diff suppressed because it is too large Load Diff

BIN
py/numpy/llgo_autogen.lla Normal file

Binary file not shown.

BIN
py/sys/llgo_autogen.lla Normal file

Binary file not shown.