Сценарий: Класс: свойство типа класс или интерфейс - указатель
Этот коммит содержится в:
родитель
a62f3e06ad
коммит
94ea4afaf4
3 изменённых файлов: 63 добавлений и 0 удалений
|
@ -7,6 +7,7 @@ import (
|
|||
)
|
||||
|
||||
func handleExpr(expr ast.Expr) string {
|
||||
// spew.Dump(expr)
|
||||
code := ""
|
||||
switch e := expr.(type) {
|
||||
case *ast.BasicLit:
|
||||
|
|
|
@ -48,6 +48,37 @@ public:
|
|||
virtual void Add(int) = 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) {
|
||||
// spew.Dump(f)
|
||||
code += " "
|
||||
|
||||
switch t := f.Type.(type) {
|
||||
case *ast.Ident:
|
||||
code += handleIdentExpr(t)
|
||||
case *ast.StarExpr:
|
||||
code += handleStarExpr(t)
|
||||
case *ast.FuncType:
|
||||
code += handleFuncDeclType(t)
|
||||
case *ast.ArrayType:
|
||||
code += handleArray(t)
|
||||
}
|
||||
|
||||
if isClassByExpr(f.Type) {
|
||||
code += "*"
|
||||
}
|
||||
|
||||
code += " "
|
||||
nado_zapyatuyu := false
|
||||
for _, n := range f.Names {
|
||||
|
@ -87,3 +94,27 @@ func handleField(f *ast.Field) (code string) {
|
|||
code += ";\n"
|
||||
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 ""
|
||||
}
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче