Сценарий: Структура с вызовом метода

Этот коммит содержится в:
Softonik 2024-01-26 03:10:29 +03:00 коммит произвёл Nobody
родитель 5e7dec08df
коммит 58a613af20
3 изменённых файлов: 51 добавлений и 3 удалений

Просмотреть файл

@ -62,13 +62,27 @@ func (c *Class) methodImplementationsToString() (code string) {
code += "\n" code += "\n"
return return
} }
var (
isInMethod = false
currentReceiverName = ""
)
func (c *Class) methodImplementationToString(m *ast.FuncDecl) (code string) { func (c *Class) methodImplementationToString(m *ast.FuncDecl) (code string) {
code += "void " code += "void "
code += c.Name + "::" code += c.Name + "::"
code += m.Name.String() + "(" code += m.Name.String() + "("
code += ") {" 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) code += handleBlockStmt(m.Body)
isInMethod = false
code += "}" code += "}"
code += "\n" code += "\n"

Просмотреть файл

@ -87,9 +87,13 @@ func handleSelectorExpr(expr ast.Expr) string {
code := "" code := ""
switch x := s.X.(type) { switch x := s.X.(type) {
case *ast.Ident: case *ast.Ident:
code += handleIdentExpr(x) if isInMethod && x.Name == currentReceiverName {
code += "this->"
} else {
code += handleIdentExpr(x)
code += "."
}
} }
code += "."
code += handleIdentExpr(s.Sel) code += handleIdentExpr(s.Sel)
if val, ok := mapping[code]; ok { if val, ok := mapping[code]; ok {
code = val code = val

Просмотреть файл

@ -79,7 +79,7 @@ public:
std::string c,d,e; std::string c,d,e;
}; };
``` ```
@f
Сценарий: Структура с методом Сценарий: Структура с методом
* Исходник: * Исходник:
``` ```
@ -101,4 +101,34 @@ public:
}; };
void device::doSomething() { 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() {
}
``` ```