122 строки
2,2 КиБ
Go
122 строки
2,2 КиБ
Go
package service
|
|
|
|
import (
|
|
"go/ast"
|
|
"strings"
|
|
)
|
|
|
|
func handleExpr(expr ast.Expr) string {
|
|
code := ""
|
|
switch e := expr.(type) {
|
|
case *ast.BasicLit:
|
|
code += handleBasicLit(e)
|
|
case *ast.UnaryExpr:
|
|
code += handleUnaryExpr(e)
|
|
case *ast.BinaryExpr:
|
|
code += handleBinaryExpr(e)
|
|
case *ast.CallExpr:
|
|
code += handleCallExpr(e)
|
|
case *ast.Ident:
|
|
code += handleIdentExpr(e)
|
|
case *ast.ParenExpr:
|
|
code += handleParenExpr(e)
|
|
case *ast.SelectorExpr:
|
|
code += handleSelectorExpr(e)
|
|
case *ast.StructType:
|
|
code += handleStructType(e)
|
|
}
|
|
return code
|
|
}
|
|
|
|
func handleBasicLit(bl *ast.BasicLit) string {
|
|
return bl.Value
|
|
}
|
|
|
|
func handleUnaryExpr(expr *ast.UnaryExpr) (code string) {
|
|
cl, ok := expr.X.(*ast.CompositeLit)
|
|
if ok {
|
|
code += "new "
|
|
code += handleIdentExpr(cl.Type)
|
|
code += "()"
|
|
} else {
|
|
code += expr.Op.String()
|
|
code += handleExpr(expr.X)
|
|
}
|
|
return
|
|
}
|
|
|
|
func handleBinaryExpr(expr ast.Expr) string {
|
|
be := expr.(*ast.BinaryExpr)
|
|
code := handleExpr(be.X)
|
|
code += be.Op.String()
|
|
code += handleExpr(be.Y)
|
|
return code
|
|
}
|
|
|
|
func handleCallExpr(expr *ast.CallExpr) string {
|
|
code := handleExpr(expr.Fun)
|
|
code += "("
|
|
args := make([]string, 0)
|
|
for _, arg := range expr.Args {
|
|
args = append(args, handleExpr(arg))
|
|
}
|
|
code += strings.Join(args, ",")
|
|
code += ")"
|
|
return code
|
|
}
|
|
|
|
func handleIdentExpr(expr ast.Expr) string {
|
|
ident := expr.(*ast.Ident)
|
|
code := ""
|
|
switch ident.Name {
|
|
case "nil":
|
|
code += "NULL"
|
|
case "uint32":
|
|
code += "unsigned long"
|
|
case "uint64":
|
|
code += "unsigned long long"
|
|
case "string":
|
|
code += "std::string"
|
|
default:
|
|
code += ident.Name
|
|
}
|
|
return code
|
|
}
|
|
|
|
func handleParenExpr(stmt *ast.ParenExpr) string {
|
|
code := ""
|
|
code += handleExpr(stmt.X)
|
|
return code
|
|
}
|
|
|
|
func handleSelectorExpr(expr ast.Expr) string {
|
|
s := expr.(*ast.SelectorExpr)
|
|
code := ""
|
|
switch x := s.X.(type) {
|
|
case *ast.Ident:
|
|
if isInMethod {
|
|
if x.Name == currentReceiverName {
|
|
code += "this"
|
|
} else {
|
|
code += handleIdentExpr(x)
|
|
}
|
|
code += "->"
|
|
} else {
|
|
code += handleIdentExpr(x)
|
|
code += "."
|
|
}
|
|
}
|
|
code += handleIdentExpr(s.Sel)
|
|
if val, ok := mapping[code]; ok {
|
|
code = val
|
|
}
|
|
return code
|
|
}
|
|
|
|
func handleStarExpr(s *ast.StarExpr) (code string) {
|
|
switch x := s.X.(type) {
|
|
case *ast.Ident:
|
|
code += handleIdentExpr(x)
|
|
}
|
|
return
|
|
}
|