Сценарий: Последовательность блоков: константы, типы, абстрактные классы
Этот коммит содержится в:
родитель
8e3a1a3d7a
коммит
7ffcf77114
5 изменённых файлов: 83 добавлений и 12 удалений
|
@ -5,6 +5,10 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
abstractClassDeclarations []Class
|
||||||
|
)
|
||||||
|
|
||||||
type abstractClass struct {
|
type abstractClass struct {
|
||||||
name string
|
name string
|
||||||
Interface *ast.InterfaceType
|
Interface *ast.InterfaceType
|
||||||
|
@ -82,6 +86,12 @@ func addAbstractClassFromStructType(name string, it *ast.InterfaceType) Class {
|
||||||
}
|
}
|
||||||
func createAbstractClass(className string) *abstractClass {
|
func createAbstractClass(className string) *abstractClass {
|
||||||
c := NewAbstractClass(className)
|
c := NewAbstractClass(className)
|
||||||
addClassDeclaration(c)
|
addAbstractClassDeclaration(c)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
func addAbstractClassDeclaration(c Class) {
|
||||||
|
dlock.Lock()
|
||||||
|
defer dlock.Unlock()
|
||||||
|
|
||||||
|
abstractClassDeclarations = append(abstractClassDeclarations, c)
|
||||||
|
}
|
||||||
|
|
|
@ -352,6 +352,10 @@ dev1=NewDevice();
|
||||||
```
|
```
|
||||||
package test
|
package test
|
||||||
|
|
||||||
|
type Device interface {
|
||||||
|
Name() string
|
||||||
|
}
|
||||||
|
|
||||||
type Mera int
|
type Mera int
|
||||||
|
|
||||||
const c1 = 4
|
const c1 = 4
|
||||||
|
@ -360,4 +364,8 @@ const c1 = 4
|
||||||
```
|
```
|
||||||
const int c1 = 4;
|
const int c1 = 4;
|
||||||
typedef int Mera;
|
typedef int Mera;
|
||||||
|
class Device {
|
||||||
|
public:
|
||||||
|
virtual std::string Name() = 0;
|
||||||
|
};
|
||||||
```
|
```
|
||||||
|
|
|
@ -87,6 +87,8 @@ func (s *defaultService) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
constDeclarations = nil
|
constDeclarations = nil
|
||||||
|
typeDeclarations = nil
|
||||||
|
abstractClassDeclarations = nil
|
||||||
classDeclarations = nil
|
classDeclarations = nil
|
||||||
funcDeclarations = nil
|
funcDeclarations = nil
|
||||||
helpersForDeclarations = nil
|
helpersForDeclarations = nil
|
||||||
|
@ -106,6 +108,8 @@ func (s *defaultService) Start() error {
|
||||||
|
|
||||||
s.printIncludeHeaders()
|
s.printIncludeHeaders()
|
||||||
s.printConstDeclarations()
|
s.printConstDeclarations()
|
||||||
|
s.printTypeDeclarations()
|
||||||
|
s.printAbstractClassDeclarations()
|
||||||
s.printClassDeclarations()
|
s.printClassDeclarations()
|
||||||
s.printFunctionDeclarations()
|
s.printFunctionDeclarations()
|
||||||
s.printGoHelperDeclarations()
|
s.printGoHelperDeclarations()
|
||||||
|
@ -145,6 +149,32 @@ func (s *defaultService) printConstDeclarations() {
|
||||||
}
|
}
|
||||||
s.out.Write([]byte("\n"))
|
s.out.Write([]byte("\n"))
|
||||||
}
|
}
|
||||||
|
func (s *defaultService) printTypeDeclarations() {
|
||||||
|
dlock.Lock()
|
||||||
|
defer dlock.Unlock()
|
||||||
|
|
||||||
|
for _, c := range typeDeclarations {
|
||||||
|
d := c.String()
|
||||||
|
s.out.Write([]byte(d + "\n"))
|
||||||
|
if s.header != nil {
|
||||||
|
s.header.Write([]byte(d + "\n"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.out.Write([]byte("\n"))
|
||||||
|
}
|
||||||
|
func (s *defaultService) printAbstractClassDeclarations() {
|
||||||
|
dlock.Lock()
|
||||||
|
defer dlock.Unlock()
|
||||||
|
|
||||||
|
for _, c := range abstractClassDeclarations {
|
||||||
|
d := c.String()
|
||||||
|
s.out.Write([]byte(d + "\n"))
|
||||||
|
if s.header != nil {
|
||||||
|
s.header.Write([]byte(d + "\n"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.out.Write([]byte("\n"))
|
||||||
|
}
|
||||||
func (s *defaultService) printClassDeclarations() {
|
func (s *defaultService) printClassDeclarations() {
|
||||||
dlock.Lock()
|
dlock.Lock()
|
||||||
defer dlock.Unlock()
|
defer dlock.Unlock()
|
||||||
|
|
|
@ -19,17 +19,6 @@ func handleTypeSpec(s *ast.TypeSpec) (code string) {
|
||||||
return handleType(s)
|
return handleType(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
func optionallyAddArrayDetails(s *ast.TypeSpec) (code string) {
|
||||||
v, ok := s.Type.(*ast.ArrayType)
|
v, ok := s.Type.(*ast.ArrayType)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
34
pkg/service/types.go
Обычный файл
34
pkg/service/types.go
Обычный файл
|
@ -0,0 +1,34 @@
|
||||||
|
package service
|
||||||
|
|
||||||
|
import "go/ast"
|
||||||
|
|
||||||
|
var (
|
||||||
|
typeDeclarations []*Type
|
||||||
|
)
|
||||||
|
|
||||||
|
type Type struct {
|
||||||
|
s *ast.TypeSpec
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewType(s *ast.TypeSpec) *Type {
|
||||||
|
return &Type{s: s}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Type) String() (code string) {
|
||||||
|
code += "typedef "
|
||||||
|
code += handleExpr(c.s.Type)
|
||||||
|
code += " "
|
||||||
|
code += handleIdentExpr(c.s.Name)
|
||||||
|
code += optionallyAddArrayDetails(c.s)
|
||||||
|
code += ";"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleType(s *ast.TypeSpec) (code string) {
|
||||||
|
addType(s)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
func addType(s *ast.TypeSpec) {
|
||||||
|
c := NewType(s)
|
||||||
|
typeDeclarations = append(typeDeclarations, c)
|
||||||
|
}
|
Загрузка…
Создание таблицы
Сослаться в новой задаче