diff --git a/pkg/service/expr.go b/pkg/service/expr.go index 465f1dc..feba1a5 100644 --- a/pkg/service/expr.go +++ b/pkg/service/expr.go @@ -115,6 +115,9 @@ func handleParenExpr(stmt *ast.ParenExpr) string { } func handleSelectorExpr(s *ast.SelectorExpr) (code string) { + // println() + // spew.Dump(s) + switch x := s.X.(type) { case *ast.Ident: if isInMethod { @@ -126,7 +129,7 @@ func handleSelectorExpr(s *ast.SelectorExpr) (code string) { code += "->" } else { code += handleIdentExpr(x) - code += "." + code += "->" } case *ast.SelectorExpr: code += "this" @@ -135,9 +138,7 @@ func handleSelectorExpr(s *ast.SelectorExpr) (code string) { code += "->" } code += handleIdentExpr(s.Sel) - if val, ok := mapping[code]; ok { - code = val - } + code = RemapCode(code) return } diff --git a/pkg/service/features/selecting.feature b/pkg/service/features/selecting.feature new file mode 100644 index 0000000..5a134a5 --- /dev/null +++ b/pkg/service/features/selecting.feature @@ -0,0 +1,116 @@ +# Во имя Бога Милостивого, Милосердного!!! +# language: ru +Функциональность: Обращения к полям + + Сценарий: Вызов метода + * Исходник: +``` +package test +func foo() { + foo.Bar(1,"2") +} +``` + * Результат: +``` +void foo(); + +void foo() { +foo->Bar(1,"2"); +} +``` + + Сценарий: Вызов метода из пакета + * Исходник: +``` +package test +func foo() { + x = bar() + y = pkg.Bar() + z = x + y +} +``` + * Результат: +``` +void foo(); + +void foo() { +x=bar(); +y=pkg->Bar(); +z=x+y; +} +``` + + Сценарий: Вызов метода из пакета ещё + * Исходник: +``` +package test +func foo() { + foo.Bar(1,"2",digital.Low) +} +``` + * Результат: +``` +void foo(); + +void foo() { +foo->Bar(1,"2",LOW); +} +``` + + Сценарий: Обращение к полям динамически созданного класса + * Исходник: +``` +package test + +type Device struct { + v int +} + +func NewDevice() *Device { + d := &Device{} + d.v = 1 + return d +} +``` + * Результат: +``` +class Device { +public: int v; +}; +Device* NewDevice(); + +Device* NewDevice() { +auto d=new Device(); +d->v=1; +return d; +} +``` + + Сценарий: + * Исходник: +``` +package test + +type Device struct { + v int +} + +func NewDevice() *Device { + d := &Device{} + d.v = 1 + return d +} +``` + * Результат: +``` +class Device { +public: int v; +}; +Device* NewDevice(); + +Device* NewDevice() { +auto d=new Device(); +d->v=1; +return d; +} +``` diff --git a/pkg/service/func.go b/pkg/service/func.go index af97195..b1f13f3 100644 --- a/pkg/service/func.go +++ b/pkg/service/func.go @@ -117,9 +117,7 @@ func handleFuncDeclName(ident *ast.Ident) (code string) { return } code += ident.Name - if val, ok := mapping[code]; ok { - code = val - } + code = RemapCode(code) return } diff --git a/pkg/service/includes.go b/pkg/service/includes.go index 3cc7284..74b092c 100644 --- a/pkg/service/includes.go +++ b/pkg/service/includes.go @@ -24,9 +24,7 @@ func (i *Include) String() (code string) { func (i *Include) genString(s *ast.ImportSpec) (code string) { if s.Name != nil { name := handleIdentExpr(s.Name) - if val, ok := mapping[name]; ok { - name = val - } + name = RemapCode(name) if name != "" { if name != "controller" { code = "#include <" + name + ".h>\n" diff --git a/pkg/service/mapping.go b/pkg/service/mapping.go index e63726d..badc1f7 100644 --- a/pkg/service/mapping.go +++ b/pkg/service/mapping.go @@ -1,8 +1,9 @@ package service +import "strings" + var mapping = map[string]string{ "fmt.Sprint": "std::to_string", - "fmt->Sprint": "std::to_string", "digital.Low": "LOW", "digital.High": "HIGH", "digital.ModeInput": "INPUT", @@ -111,3 +112,12 @@ var mapping = map[string]string{ "semaphore.SemaphoreGive": "xSemaphoreGive", "log.Log_e": "log_e", } + +func RemapCode(code string) string { + check := strings.ReplaceAll(code, "->", ".") + + if val, ok := mapping[check]; ok { + return val + } + return code +} diff --git a/pkg/service/service_ginkgo_test.go b/pkg/service/service_ginkgo_test.go index 64ff353..752e276 100644 --- a/pkg/service/service_ginkgo_test.go +++ b/pkg/service/service_ginkgo_test.go @@ -138,22 +138,6 @@ var _ = Describe("Go Translator", func() { Compare(source, expected) }) - It("Function_With_Package_Function_Call", func() { - source := `package test - func foo() { - foo.Bar(1,"2") - } - ` - expected := ` - void foo(); - - void foo() { - foo.Bar(1,"2"); - } - ` - Compare(source, expected) - }) - It("Function_With_Assignments", func() { source := `package test func foo() { @@ -174,26 +158,6 @@ var _ = Describe("Go Translator", func() { Compare(source, expected) }) - It("Function_With_Package_Selector_Assignments", func() { - source := `package test - func foo() { - x = bar() - y = pkg.Bar() - z = x + y - } - ` - expected := ` - void foo(); - - void foo() { - x = bar(); - y = pkg.Bar(); - z = x + y; - } - ` - Compare(source, expected) - }) - It("Function_Ident_Mapping", func() { source := `package test func foo() { @@ -210,22 +174,6 @@ var _ = Describe("Go Translator", func() { Compare(source, expected) }) - It("Function_With_Ident_Param", func() { - source := `package test - func foo() { - foo.Bar(1,"2",digital.Low) - } - ` - expected := ` - void foo(); - - void foo() { - foo.Bar(1,"2",LOW); - } - ` - Compare(source, expected) - }) - It("Function_With_Function_Param", func() { source := `package test func foo() { diff --git a/pkg/service/stmt.go b/pkg/service/stmt.go index 0701221..e37a31e 100644 --- a/pkg/service/stmt.go +++ b/pkg/service/stmt.go @@ -23,6 +23,7 @@ func handleBlockStmt(body *ast.BlockStmt) string { func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string { // println() // spew.Dump(stmt) + code := "" switch s := stmt.(type) { case *ast.AssignStmt: @@ -96,14 +97,13 @@ func isExpr0String(ee []ast.Expr) bool { return false } -func handleAssignStmtExpr(e []ast.Expr) string { +func handleAssignStmtExpr(e []ast.Expr) (code string) { ops := make([]string, 0) - code := "" for _, op := range e { ops = append(ops, handleExpr(op)) } code += strings.Join(ops, ",") - return code + return } func handleToken(t token.Token) (code string, new bool) {