Сценарий: Кастомные типы

Этот коммит содержится в:
Softonik 2024-02-11 21:47:06 +03:00 коммит произвёл Nobody
родитель f7ed856d0c
коммит b1694abd29
3 изменённых файлов: 42 добавлений и 3 удалений

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

@ -25,6 +25,8 @@ func handleExpr(expr ast.Expr) string {
code += handleSelectorExpr(e)
case *ast.StructType:
code += handleStructType(e)
case *ast.ArrayType:
code += handleArray(e)
}
return code
}

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

@ -57,4 +57,18 @@ int a[];
bool b[];
int c[8];
int d[LENGTH];
```
```
Сценарий: Кастомные типы
* Исходник:
```
package test
type Mera int
type GPIOS [GPIO_count]bool
```
* Результат:
```
typedef int Mera;
typedef bool GPIOS[GPIO_count];
```

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

@ -17,8 +17,31 @@ func handleTypeSpec(s *ast.TypeSpec) (code string) {
return handleType(s)
}
func handleType(s *ast.TypeSpec) string {
return ""
func handleType(s *ast.TypeSpec) (code string) {
code += "typedef "
code += handleExpr(s.Type)
code += " "
code += handleIdentExpr(s.Name)
code += optionallyAddArrayDetails(s)
code += ";"
return
}
func optionallyAddArrayDetails(s *ast.TypeSpec) (code string) {
v, ok := s.Type.(*ast.ArrayType)
if !ok {
return
}
code += "["
code += handleArrayLen(v)
code += "]"
return
}
func handleArrayLen(a *ast.ArrayType) (code string) {
return handleExpr(a.Len)
}
func handleStructType(s *ast.StructType) (code string) {