Сценарий: Структура с объявлением переменных-указателей

Этот коммит содержится в:
Softonik 2024-01-26 03:57:38 +03:00 коммит произвёл Nobody
родитель 56fe611154
коммит 11825a08c4
5 изменённых файлов: 52 добавлений и 8 удалений

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

@ -6,6 +6,9 @@ import (
var (
classDeclarations []*Class
isInMethod = false
currentReceiverName = ""
)
type Class struct {
@ -63,11 +66,6 @@ func (c *Class) methodImplementationsToString() (code string) {
return
}
var (
isInMethod = false
currentReceiverName = ""
)
func (c *Class) methodImplementationToString(m *ast.FuncDecl) (code string) {
code += "void "
code += c.Name + "::"

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

@ -105,3 +105,11 @@ func handleSelectorExpr(expr ast.Expr) string {
}
return code
}
func handleStarExpr(s *ast.StarExpr) (code string) {
switch x := s.X.(type) {
case *ast.Ident:
code += handleIdentExpr(x)
}
return
}

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

@ -196,3 +196,21 @@ this->x=dev2->x;
}
```
Сценарий: Структура с объявлением переменных-указателей
* Исходник:
```
package test
var (
dev1, dev2 *device
)
type device struct {}
```
* Результат:
```
class device {
public:
};
device *dev1,*dev2;
```

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

@ -4,6 +4,10 @@ import (
"go/ast"
)
var (
isPointerType = false
)
func handleTypeSpec(spec ast.Spec) (code string) {
s := spec.(*ast.TypeSpec)
handleClass(s)

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

@ -9,7 +9,14 @@ func handleValueSpec(spec ast.Spec) string {
code := ""
code += handleValueSpecType(s.Type)
code += " "
_, ok := s.Type.(*ast.StarExpr)
if ok {
isPointerType = true
}
code += handleValueSpecNames(s.Names)
isPointerType = false
if s.Values != nil {
code += " = "
code += handleValueSpecValues(s.Values)
@ -17,12 +24,19 @@ func handleValueSpec(spec ast.Spec) string {
return code
}
func handleValueSpecNames(names []*ast.Ident) string {
code := ""
func handleValueSpecNames(names []*ast.Ident) (code string) {
nado_zapyatuyu := false
for _, name := range names {
if nado_zapyatuyu {
code += ","
}
if isPointerType {
code += "*"
}
code += handleIdentExpr(name)
nado_zapyatuyu = true
}
return code
return
}
func handleValueSpecType(expr ast.Expr) string {
@ -32,6 +46,8 @@ func handleValueSpecType(expr ast.Expr) string {
code += handleSelectorExpr(t)
case *ast.Ident:
code += handleIdentExpr(t)
case *ast.StarExpr:
code += handleStarExpr(t)
}
return code
}