Чистка: Expr обработчики вынесены в отдельный файл

Этот коммит содержится в:
Softonik 2024-01-22 03:26:48 +03:00 коммит произвёл Nobody
родитель 5bd52e1421
коммит 31fd0251de
4 изменённых файлов: 101 добавлений и 95 удалений

96
pkg/service/expr.go Обычный файл
Просмотреть файл

@ -0,0 +1,96 @@
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)
}
return code
}
func handleBasicLit(bl *ast.BasicLit) string {
return bl.Value
}
func handleUnaryExpr(expr *ast.UnaryExpr) string {
code := expr.Op.String()
code += handleExpr(expr.X)
return code
}
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 += "char*"
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:
code += handleIdentExpr(x)
}
code += "."
code += handleIdentExpr(s.Sel)
if val, ok := mapping[code]; ok {
code = val
}
return code
}

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

@ -161,36 +161,6 @@ func addFunctionDeclaration(f string) {
funcDeclarations = append(funcDeclarations, f)
}
func handleBasicLit(bl *ast.BasicLit) string {
return bl.Value
}
func handleUnaryExpr(expr *ast.UnaryExpr) string {
code := expr.Op.String()
code += handleExpr(expr.X)
return code
}
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 handleDecl(id int, decl ast.Decl, dst chan<- string, done chan<- bool) {
code := ""
switch d := decl.(type) {
@ -204,33 +174,6 @@ func handleDecl(id int, decl ast.Decl, dst chan<- string, done chan<- bool) {
done <- true
}
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 += handleIdent(e)
case *ast.ParenExpr:
code += handleParenExpr(e)
case *ast.SelectorExpr:
code += handleSelectorExpr(e)
}
return code
}
func handleParenExpr(stmt *ast.ParenExpr) string {
code := ""
code += handleExpr(stmt.X)
return code
}
func handleFuncDecl(decl ast.Decl) string {
fd := decl.(*ast.FuncDecl)
if shouldSkipFunction(fd.Type) {
@ -299,7 +242,7 @@ func handleFuncDeclParams(t *ast.FuncType) string {
ftype := ""
switch ft := field.Type.(type) {
case *ast.Ident:
ftype = handleIdent(ft)
ftype = handleIdentExpr(ft)
}
for _, names := range field.Names {
values = append(values, ftype+" "+names.Name)
@ -334,7 +277,7 @@ func handleFuncDeclType(t *ast.FuncType) string {
switch ft := fl.List[0].Type.(type) {
case *ast.Ident:
code += handleIdent(ft)
code += handleIdentExpr(ft)
}
return code
@ -352,36 +295,3 @@ func handleGenDecl(decl ast.Decl) string {
code += handleSpecs(gd.Specs)
return code
}
func handleIdent(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 += "char*"
default:
code += ident.Name
}
return code
}
func handleSelectorExpr(expr ast.Expr) string {
s := expr.(*ast.SelectorExpr)
code := ""
switch x := s.X.(type) {
case *ast.Ident:
code += handleIdent(x)
}
code += "."
code += handleIdent(s.Sel)
if val, ok := mapping[code]; ok {
code = val
}
return code
}

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

@ -23,7 +23,7 @@ func handleImportSpec(spec ast.Spec) string {
s := spec.(*ast.ImportSpec)
code := ""
if s.Name != nil {
name := handleIdent(s.Name)
name := handleIdentExpr(s.Name)
if val, ok := mapping[name]; ok {
name = val
}

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

@ -20,7 +20,7 @@ func handleValueSpec(spec ast.Spec) string {
func handleValueSpecNames(names []*ast.Ident) string {
code := ""
for _, name := range names {
code += handleIdent(name)
code += handleIdentExpr(name)
}
return code
}
@ -31,7 +31,7 @@ func handleValueSpecType(expr ast.Expr) string {
case *ast.SelectorExpr:
code += handleSelectorExpr(t)
case *ast.Ident:
code += handleIdent(t)
code += handleIdentExpr(t)
}
return code
}