From 0addf00c20a8725c193ab437683a5ed1ba3146db Mon Sep 17 00:00:00 2001 From: Softonik Date: Thu, 8 Feb 2024 23:28:59 +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=91=D0=BB=D0=BE=D0=BA=20=D0=BA=D0=BE=D0=BD=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/service/expr.go | 16 ++++++++++++++++ pkg/service/features/variables.feature | 22 ++++++++++++++++++++++ pkg/service/service_ginkgo_test.go | 4 ++-- pkg/service/spec.go | 19 +++++++++++++------ pkg/service/value.go | 23 ++++++++++++++++++++--- 5 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 pkg/service/features/variables.feature diff --git a/pkg/service/expr.go b/pkg/service/expr.go index a061aea..246cf21 100644 --- a/pkg/service/expr.go +++ b/pkg/service/expr.go @@ -2,6 +2,7 @@ package service import ( "go/ast" + "go/token" "strings" ) @@ -32,6 +33,21 @@ func handleBasicLit(bl *ast.BasicLit) string { return bl.Value } +func handleBasicLitType(bl *ast.BasicLit) string { + switch bl.Kind { + case token.INT: + return "int" + case token.CHAR: + return "char" + case token.FLOAT: + return "double" + case token.STRING: + return "std::string" + } + + return "" +} + func handleUnaryExpr(expr *ast.UnaryExpr) (code string) { cl, ok := expr.X.(*ast.CompositeLit) if ok { diff --git a/pkg/service/features/variables.feature b/pkg/service/features/variables.feature new file mode 100644 index 0000000..cc35fa4 --- /dev/null +++ b/pkg/service/features/variables.feature @@ -0,0 +1,22 @@ +# Во имя Бога Милостивого, Милосердного!!! +# language: ru +Функциональность: Преобразование в C++: переменные и константы + + Сценарий: Блок констант + * Исходник: +``` +package test +const ( + c1 = 4 + c2 = 5 + c3 = 5.5 + s1 = "privet" +) +``` + * Результат: +``` +const int c1 = 4; +const int c2 = 5; +const double c3 = 5.5; +const std::string s1 = "privet"; +``` diff --git a/pkg/service/service_ginkgo_test.go b/pkg/service/service_ginkgo_test.go index 94c52a6..1aae9dc 100644 --- a/pkg/service/service_ginkgo_test.go +++ b/pkg/service/service_ginkgo_test.go @@ -399,7 +399,7 @@ var _ = Describe("Go Translator", func() { void setup(); void loop(); - const maxX = 1; + const int maxX = 1; void setup() {} void loop() { if (x == maxX) { @@ -426,7 +426,7 @@ var _ = Describe("Go Translator", func() { void setup(); void loop(); - const maxX = 1; + const int maxX = 1; void setup() {} void loop() { if (x == maxX) { diff --git a/pkg/service/spec.go b/pkg/service/spec.go index 2057a79..99cb924 100644 --- a/pkg/service/spec.go +++ b/pkg/service/spec.go @@ -5,27 +5,34 @@ import ( "go/token" ) +var InConstBlock = false + func handleGenDecl(decl ast.Decl) string { gd := decl.(*ast.GenDecl) code := "" switch gd.Tok { case token.CONST: - code += "const " - case token.VAR: - code += "" + InConstBlock = true + code += handleSpecs(gd.Specs) + InConstBlock = false + default: + code += handleSpecs(gd.Specs) } - code += handleSpecs(gd.Specs) return code } func handleSpecs(specs []ast.Spec) string { code := "" for _, spec := range specs { - switch spec.(type) { + if InConstBlock { + code += "const" + } + + switch s := spec.(type) { case *ast.ImportSpec: code += handleImportSpec(spec) case *ast.ValueSpec: - code += handleValueSpec(spec) + ";" + code += handleValueSpec(s) + ";" case *ast.TypeSpec: code += handleTypeSpec(spec) } diff --git a/pkg/service/value.go b/pkg/service/value.go index da64ab8..5441994 100644 --- a/pkg/service/value.go +++ b/pkg/service/value.go @@ -4,9 +4,11 @@ import ( "go/ast" ) -func handleValueSpec(spec ast.Spec) string { - s := spec.(*ast.ValueSpec) - code := "" +func handleValueSpec(s *ast.ValueSpec) (code string) { + if s.Type == nil { + code += addTypeByValue(s) + } + code += handleValueSpecType(s.Type) code += " " @@ -24,6 +26,21 @@ func handleValueSpec(spec ast.Spec) string { return code } +func addTypeByValue(s *ast.ValueSpec) (code string) { + if len(s.Values) == 0 { + return + } + + value := s.Values[0] + + switch v := value.(type) { + case *ast.BasicLit: + code += " " + code += handleBasicLitType(v) + } + return +} + func handleValueSpecNames(names []*ast.Ident) (code string) { nado_zapyatuyu := false for _, name := range names {