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

Этот коммит содержится в:
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"
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"

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

@ -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

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

@ -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() {
}
```