Сценарий: Кастомные типы
Этот коммит содержится в:
родитель
f7ed856d0c
коммит
b1694abd29
3 изменённых файлов: 42 добавлений и 3 удалений
|
@ -25,6 +25,8 @@ func handleExpr(expr ast.Expr) string {
|
||||||
code += handleSelectorExpr(e)
|
code += handleSelectorExpr(e)
|
||||||
case *ast.StructType:
|
case *ast.StructType:
|
||||||
code += handleStructType(e)
|
code += handleStructType(e)
|
||||||
|
case *ast.ArrayType:
|
||||||
|
code += handleArray(e)
|
||||||
}
|
}
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,4 +57,18 @@ int a[];
|
||||||
bool b[];
|
bool b[];
|
||||||
int c[8];
|
int c[8];
|
||||||
int d[LENGTH];
|
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)
|
return handleType(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleType(s *ast.TypeSpec) string {
|
func handleType(s *ast.TypeSpec) (code string) {
|
||||||
return ""
|
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) {
|
func handleStructType(s *ast.StructType) (code string) {
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче