From 58a613af208c150b96d8723c5ebd8c37e547b6e5 Mon Sep 17 00:00:00 2001 From: Softonik Date: Fri, 26 Jan 2024 03:10:29 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D1=86=D0=B5=D0=BD=D0=B0=D1=80=D0=B8?= =?UTF-8?q?=D0=B9:=20=D0=A1=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80?= =?UTF-8?q?=D0=B0=20=D1=81=20=D0=B2=D1=8B=D0=B7=D0=BE=D0=B2=D0=BE=D0=BC=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/service/class.go | 14 ++++++++++++++ pkg/service/expr.go | 8 ++++++-- pkg/service/features/app.feature | 32 +++++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/pkg/service/class.go b/pkg/service/class.go index c8d7a1a..6086eb4 100644 --- a/pkg/service/class.go +++ b/pkg/service/class.go @@ -62,13 +62,27 @@ func (c *Class) methodImplementationsToString() (code string) { code += "\n" return } + +var ( + isInMethod = false + currentReceiverName = "" +) + func (c *Class) methodImplementationToString(m *ast.FuncDecl) (code string) { code += "void " code += c.Name + "::" code += m.Name.String() + "(" code += ") {" + if len(m.Recv.List) > 0 { + n := m.Recv.List[0].Names + if len(n) > 0 { + isInMethod = true + currentReceiverName = n[0].Name + } + } code += handleBlockStmt(m.Body) + isInMethod = false code += "}" code += "\n" diff --git a/pkg/service/expr.go b/pkg/service/expr.go index 53c9442..0371eb5 100644 --- a/pkg/service/expr.go +++ b/pkg/service/expr.go @@ -87,9 +87,13 @@ func handleSelectorExpr(expr ast.Expr) string { code := "" switch x := s.X.(type) { case *ast.Ident: - code += handleIdentExpr(x) + if isInMethod && x.Name == currentReceiverName { + code += "this->" + } else { + code += handleIdentExpr(x) + code += "." + } } - code += "." code += handleIdentExpr(s.Sel) if val, ok := mapping[code]; ok { code = val diff --git a/pkg/service/features/app.feature b/pkg/service/features/app.feature index 6fd0dec..0ef34e7 100644 --- a/pkg/service/features/app.feature +++ b/pkg/service/features/app.feature @@ -79,7 +79,7 @@ public: std::string c,d,e; }; ``` -@f + Сценарий: Структура с методом * Исходник: ``` @@ -101,4 +101,34 @@ public: }; void device::doSomething() { } +``` + + Сценарий: Структура с вызовом метода + * Исходник: +``` +package test + +type device struct { + a int +} + +func (d *device) doSomething() { + d.doSomethingElse() +} +func (d *device) doSomethingElse() { +} +``` + * Результат: +``` +class device { +public: + int a; + void doSomething(); + void doSomethingElse(); +}; +void device::doSomething() { +this->doSomethingElse(); +} +void device::doSomethingElse() { +} ```