From 8e3a1a3d7ac445d91d20b663b661922b0931e2c0 Mon Sep 17 00:00:00 2001 From: Softonik Date: Mon, 12 Feb 2024 02:06:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D1=86=D0=B5=D0=BD=D0=B0=D1=80=D0=B8?= =?UTF-8?q?=D0=B9:=20=D0=9F=D0=BE=D1=81=D0=BB=D0=B5=D0=B4=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D0=BB=D1=8C=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20?= =?UTF-8?q?=D0=B1=D0=BB=D0=BE=D0=BA=D0=BE=D0=B2:=20=D0=BA=D0=BE=D0=BD?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BD=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/service/const.go | 41 ++++++++++++++++++++++++++++++ pkg/service/features/app.feature | 15 +++++++++++ pkg/service/service.go | 15 +++++++++++ pkg/service/service_ginkgo_test.go | 9 ++++--- pkg/service/spec.go | 11 +++++--- 5 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 pkg/service/const.go 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)