Сценарий: Инициализация переменной - тип auto

Этот коммит содержится в:
Softonik 2024-02-12 05:55:59 +03:00 коммит произвёл Nobody
родитель 1d39dfa762
коммит a62f3e06ad
2 изменённых файлов: 36 добавлений и 4 удалений

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

@ -87,4 +87,20 @@ void foo();
void foo() { void foo() {
std::string foo = "bar"; std::string foo = "bar";
} }
```
Сценарий: Инициализация переменной - тип auto
* Исходник:
```
package test
func foo() {
a := NewDevice(1,b,"stroka")
}
```
* Результат:
```
void foo();
void foo() {
auto a=NewDevice(1,b,"stroka");
}
``` ```

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

@ -3,6 +3,7 @@ package service
import ( import (
"fmt" "fmt"
"go/ast" "go/ast"
"go/token"
"strings" "strings"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
@ -57,12 +58,18 @@ func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string {
return code return code
} }
func handleAssignStmt(as *ast.AssignStmt) string { func handleAssignStmt(as *ast.AssignStmt) (code string) {
code := handleAssignStmtExpr(as.Lhs) tkn, new := handleToken(as.Tok)
code += as.Tok.String() if new {
code += "auto "
}
code += handleAssignStmtExpr(as.Lhs)
code += tkn
code += handleAssignStmtExpr(as.Rhs) code += handleAssignStmtExpr(as.Rhs)
return code return
} }
func handleAssignStmtExpr(e []ast.Expr) string { func handleAssignStmtExpr(e []ast.Expr) string {
ops := make([]string, 0) ops := make([]string, 0)
code := "" code := ""
@ -73,6 +80,15 @@ func handleAssignStmtExpr(e []ast.Expr) string {
return code return code
} }
func handleToken(t token.Token) (code string, new bool) {
st := t.String()
switch st {
case ":=":
return "=", true
}
return st, false
}
func handleIncDecStmt(as *ast.IncDecStmt) string { func handleIncDecStmt(as *ast.IncDecStmt) string {
code := handleExpr(as.X) code := handleExpr(as.X)
code += as.Tok.String() code += as.Tok.String()