Skip to content
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

llcppsymg: go symbol name for go struct methodname #7

Merged
merged 1 commit into from
Dec 5, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.dll
*.so
*.dylib
*.DS_Store

# Test binary, built with `go test -c`
*.test
Expand Down
14 changes: 14 additions & 0 deletions _xtool/llcppsymg/_cmptest/names_test/llgo.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#stdout
=== Test ToGoName ===
Before: lua_closethread After: Closethread
Before: luaL_checknumber After: Checknumber
Before: sqlite3_close_v2 After: CloseV2
Before: sqlite3_callback After: Callback
Before: GetReal After: GetReal
Before: GetBoolean After: GetBoolean
Before: INIReader After: Reader


#stderr

#exit 0
38 changes: 38 additions & 0 deletions _xtool/llcppsymg/_cmptest/names_test/names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"

"github.com/goplus/llcppg/_xtool/llcppsymg/names"
"github.com/goplus/llcppg/_xtool/llcppsymg/parse"
)

func main() {
TestToGoName()
}

func TestToGoName() {
fmt.Println("=== Test ToGoName ===")
process1 := parse.NewSymbolProcessor([]string{}, []string{"lua_", "luaL_"})
process2 := parse.NewSymbolProcessor([]string{}, []string{"sqlite3_", "sqlite3_"})
process3 := parse.NewSymbolProcessor([]string{}, []string{"INI"})

testCases := []struct {
processor *parse.SymbolProcessor
input string
}{
{process1, "lua_closethread"},
{process1, "luaL_checknumber"},
{process2, "sqlite3_close_v2"},
{process2, "sqlite3_callback"},
{process3, "GetReal"},
{process3, "GetBoolean"},
{process3, "INIReader"},
}

for _, tc := range testCases {
result := names.GoName(tc.input, tc.processor.Prefixes, true)
fmt.Printf("Before: %s After: %s\n", tc.input, result)
}
fmt.Println()
}
18 changes: 3 additions & 15 deletions _xtool/llcppsymg/_cmptest/parse_test/llgo.expect
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@
=== Test NewSymbolProcessor ===
Before: No prefixes After: Prefixes: [lua_ luaL_]

=== Test RemovePrefix ===
Before: lua_closethread After: closethread
Before: luaL_checknumber After: checknumber

=== Test ToGoName ===
Before: lua_closethread After: Closethread
Before: luaL_checknumber After: Checknumber
Before: sqlite3_close_v2 After: CloseV2
Before: sqlite3_callback After: Callback
Before: GetReal After: GetReal
Before: GetBoolean After: GetBoolean
Before: INIReader After: Reader

=== Test GenMethodName ===
Before: Class: INIReader, Name: INIReader After: (*INIReader).Init
Before: Class: INIReader, Name: INIReader After: (*INIReader).Dispose
Expand All @@ -35,8 +22,9 @@ Symbol Map GoName: (*Reader).ParseError, ProtoName In HeaderFile: INIReader::Par

=== Test Case: C Functions ===
Parsed Symbols:
Symbol Map GoName: Compare, ProtoName In HeaderFile: lua_compare(lua_State *, int, int, int), MangledName: lua_compare
Symbol Map GoName: Rawequal, ProtoName In HeaderFile: lua_rawequal(lua_State *, int, int), MangledName: lua_rawequal
Symbol Map GoName: (*State).Compare, ProtoName In HeaderFile: lua_compare(lua_State *, int, int, int), MangledName: lua_compare
Symbol Map GoName: (*State).Rawequal, ProtoName In HeaderFile: lua_rawequal(lua_State *, int, int), MangledName: lua_rawequal
Symbol Map GoName: Sizecomp, ProtoName In HeaderFile: lua_sizecomp(int, int, int, int), MangledName: lua_sizecomp


#stderr
Expand Down
55 changes: 8 additions & 47 deletions _xtool/llcppsymg/_cmptest/parse_test/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,24 @@ import (
"fmt"
"sort"

"github.com/goplus/llcppg/_xtool/llcppsymg/names"
"github.com/goplus/llcppg/_xtool/llcppsymg/parse"
)

func main() {
TestNewSymbolProcessor()
TestRemovePrefix()
TestToGoName()
TestGenMethodName()
TestAddSuffix()
TestParseHeaderFile()
}

func TestNewSymbolProcessor() {
fmt.Println("=== Test NewSymbolProcessor ===")
process := parse.NewSymbolProcessor([]string{"lua_", "luaL_"})
process := parse.NewSymbolProcessor([]string{}, []string{"lua_", "luaL_"})
fmt.Printf("Before: No prefixes After: Prefixes: %v\n", process.Prefixes)
fmt.Println()
}

func TestRemovePrefix() {
fmt.Println("=== Test RemovePrefix ===")
process := parse.NewSymbolProcessor([]string{"lua_", "luaL_"})

testCases := []string{"lua_closethread", "luaL_checknumber"}

for _, input := range testCases {
result := process.TrimPrefixes(input)
fmt.Printf("Before: %s After: %s\n", input, result)
}
fmt.Println()
}

func TestToGoName() {
fmt.Println("=== Test ToGoName ===")
process1 := parse.NewSymbolProcessor([]string{"lua_", "luaL_"})
process2 := parse.NewSymbolProcessor([]string{"sqlite3_", "sqlite3_"})
process3 := parse.NewSymbolProcessor([]string{"INI"})

testCases := []struct {
processor *parse.SymbolProcessor
input string
}{
{process1, "lua_closethread"},
{process1, "luaL_checknumber"},
{process2, "sqlite3_close_v2"},
{process2, "sqlite3_callback"},
{process3, "GetReal"},
{process3, "GetBoolean"},
{process3, "INIReader"},
}

for _, tc := range testCases {
result := tc.processor.ToGoName(tc.input)
fmt.Printf("Before: %s After: %s\n", tc.input, result)
}
fmt.Println()
}

func TestGenMethodName() {
fmt.Println("=== Test GenMethodName ===")
process := &parse.SymbolProcessor{}
Expand All @@ -77,25 +37,25 @@ func TestGenMethodName() {
}
for _, tc := range testCases {
input := fmt.Sprintf("Class: %s, Name: %s", tc.class, tc.name)
result := process.GenMethodName(tc.class, tc.name, tc.isDestructor)
result := process.GenMethodName(tc.class, tc.name, tc.isDestructor, true)
fmt.Printf("Before: %s After: %s\n", input, result)
}
fmt.Println()
}

func TestAddSuffix() {
fmt.Println("=== Test AddSuffix ===")
process := parse.NewSymbolProcessor([]string{"INI"})
process := parse.NewSymbolProcessor([]string{}, []string{"INI"})
methods := []string{
"INIReader",
"INIReader",
"ParseError",
"HasValue",
}
for _, method := range methods {
goName := process.ToGoName(method)
className := process.ToGoName("INIReader")
methodName := process.GenMethodName(className, goName, false)
goName := names.GoName(method, process.Prefixes, true)
className := names.GoName("INIReader", process.Prefixes, true)
methodName := process.GenMethodName(className, goName, false, true)
finalName := process.AddSuffix(methodName)
input := fmt.Sprintf("Class: INIReader, Method: %s", method)
fmt.Printf("Before: %s After: %s\n", input, finalName)
Expand Down Expand Up @@ -132,6 +92,7 @@ class INIReader {
typedef struct lua_State lua_State;
int(lua_rawequal)(lua_State *L, int idx1, int idx2);
int(lua_compare)(lua_State *L, int idx1, int idx2, int op);
int(lua_sizecomp)(size_t s, int idx1, int idx2, int op);
`,
isCpp: false,
prefixes: []string{"lua_"},
Expand Down
8 changes: 4 additions & 4 deletions _xtool/llcppsymg/_cmptest/symg_test/llgo.expect
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@
[{
"mangle": "lua_error",
"c++": "lua_error(lua_State *)",
"go": "Error"
"go": "(*State).Error"
}, {
"mangle": "lua_next",
"c++": "lua_next(lua_State *, int)",
"go": "Next"
"go": "(*State).Next"
}, {
"mangle": "lua_concat",
"c++": "lua_concat(lua_State *, int)",
"go": "Concat"
"go": "(*State).Concat"
}, {
"mangle": "lua_stringtonumber",
"c++": "lua_stringtonumber(lua_State *, const char *)",
"go": "Stringtonumber"
"go": "(*State).Stringtonumber"
}]

#stderr
Expand Down
48 changes: 48 additions & 0 deletions _xtool/llcppsymg/names/names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package names

import (
"strings"
)

func GoName(name string, trimPrefixes []string, inCurPkg bool) string {
if inCurPkg {
name = removePrefixedName(name, trimPrefixes)
}
return pubName(name)
}

func removePrefixedName(name string, trimPrefixes []string) string {
if len(trimPrefixes) == 0 {
return name
}
for _, prefix := range trimPrefixes {
if strings.HasPrefix(name, prefix) {
return strings.TrimPrefix(name, prefix)
}
}
return name
}

func pubName(name string) string {
if len(name) == 0 {
return name
}
toCamelCase := func(s string) string {
parts := strings.Split(s, "_")
for i := 0; i < len(parts); i++ {
if len(parts[i]) > 0 {
parts[i] = strings.ToUpper(parts[i][:1]) + parts[i][1:]
}
}
return strings.Join(parts, "")
}
if name[0] == '_' {
i := 0
for i < len(name) && name[i] == '_' {
i++
}
prefix := name[:i]
return "X" + prefix + toCamelCase(name[i:])
}
return toCamelCase(name)
}
Loading
Loading