Сценарий: Блок констант
Этот коммит содержится в:
родитель
6c3785004c
коммит
0addf00c20
5 изменённых файлов: 73 добавлений и 11 удалений
|
@ -2,6 +2,7 @@ package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
|
"go/token"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -32,6 +33,21 @@ func handleBasicLit(bl *ast.BasicLit) string {
|
||||||
return bl.Value
|
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) {
|
func handleUnaryExpr(expr *ast.UnaryExpr) (code string) {
|
||||||
cl, ok := expr.X.(*ast.CompositeLit)
|
cl, ok := expr.X.(*ast.CompositeLit)
|
||||||
if ok {
|
if ok {
|
||||||
|
|
22
pkg/service/features/variables.feature
Обычный файл
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 setup();
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
const maxX = 1;
|
const int maxX = 1;
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
if (x == maxX) {
|
if (x == maxX) {
|
||||||
|
@ -426,7 +426,7 @@ var _ = Describe("Go Translator", func() {
|
||||||
void setup();
|
void setup();
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
const maxX = 1;
|
const int maxX = 1;
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
if (x == maxX) {
|
if (x == maxX) {
|
||||||
|
|
|
@ -5,27 +5,34 @@ import (
|
||||||
"go/token"
|
"go/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var InConstBlock = false
|
||||||
|
|
||||||
func handleGenDecl(decl ast.Decl) string {
|
func handleGenDecl(decl ast.Decl) string {
|
||||||
gd := decl.(*ast.GenDecl)
|
gd := decl.(*ast.GenDecl)
|
||||||
code := ""
|
code := ""
|
||||||
switch gd.Tok {
|
switch gd.Tok {
|
||||||
case token.CONST:
|
case token.CONST:
|
||||||
code += "const "
|
InConstBlock = true
|
||||||
case token.VAR:
|
|
||||||
code += ""
|
|
||||||
}
|
|
||||||
code += handleSpecs(gd.Specs)
|
code += handleSpecs(gd.Specs)
|
||||||
|
InConstBlock = false
|
||||||
|
default:
|
||||||
|
code += handleSpecs(gd.Specs)
|
||||||
|
}
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSpecs(specs []ast.Spec) string {
|
func handleSpecs(specs []ast.Spec) string {
|
||||||
code := ""
|
code := ""
|
||||||
for _, spec := range specs {
|
for _, spec := range specs {
|
||||||
switch spec.(type) {
|
if InConstBlock {
|
||||||
|
code += "const"
|
||||||
|
}
|
||||||
|
|
||||||
|
switch s := spec.(type) {
|
||||||
case *ast.ImportSpec:
|
case *ast.ImportSpec:
|
||||||
code += handleImportSpec(spec)
|
code += handleImportSpec(spec)
|
||||||
case *ast.ValueSpec:
|
case *ast.ValueSpec:
|
||||||
code += handleValueSpec(spec) + ";"
|
code += handleValueSpec(s) + ";"
|
||||||
case *ast.TypeSpec:
|
case *ast.TypeSpec:
|
||||||
code += handleTypeSpec(spec)
|
code += handleTypeSpec(spec)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,11 @@ import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleValueSpec(spec ast.Spec) string {
|
func handleValueSpec(s *ast.ValueSpec) (code string) {
|
||||||
s := spec.(*ast.ValueSpec)
|
if s.Type == nil {
|
||||||
code := ""
|
code += addTypeByValue(s)
|
||||||
|
}
|
||||||
|
|
||||||
code += handleValueSpecType(s.Type)
|
code += handleValueSpecType(s.Type)
|
||||||
code += " "
|
code += " "
|
||||||
|
|
||||||
|
@ -24,6 +26,21 @@ func handleValueSpec(spec ast.Spec) string {
|
||||||
return code
|
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) {
|
func handleValueSpecNames(names []*ast.Ident) (code string) {
|
||||||
nado_zapyatuyu := false
|
nado_zapyatuyu := false
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче