Сценарий: Класс: свойство типа класс или интерфейс - указатель

Этот коммит содержится в:
Softonik 2024-02-12 06:30:07 +03:00 коммит произвёл Nobody
родитель a62f3e06ad
коммит 94ea4afaf4
3 изменённых файлов: 63 добавлений и 0 удалений

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

@ -7,6 +7,7 @@ import (
) )
func handleExpr(expr ast.Expr) string { func handleExpr(expr ast.Expr) string {
// spew.Dump(expr)
code := "" code := ""
switch e := expr.(type) { switch e := expr.(type) {
case *ast.BasicLit: case *ast.BasicLit:

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

@ -48,6 +48,37 @@ public:
virtual void Add(int) = 0; virtual void Add(int) = 0;
virtual void Add2(int,double) = 0; virtual void Add2(int,double) = 0;
}; };
```
Сценарий: Класс: свойство типа интерфейс - указатель
* Исходник:
```
package test
type Device interface {
}
type DeviceClass struct {
}
type system struct {
d Device
dc DeviceClass
dcp *DeviceClass
}
```
* Результат:
```
class Device {
public:
};
class DeviceClass {
public:
};
class system {
public:
Device* d;
DeviceClass* dc;
DeviceClass* dcp;
};
``` ```
Сценарий: Структура с полем Сценарий: Структура с полем

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

@ -58,17 +58,24 @@ func handleFieldList(l *ast.FieldList) string {
} }
func handleField(f *ast.Field) (code string) { func handleField(f *ast.Field) (code string) {
// spew.Dump(f)
code += " " code += " "
switch t := f.Type.(type) { switch t := f.Type.(type) {
case *ast.Ident: case *ast.Ident:
code += handleIdentExpr(t) code += handleIdentExpr(t)
case *ast.StarExpr:
code += handleStarExpr(t)
case *ast.FuncType: case *ast.FuncType:
code += handleFuncDeclType(t) code += handleFuncDeclType(t)
case *ast.ArrayType: case *ast.ArrayType:
code += handleArray(t) code += handleArray(t)
} }
if isClassByExpr(f.Type) {
code += "*"
}
code += " " code += " "
nado_zapyatuyu := false nado_zapyatuyu := false
for _, n := range f.Names { for _, n := range f.Names {
@ -87,3 +94,27 @@ func handleField(f *ast.Field) (code string) {
code += ";\n" code += ";\n"
return return
} }
func isClassByExpr(e ast.Expr) (found bool) {
name := getExprTypeName(e)
for _, c := range classDeclarations {
if c.Name() == name {
found = true
break
}
}
return
}
func getExprTypeName(te ast.Expr) string {
switch t := te.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
ident, ok := t.X.(*ast.Ident)
if ok {
return ident.Name
}
}
return ""
}