Сценарий: Блок констант

Этот коммит содержится в:
Softonik 2024-02-08 23:28:59 +03:00 коммит произвёл Nobody
родитель 6c3785004c
коммит 0addf00c20
5 изменённых файлов: 73 добавлений и 11 удалений

Просмотреть файл

@ -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 {

22
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";
```

Просмотреть файл

@ -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) {

Просмотреть файл

@ -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)
}

Просмотреть файл

@ -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 {