diff --git a/mapgen/main.go b/mapgen/main.go index 6c78f7b..e1f644e 100644 --- a/mapgen/main.go +++ b/mapgen/main.go @@ -33,15 +33,20 @@ type StructSymbol struct { } type TopLevelSymbolScope struct { - Structs map[string]StructSymbol `json:"structs"` - Functions map[string]FunctionSymbol `json:"functions"` + PackageName string `json:"package_name"` + Structs map[string]StructSymbol `json:"structs"` + Functions map[string]FunctionSymbol `json:"functions"` } func main() { cfg := &packages.Config{ - Mode: packages.NeedTypes | packages.NeedTypesInfo, + Mode: packages.NeedName | + packages.NeedTypes | + packages.NeedTypesInfo | + packages.NeedModule, } + // you can expand this to multiple packages later pkgs, err := packages.Load(cfg, "fmt") if err != nil { panic(err) @@ -51,12 +56,16 @@ func main() { os.Exit(1) } - result := TopLevelSymbolScope{ - Structs: make(map[string]StructSymbol), - Functions: make(map[string]FunctionSymbol), - } + // 🔑 FINAL RESULT: map[import_path]scope + result := make(map[string]TopLevelSymbolScope) for _, pkg := range pkgs { + scopeData := TopLevelSymbolScope{ + PackageName: pkg.Name, // e.g. "http" + Structs: make(map[string]StructSymbol), + Functions: make(map[string]FunctionSymbol), + } + scope := pkg.Types.Scope() for _, name := range scope.Names() { @@ -69,15 +78,17 @@ func main() { switch obj := obj.(type) { case *types.Func: - fn := buildFunction(obj) - result.Functions[name] = fn + scopeData.Functions[name] = buildFunction(obj) case *types.TypeName: if strct, ok := buildStruct(obj); ok { - result.Structs[name] = strct + scopeData.Structs[name] = strct } } } + + // 🔑 key = import path (e.g. "net/http") + result[pkg.PkgPath] = scopeData } out, err := json.MarshalIndent(result, "", " ") @@ -114,7 +125,7 @@ func buildFunction(fn *types.Func) FunctionSymbol { } return FunctionSymbol{ - Export: token.IsExported(fn.Name()), + Export: true, // already filtered before calling Name: fn.Name(), Params: params, ReturnTypes: returnTypes, @@ -134,8 +145,12 @@ func buildStruct(tn *types.TypeName) (StructSymbol, bool) { for i := 0; i < strct.NumFields(); i++ { f := strct.Field(i) + if !token.IsExported(f.Name()) { + continue + } + fields[f.Name()] = VarSymbol{ - Export: token.IsExported(f.Name()), + Export: true, Name: f.Name(), VarType: TypeSymbol(f.Type().String()), } @@ -147,11 +162,15 @@ func buildStruct(tn *types.TypeName) (StructSymbol, bool) { for i := 0; i < methodSet.Len(); i++ { m := methodSet.At(i).Obj().(*types.Func) + if !token.IsExported(m.Name()) { + continue + } + methods[m.Name()] = buildFunction(m) } return StructSymbol{ - Export: token.IsExported(tn.Name()), + Export: true, Name: tn.Name(), Fields: fields, Methods: methods, diff --git a/test/mist.map.json b/test/mist.map.json new file mode 100644 index 0000000..cf63929 --- /dev/null +++ b/test/mist.map.json @@ -0,0 +1,450 @@ +{ + "fmt": { + "package_name": "fmt", + "structs": {}, + "functions": { + "Append": { + "export": true, + "name": "Append", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "b": { + "export": true, + "var_type": "[]byte", + "name": "b" + } + }, + "return_types": [ + "[]byte" + ] + }, + "Appendf": { + "export": true, + "name": "Appendf", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "b": { + "export": true, + "var_type": "[]byte", + "name": "b" + }, + "format": { + "export": true, + "var_type": "string", + "name": "format" + } + }, + "return_types": [ + "[]byte" + ] + }, + "Appendln": { + "export": true, + "name": "Appendln", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "b": { + "export": true, + "var_type": "[]byte", + "name": "b" + } + }, + "return_types": [ + "[]byte" + ] + }, + "Errorf": { + "export": true, + "name": "Errorf", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "format": { + "export": true, + "var_type": "string", + "name": "format" + } + }, + "return_types": [ + "error" + ] + }, + "FormatString": { + "export": true, + "name": "FormatString", + "params": { + "state": { + "export": true, + "var_type": "fmt.State", + "name": "state" + }, + "verb": { + "export": true, + "var_type": "rune", + "name": "verb" + } + }, + "return_types": [ + "string" + ] + }, + "Fprint": { + "export": true, + "name": "Fprint", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "w": { + "export": true, + "var_type": "io.Writer", + "name": "w" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Fprintf": { + "export": true, + "name": "Fprintf", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "format": { + "export": true, + "var_type": "string", + "name": "format" + }, + "w": { + "export": true, + "var_type": "io.Writer", + "name": "w" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Fprintln": { + "export": true, + "name": "Fprintln", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "w": { + "export": true, + "var_type": "io.Writer", + "name": "w" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Fscan": { + "export": true, + "name": "Fscan", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "r": { + "export": true, + "var_type": "io.Reader", + "name": "r" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Fscanf": { + "export": true, + "name": "Fscanf", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "format": { + "export": true, + "var_type": "string", + "name": "format" + }, + "r": { + "export": true, + "var_type": "io.Reader", + "name": "r" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Fscanln": { + "export": true, + "name": "Fscanln", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "r": { + "export": true, + "var_type": "io.Reader", + "name": "r" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Print": { + "export": true, + "name": "Print", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Printf": { + "export": true, + "name": "Printf", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "format": { + "export": true, + "var_type": "string", + "name": "format" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Println": { + "export": true, + "name": "Println", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Scan": { + "export": true, + "name": "Scan", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Scanf": { + "export": true, + "name": "Scanf", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "format": { + "export": true, + "var_type": "string", + "name": "format" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Scanln": { + "export": true, + "name": "Scanln", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Sprint": { + "export": true, + "name": "Sprint", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + } + }, + "return_types": [ + "string" + ] + }, + "Sprintf": { + "export": true, + "name": "Sprintf", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "format": { + "export": true, + "var_type": "string", + "name": "format" + } + }, + "return_types": [ + "string" + ] + }, + "Sprintln": { + "export": true, + "name": "Sprintln", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + } + }, + "return_types": [ + "string" + ] + }, + "Sscan": { + "export": true, + "name": "Sscan", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "str": { + "export": true, + "var_type": "string", + "name": "str" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Sscanf": { + "export": true, + "name": "Sscanf", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "format": { + "export": true, + "var_type": "string", + "name": "format" + }, + "str": { + "export": true, + "var_type": "string", + "name": "str" + } + }, + "return_types": [ + "int", + "error" + ] + }, + "Sscanln": { + "export": true, + "name": "Sscanln", + "params": { + "a": { + "export": true, + "var_type": "[]any", + "name": "a" + }, + "str": { + "export": true, + "var_type": "string", + "name": "str" + } + }, + "return_types": [ + "int", + "error" + ] + } + } + } +} \ No newline at end of file diff --git a/test/src/main.ms b/test/src/main.ms index 44279da..4346ab5 100644 --- a/test/src/main.ms +++ b/test/src/main.ms @@ -1,5 +1,7 @@ package main; +import "fmt"; + function main() { let a = 10; }