diff --git a/pkg/service/const.go b/pkg/service/const.go new file mode 100644 index 0000000..7c90939 --- /dev/null +++ b/pkg/service/const.go @@ -0,0 +1,41 @@ +package service + +import "go/ast" + +var ( + constDeclarations []*Const +) + +type Const struct { + s *ast.ValueSpec +} + +func NewConst(s *ast.ValueSpec) *Const { + return &Const{s: s} +} + +func (c *Const) String() (code string) { + // return c.s.Names[0].Name + return c.generate() +} + +func (c *Const) generate() (code string) { + code += "const " + code += handleValueSpec(c.s) + code += ";" + return +} + +func handleConstSpecs(specs []ast.Spec) string { + for _, spec := range specs { + switch s := spec.(type) { + case *ast.ValueSpec: + addConstant(s) + } + } + return "" +} +func addConstant(s *ast.ValueSpec) { + c := NewConst(s) + constDeclarations = append(constDeclarations, c) +} diff --git a/pkg/service/features/app.feature b/pkg/service/features/app.feature index 82d1f53..75c6679 100644 --- a/pkg/service/features/app.feature +++ b/pkg/service/features/app.feature @@ -345,4 +345,19 @@ return new Device(); void main() { dev1=NewDevice(); } +``` + + Сценарий: Последовательность блоков: константы, типы, абстрактные классы, классы, переменные, функции + * Исходник: +``` +package test + +type Mera int + +const c1 = 4 +``` + * Результат: +``` +const int c1 = 4; +typedef int Mera; ``` diff --git a/pkg/service/service.go b/pkg/service/service.go index b32606d..c24ddb6 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -86,6 +86,7 @@ func (s *defaultService) Start() error { dst[i] = make(chan string, 1) } + constDeclarations = nil classDeclarations = nil funcDeclarations = nil helpersForDeclarations = nil @@ -104,6 +105,7 @@ func (s *defaultService) Start() error { } s.printIncludeHeaders() + s.printConstDeclarations() s.printClassDeclarations() s.printFunctionDeclarations() s.printGoHelperDeclarations() @@ -130,6 +132,19 @@ func (s *defaultService) printIncludeHeaders() { h := "#include \n\n" s.out.Write([]byte(h)) } +func (s *defaultService) printConstDeclarations() { + dlock.Lock() + defer dlock.Unlock() + + for _, c := range constDeclarations { + 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() { dlock.Lock() defer dlock.Unlock() diff --git a/pkg/service/service_ginkgo_test.go b/pkg/service/service_ginkgo_test.go index 1aae9dc..2319f9f 100644 --- a/pkg/service/service_ginkgo_test.go +++ b/pkg/service/service_ginkgo_test.go @@ -80,10 +80,11 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + const std::string foo = "bar"; + void foo(); void foo() { - const std::string foo = "bar"; } ` Compare(source, expected) @@ -396,10 +397,11 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + const int maxX = 1; + void setup(); void loop(); - const int maxX = 1; void setup() {} void loop() { if (x == maxX) { @@ -423,10 +425,11 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + const int maxX = 1; + void setup(); void loop(); - const int maxX = 1; void setup() {} void loop() { if (x == maxX) { diff --git a/pkg/service/spec.go b/pkg/service/spec.go index 3d20dc8..a008276 100644 --- a/pkg/service/spec.go +++ b/pkg/service/spec.go @@ -22,11 +22,14 @@ func handleGenDecl(decl ast.Decl) string { } func handleSpecs(specs []ast.Spec) (code string) { - for _, spec := range specs { - if InConstBlock { - code += "const " - } + if InConstBlock { + return handleConstSpecs(specs) + } + return handleSpecsVariables(specs) +} +func handleSpecsVariables(specs []ast.Spec) (code string) { + for _, spec := range specs { switch s := spec.(type) { case *ast.ImportSpec: code += handleImportSpec(spec)