From 9fb01827843228fac508356c1d9c33dfbf3e0bae Mon Sep 17 00:00:00 2001 From: Softonik Date: Mon, 12 Feb 2024 19:42:31 +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=98=D0=BD=D0=B8=D1=86=D0=B8=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D0=BE=D0=B9=20-=20=D1=82=D0=B8=D0=BF=20strin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/service/features/variables.feature | 16 +++++++++++++ pkg/service/stmt.go | 33 ++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/pkg/service/features/variables.feature b/pkg/service/features/variables.feature index f115083..743dca3 100644 --- a/pkg/service/features/variables.feature +++ b/pkg/service/features/variables.feature @@ -103,4 +103,20 @@ void foo(); void foo() { auto a=NewDevice(1,b,"stroka"); } +``` + + Сценарий: Инициализация переменной - тип string + * Исходник: +``` +package test +func foo() { + a := "stroka" +} +``` + * Результат: +``` +void foo(); +void foo() { +std::string a="stroka"; +} ``` diff --git a/pkg/service/stmt.go b/pkg/service/stmt.go index e8f9ea7..0701221 100644 --- a/pkg/service/stmt.go +++ b/pkg/service/stmt.go @@ -61,18 +61,41 @@ func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string { return code } -func handleAssignStmt(as *ast.AssignStmt) (code string) { - tkn, new := handleToken(as.Tok) +func handleAssignStmt(a *ast.AssignStmt) (code string) { + // println() + // spew.Dump(a) + + tkn, new := handleToken(a.Tok) if new { - code += "auto " + t := "auto" + + if isExpr0String(a.Rhs) { + t = "std::string" + } + + code += t + " " } - code += handleAssignStmtExpr(as.Lhs) + code += handleAssignStmtExpr(a.Lhs) code += tkn - code += handleAssignStmtExpr(as.Rhs) + code += handleAssignStmtExpr(a.Rhs) return } +func isExpr0String(ee []ast.Expr) bool { + if len(ee) == 0 { + return false + } + e0 := ee[0] + + switch e := e0.(type) { + case *ast.BasicLit: + return e.Kind == token.STRING + } + + return false +} + func handleAssignStmtExpr(e []ast.Expr) string { ops := make([]string, 0) code := ""